comparison graal/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/template/TemplateMethod.java @ 10600:e93efe3ba5f4

Truffle-DSL: rewritten polymorphic optimization for simpler generated code.
author Christian Humer <christian.humer@gmail.com>
date Tue, 02 Jul 2013 14:51:05 +0200
parents 79041ab43660
children b8fe1fe004ec
comparison
equal deleted inserted replaced
10599:ebf5c5c23564 10600:e93efe3ba5f4
26 26
27 import javax.lang.model.element.*; 27 import javax.lang.model.element.*;
28 import javax.lang.model.type.*; 28 import javax.lang.model.type.*;
29 29
30 import com.oracle.truffle.dsl.processor.*; 30 import com.oracle.truffle.dsl.processor.*;
31 import com.oracle.truffle.dsl.processor.template.TemplateMethod.Signature;
31 import com.oracle.truffle.dsl.processor.typesystem.*; 32 import com.oracle.truffle.dsl.processor.typesystem.*;
32 33
33 /** 34 /**
34 * Note: this class has a natural ordering that is inconsistent with equals. 35 * Note: this class has a natural ordering that is inconsistent with equals.
35 */ 36 */
313 @Override 314 @Override
314 public int hashCode() { 315 public int hashCode() {
315 return types.hashCode(); 316 return types.hashCode();
316 } 317 }
317 318
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() { 319 public int size() {
336 return types.size(); 320 return types.size();
337 } 321 }
338 322
339 public TypeData get(int index) { 323 public TypeData get(int index) {
340 return types.get(index); 324 return types.get(index);
325 }
326
327 public int compareTo(Signature other) {
328 if (this == other) {
329 return 0;
330 } else if (types.size() != other.types.size()) {
331 return types.size() - other.types.size();
332 } else if (types.isEmpty()) {
333 return 0;
334 }
335
336 for (int i = 0; i < types.size(); i++) {
337 TypeData type1 = types.get(i);
338 TypeData type2 = other.types.get(i);
339
340 int comparison = type1.compareTo(type2);
341 if (comparison != 0) {
342 return comparison;
343 }
344 }
345
346 return 0;
341 } 347 }
342 348
343 public Signature combine(Signature genericSignature, Signature other) { 349 public Signature combine(Signature genericSignature, Signature other) {
344 assert types.size() == other.types.size(); 350 assert types.size() == other.types.size();
345 assert genericSignature.types.size() == other.types.size(); 351 assert genericSignature.types.size() == other.types.size();
386 return true; 392 return true;
387 } 393 }
388 } 394 }
389 return false; 395 return false;
390 } 396 }
397
398 public boolean isCompatibleTo(Signature signature) {
399 if (size() != signature.size()) {
400 return false;
401 }
402
403 for (int i = 0; i < size(); i++) {
404 TypeData o1 = get(i);
405 TypeData o2 = signature.get(i);
406 if (o1.equals(o2)) {
407 continue;
408 } else if (o2.isGeneric()) {
409 continue;
410 } else {
411 return false;
412 }
413 }
414 return true;
415 }
391 } 416 }
392 417
393 } 418 }