Mercurial > hg > truffle
comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/SpecializationData.java @ 19289:62c43fcf5be2
Truffle-DSL: implement @Cached and fixes for the new guard expression syntax.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Tue, 03 Feb 2015 15:07:07 +0100 |
parents | 08aa0372dad4 |
children | f4792a544170 |
comparison
equal
deleted
inserted
replaced
19288:3a37116ef37f | 19289:62c43fcf5be2 |
---|---|
25 import java.util.*; | 25 import java.util.*; |
26 | 26 |
27 import javax.lang.model.element.*; | 27 import javax.lang.model.element.*; |
28 | 28 |
29 import com.oracle.truffle.dsl.processor.*; | 29 import com.oracle.truffle.dsl.processor.*; |
30 import com.oracle.truffle.dsl.processor.expression.*; | |
31 import com.oracle.truffle.dsl.processor.java.*; | |
30 | 32 |
31 public final class SpecializationData extends TemplateMethod { | 33 public final class SpecializationData extends TemplateMethod { |
32 | 34 |
33 public enum SpecializationKind { | 35 public enum SpecializationKind { |
34 UNINITIALIZED, | 36 UNINITIALIZED, |
50 private final Set<SpecializationData> excludedBy = new TreeSet<>(); | 52 private final Set<SpecializationData> excludedBy = new TreeSet<>(); |
51 private String insertBeforeName; | 53 private String insertBeforeName; |
52 private SpecializationData insertBefore; | 54 private SpecializationData insertBefore; |
53 private boolean reachable; | 55 private boolean reachable; |
54 private int index; | 56 private int index; |
57 private DSLExpression limitExpression; | |
55 | 58 |
56 public SpecializationData(NodeData node, TemplateMethod template, SpecializationKind kind, List<SpecializationThrowsData> exceptions) { | 59 public SpecializationData(NodeData node, TemplateMethod template, SpecializationKind kind, List<SpecializationThrowsData> exceptions) { |
57 super(template); | 60 super(template); |
58 this.node = node; | 61 this.node = node; |
59 this.kind = kind; | 62 this.kind = kind; |
63 for (SpecializationThrowsData exception : exceptions) { | 66 for (SpecializationThrowsData exception : exceptions) { |
64 exception.setSpecialization(this); | 67 exception.setSpecialization(this); |
65 } | 68 } |
66 } | 69 } |
67 | 70 |
71 public boolean isDynamicParameterBound(DSLExpression expression) { | |
72 Set<VariableElement> boundVariables = expression.findBoundVariableElements(); | |
73 for (Parameter parameter : getDynamicParameters()) { | |
74 if (boundVariables.contains(parameter.getVariableElement())) { | |
75 return true; | |
76 } | |
77 } | |
78 return false; | |
79 } | |
80 | |
68 public Parameter findByVariable(VariableElement variable) { | 81 public Parameter findByVariable(VariableElement variable) { |
69 for (Parameter parameter : getParameters()) { | 82 for (Parameter parameter : getParameters()) { |
70 if (parameter.getVariableElement() == variable) { | 83 if (ElementUtils.variableEquals(parameter.getVariableElement(), variable)) { |
71 return parameter; | 84 return parameter; |
72 } | 85 } |
73 } | 86 } |
74 return null; | 87 return null; |
88 } | |
89 | |
90 public DSLExpression getLimitExpression() { | |
91 return limitExpression; | |
92 } | |
93 | |
94 public void setLimitExpression(DSLExpression limitExpression) { | |
95 this.limitExpression = limitExpression; | |
75 } | 96 } |
76 | 97 |
77 public void setInsertBefore(SpecializationData insertBefore) { | 98 public void setInsertBefore(SpecializationData insertBefore) { |
78 this.insertBefore = insertBefore; | 99 this.insertBefore = insertBefore; |
79 } | 100 } |
114 return reachable; | 135 return reachable; |
115 } | 136 } |
116 | 137 |
117 public boolean isPolymorphic() { | 138 public boolean isPolymorphic() { |
118 return kind == SpecializationKind.POLYMORPHIC; | 139 return kind == SpecializationKind.POLYMORPHIC; |
140 } | |
141 | |
142 public List<Parameter> getDynamicParameters() { | |
143 List<Parameter> uncachedParameters = new ArrayList<>(getParameters()); | |
144 for (CacheExpression cacheExpression : getCaches()) { | |
145 uncachedParameters.remove(cacheExpression.getParameter()); | |
146 } | |
147 return uncachedParameters; | |
119 } | 148 } |
120 | 149 |
121 @Override | 150 @Override |
122 protected List<MessageContainer> findChildContainers() { | 151 protected List<MessageContainer> findChildContainers() { |
123 List<MessageContainer> sinks = new ArrayList<>(); | 152 List<MessageContainer> sinks = new ArrayList<>(); |
125 sinks.addAll(exceptions); | 154 sinks.addAll(exceptions); |
126 } | 155 } |
127 if (guards != null) { | 156 if (guards != null) { |
128 sinks.addAll(guards); | 157 sinks.addAll(guards); |
129 } | 158 } |
159 if (caches != null) { | |
160 sinks.addAll(caches); | |
161 } | |
130 return sinks; | 162 return sinks; |
131 } | 163 } |
132 | 164 |
133 public boolean hasRewrite(ProcessorContext context) { | 165 public boolean hasRewrite(ProcessorContext context) { |
134 if (!getExceptions().isEmpty()) { | 166 if (!getExceptions().isEmpty()) { |
138 return true; | 170 return true; |
139 } | 171 } |
140 if (!getAssumptions().isEmpty()) { | 172 if (!getAssumptions().isEmpty()) { |
141 return true; | 173 return true; |
142 } | 174 } |
175 | |
143 for (Parameter parameter : getSignatureParameters()) { | 176 for (Parameter parameter : getSignatureParameters()) { |
144 NodeChildData child = parameter.getSpecification().getExecution().getChild(); | 177 NodeChildData child = parameter.getSpecification().getExecution().getChild(); |
145 ExecutableTypeData type = child.findExecutableType(parameter.getTypeSystemType()); | 178 ExecutableTypeData type = child.findExecutableType(parameter.getTypeSystemType()); |
146 if (type == null) { | 179 if (type == null) { |
147 type = child.findAnyGenericExecutableType(context); | 180 type = child.findAnyGenericExecutableType(context); |
189 | 222 |
190 public int getIndex() { | 223 public int getIndex() { |
191 return index; | 224 return index; |
192 } | 225 } |
193 | 226 |
194 public boolean isContainedBy(SpecializationData next) { | |
195 if (compareTo(next) > 0) { | |
196 // must be declared after the current specialization | |
197 return false; | |
198 } | |
199 | |
200 Iterator<Parameter> currentSignature = getSignatureParameters().iterator(); | |
201 Iterator<Parameter> nextSignature = next.getSignatureParameters().iterator(); | |
202 | |
203 while (currentSignature.hasNext() && nextSignature.hasNext()) { | |
204 TypeData currentType = currentSignature.next().getTypeSystemType(); | |
205 TypeData prevType = nextSignature.next().getTypeSystemType(); | |
206 | |
207 if (!currentType.isImplicitSubtypeOf(prevType)) { | |
208 return false; | |
209 } | |
210 } | |
211 | |
212 for (String nextAssumption : next.getAssumptions()) { | |
213 if (!getAssumptions().contains(nextAssumption)) { | |
214 return false; | |
215 } | |
216 } | |
217 | |
218 Iterator<GuardExpression> nextGuards = next.getGuards().iterator(); | |
219 while (nextGuards.hasNext()) { | |
220 GuardExpression nextGuard = nextGuards.next(); | |
221 boolean implied = false; | |
222 for (GuardExpression currentGuard : getGuards()) { | |
223 if (currentGuard.implies(nextGuard)) { | |
224 implied = true; | |
225 break; | |
226 } | |
227 } | |
228 if (!implied) { | |
229 return false; | |
230 } | |
231 } | |
232 | |
233 return true; | |
234 } | |
235 | |
236 public NodeData getNode() { | 227 public NodeData getNode() { |
237 return node; | 228 return node; |
238 } | 229 } |
239 | 230 |
240 public void setGuards(List<GuardExpression> guards) { | 231 public void setGuards(List<GuardExpression> guards) { |
310 | 301 |
311 public List<AssumptionExpression> getAssumptionExpressions() { | 302 public List<AssumptionExpression> getAssumptionExpressions() { |
312 return assumptionExpressions; | 303 return assumptionExpressions; |
313 } | 304 } |
314 | 305 |
306 public boolean hasMultipleInstances() { | |
307 if (!getCaches().isEmpty()) { | |
308 for (GuardExpression guard : getGuards()) { | |
309 DSLExpression guardExpression = guard.getExpression(); | |
310 Set<VariableElement> boundVariables = guardExpression.findBoundVariableElements(); | |
311 if (isDynamicParameterBound(guardExpression)) { | |
312 for (CacheExpression cache : getCaches()) { | |
313 if (boundVariables.contains(cache.getParameter().getVariableElement())) { | |
314 return true; | |
315 } | |
316 } | |
317 } | |
318 } | |
319 } | |
320 | |
321 return false; | |
322 | |
323 } | |
324 | |
315 public boolean isReachableAfter(SpecializationData prev) { | 325 public boolean isReachableAfter(SpecializationData prev) { |
316 if (!prev.isSpecialized()) { | 326 if (!prev.isSpecialized()) { |
317 return true; | 327 return true; |
318 } | 328 } |
319 | 329 |
320 if (!prev.getExceptions().isEmpty()) { | 330 if (!prev.getExceptions().isEmpty()) { |
331 // may get excluded by exception | |
332 return true; | |
333 } | |
334 | |
335 if (hasMultipleInstances()) { | |
336 // may fallthrough due to limit | |
321 return true; | 337 return true; |
322 } | 338 } |
323 | 339 |
324 Iterator<Parameter> currentSignature = getSignatureParameters().iterator(); | 340 Iterator<Parameter> currentSignature = getSignatureParameters().iterator(); |
325 Iterator<Parameter> prevSignature = prev.getSignatureParameters().iterator(); | 341 Iterator<Parameter> prevSignature = prev.getSignatureParameters().iterator(); |
349 } | 365 } |
350 } | 366 } |
351 | 367 |
352 return false; | 368 return false; |
353 } | 369 } |
370 | |
371 public CacheExpression findCache(Parameter resolvedParameter) { | |
372 for (CacheExpression cache : getCaches()) { | |
373 if (cache.getParameter() == resolvedParameter) { | |
374 return cache; | |
375 } | |
376 } | |
377 return null; | |
378 } | |
379 | |
354 } | 380 } |