comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/template/TemplateMethod.java @ 10596:f43eb2f1bbbc

Truffle-DSL: code-generation of polymorphic caching
author Christian Humer <christian.humer@gmail.com>
date Mon, 01 Jul 2013 20:32:20 +0200
parents a5d3e0973e83
children
comparison
equal deleted inserted replaced
10595:47233c73ca58 10596:f43eb2f1bbbc
48 this.template = template; 48 this.template = template;
49 this.specification = specification; 49 this.specification = specification;
50 this.method = method; 50 this.method = method;
51 this.markerAnnotation = markerAnnotation; 51 this.markerAnnotation = markerAnnotation;
52 this.returnType = returnType; 52 this.returnType = returnType;
53 this.parameters = parameters; 53 this.parameters = new ArrayList<>();
54 for (ActualParameter param : parameters) {
55 ActualParameter newParam = new ActualParameter(param);
56 this.parameters.add(newParam);
57 newParam.setMethod(this);
58 }
54 this.id = id; 59 this.id = id;
55
56 if (parameters != null) {
57 for (ActualParameter param : parameters) {
58 param.setMethod(this);
59 }
60 }
61 } 60 }
62 61
63 public TemplateMethod(TemplateMethod method) { 62 public TemplateMethod(TemplateMethod method) {
64 this(method.id, method.template, method.specification, method.method, method.markerAnnotation, method.returnType, method.parameters); 63 this(method.id, method.template, method.specification, method.method, method.markerAnnotation, method.returnType, method.parameters);
64 getMessages().addAll(method.getMessages());
65 }
66
67 public TemplateMethod(TemplateMethod method, ExecutableElement executable) {
68 this(method.id, method.template, method.specification, executable, method.markerAnnotation, method.returnType, method.parameters);
65 getMessages().addAll(method.getMessages()); 69 getMessages().addAll(method.getMessages());
66 } 70 }
67 71
68 public void setParameters(List<ActualParameter> parameters) { 72 public void setParameters(List<ActualParameter> parameters) {
69 this.parameters = parameters; 73 this.parameters = parameters;
196 prev = param; 200 prev = param;
197 } 201 }
198 return prev; 202 return prev;
199 } 203 }
200 204
201 public TypeData getReturnSignature() { 205 public Signature getSignature() {
202 return getReturnType().getTypeSystemType(); 206 Signature signature = new Signature();
203 } 207 for (ActualParameter parameter : getReturnTypeAndParameters()) {
204
205 public List<TypeData> getSignature() {
206 List<TypeData> types = new ArrayList<>();
207 for (ActualParameter parameter : getParameters()) {
208 if (!parameter.getSpecification().isSignature()) { 208 if (!parameter.getSpecification().isSignature()) {
209 continue; 209 continue;
210 } 210 }
211 TypeData typeData = parameter.getTypeSystemType(); 211 TypeData typeData = parameter.getTypeSystemType();
212 if (typeData != null) { 212 if (typeData != null) {
213 types.add(typeData); 213 signature.types.add(typeData);
214 } 214 }
215 } 215 }
216 return types; 216 return signature;
217 } 217 }
218 218
219 public List<ActualParameter> getSignatureParameters() { 219 public void updateSignature(Signature signature) {
220 List<ActualParameter> types = new ArrayList<>(); 220 assert signature.size() >= 1;
221 for (ActualParameter parameter : getParameters()) { 221 int signatureIndex = 0;
222 for (ActualParameter parameter : getReturnTypeAndParameters()) {
222 if (!parameter.getSpecification().isSignature()) { 223 if (!parameter.getSpecification().isSignature()) {
223 continue; 224 continue;
224 } 225 }
225 types.add(parameter); 226 TypeData newType = signature.get(signatureIndex++);
226 } 227 if (!parameter.getTypeSystemType().equals(newType)) {
227 return types; 228 replaceParameter(parameter.getLocalName(), new ActualParameter(parameter, newType));
229 }
230 }
228 } 231 }
229 232
230 @Override 233 @Override
231 public int compareTo(TemplateMethod o) { 234 public int compareTo(TemplateMethod o) {
232 if (this == o) { 235 if (this == o) {
264 TypeSystemData typeSystem = getTemplate().getTypeSystem(); 267 TypeSystemData typeSystem = getTemplate().getTypeSystem();
265 if (typeSystem != compareMethod.getTemplate().getTypeSystem()) { 268 if (typeSystem != compareMethod.getTemplate().getTypeSystem()) {
266 throw new IllegalStateException("Cannot compare two methods with different type systems."); 269 throw new IllegalStateException("Cannot compare two methods with different type systems.");
267 } 270 }
268 271
269 List<TypeData> signature1 = getSignature(); 272 Signature signature1 = getSignature();
270 List<TypeData> signature2 = compareMethod.getSignature(); 273 Signature signature2 = compareMethod.getSignature();
271 if (signature1.size() != signature2.size()) { 274 if (signature1.size() != signature2.size()) {
272 return signature2.size() - signature1.size(); 275 return signature2.size() - signature1.size();
273 } 276 }
274 277
275 int result = 0; 278 int result = 0;
276 for (int i = 0; i < signature1.size(); i++) { 279 for (int i = 1; i < signature1.size(); i++) {
277 int typeResult = compareActualParameter(typeSystem, signature1.get(i), signature2.get(i)); 280 int typeResult = compareActualParameter(typeSystem, signature1.get(i), signature2.get(i));
278 if (result == 0) { 281 if (result == 0) {
279 result = typeResult; 282 result = typeResult;
280 } else if (typeResult != 0 && Math.signum(result) != Math.signum(typeResult)) { 283 } else if (typeResult != 0 && Math.signum(result) != Math.signum(typeResult)) {
281 // We cannot define an order. 284 // We cannot define an order.
282 return 0; 285 return 0;
283 } 286 }
284 } 287 }
285 if (result == 0) { 288 if (result == 0 && signature1.size() > 0) {
286 TypeData returnSignature1 = getReturnSignature(); 289 result = compareActualParameter(typeSystem, signature1.get(0), signature2.get(0));
287 TypeData returnSignature2 = compareMethod.getReturnSignature();
288
289 result = compareActualParameter(typeSystem, returnSignature1, returnSignature2);
290 } 290 }
291 291
292 return result; 292 return result;
293 } 293 }
294 294
296 int index1 = typeSystem.findType(t1); 296 int index1 = typeSystem.findType(t1);
297 int index2 = typeSystem.findType(t2); 297 int index2 = typeSystem.findType(t2);
298 return index1 - index2; 298 return index1 - index2;
299 } 299 }
300 300
301 public static class Signature implements Iterable<TypeData>, Comparable<Signature> {
302
303 final List<TypeData> types;
304
305 public Signature() {
306 this.types = new ArrayList<>();
307 }
308
309 public Signature(List<TypeData> signature) {
310 this.types = signature;
311 }
312
313 @Override
314 public int hashCode() {
315 return types.hashCode();
316 }
317
318 public int compareTo(Signature o) {
319 if (o.size() != size()) {
320 return size() - o.size();
321 }
322
323 int typeSum = 0;
324 int otherTypeSum = 0;
325 for (int i = 0; i < types.size(); i++) {
326 TypeData type = types.get(i);
327 TypeData otherType = o.get(i);
328 typeSum += type.isGeneric() ? 1 : 0;
329 otherTypeSum += otherType.isGeneric() ? 1 : 0;
330 }
331
332 return typeSum - otherTypeSum;
333 }
334
335 public int size() {
336 return types.size();
337 }
338
339 public TypeData get(int index) {
340 return types.get(index);
341 }
342
343 public Signature combine(Signature genericSignature, Signature other) {
344 assert types.size() == other.types.size();
345 assert genericSignature.types.size() == other.types.size();
346
347 if (this.equals(other)) {
348 return this;
349 }
350
351 Signature signature = new Signature();
352 for (int i = 0; i < types.size(); i++) {
353 TypeData type1 = types.get(i);
354 TypeData type2 = other.types.get(i);
355 if (type1.equals(type2)) {
356 signature.types.add(type1);
357 } else {
358 signature.types.add(genericSignature.types.get(i));
359 }
360 }
361 return signature;
362 }
363
364 @Override
365 public boolean equals(Object obj) {
366 if (obj instanceof Signature) {
367 return ((Signature) obj).types.equals(types);
368 }
369 return super.equals(obj);
370 }
371
372 public Iterator<TypeData> iterator() {
373 return types.iterator();
374 }
375
376 @Override
377 public String toString() {
378 return types.toString();
379 }
380
381 public boolean hasAnyParameterMatch(Signature other) {
382 for (int i = 1; i < types.size(); i++) {
383 TypeData type1 = types.get(i);
384 TypeData type2 = other.types.get(i);
385 if (type1.equals(type2)) {
386 return true;
387 }
388 }
389 return false;
390 }
391 }
392
301 } 393 }