comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/node/NodeParser.java @ 8595:8a1115c92271

Implemented codegen guard definitions can now omit unused parameters.
author Christian Humer <christian.humer@gmail.com>
date Mon, 01 Apr 2013 21:43:20 +0200
parents a80bf36c6a1e
children 586b5e117c44
comparison
equal deleted inserted replaced
8594:ce6e8672f798 8595:8a1115c92271
333 // should not use messages from generic specialization 333 // should not use messages from generic specialization
334 uninializedMethod.getMessages().clear(); 334 uninializedMethod.getMessages().clear();
335 specializations.add(new SpecializationData(uninializedMethod, false, true)); 335 specializations.add(new SpecializationData(uninializedMethod, false, true));
336 } 336 }
337 337
338 Collections.sort(specializations, new Comparator<SpecializationData>() { 338 Collections.sort(specializations);
339
340 @Override
341 public int compare(SpecializationData o1, SpecializationData o2) {
342 return compareSpecialization(o1, o2);
343 }
344 });
345 339
346 node.setSpecializations(specializations); 340 node.setSpecializations(specializations);
347 341
348 List<SpecializationData> needsId = new ArrayList<>(); 342 List<SpecializationData> needsId = new ArrayList<>();
349 for (SpecializationData specialization : specializations) { 343 for (SpecializationData specialization : specializations) {
370 List<GuardData> foundGuards = new ArrayList<>(); 364 List<GuardData> foundGuards = new ArrayList<>();
371 List<ExecutableElement> methods = ElementFilter.methodsIn(elements); 365 List<ExecutableElement> methods = ElementFilter.methodsIn(elements);
372 for (String guardDefinition : specialization.getGuardDefinitions()) { 366 for (String guardDefinition : specialization.getGuardDefinitions()) {
373 GuardParser parser = new GuardParser(context, specialization, guardDefinition); 367 GuardParser parser = new GuardParser(context, specialization, guardDefinition);
374 List<GuardData> guards = parser.parse(methods); 368 List<GuardData> guards = parser.parse(methods);
375 Collections.sort(guards, new Comparator<GuardData>() {
376
377 @Override
378 public int compare(GuardData o1, GuardData o2) {
379 int compare = o1.compareBySignature(o2);
380 if (compare == 0) {
381 compare = o1.getId().compareTo(o2.getId());
382 }
383 if (compare == 0) {
384 TypeElement enclosingType1 = Utils.findNearestEnclosingType(o1.getMethod());
385 TypeElement enclosingType2 = Utils.findNearestEnclosingType(o2.getMethod());
386 compare = enclosingType1.getQualifiedName().toString().compareTo(enclosingType2.getQualifiedName().toString());
387 }
388 return compare;
389 }
390 });
391 if (!guards.isEmpty()) { 369 if (!guards.isEmpty()) {
392 foundGuards.add(guards.get(0)); 370 foundGuards.add(guards.get(0));
393 } else { 371 } else {
394 // error no guard found 372 // error no guard found
395 specialization.addError("No guard found with with name '%s'.", guardDefinition); 373 MethodSpec spec = parser.createSpecification(specialization.getMethod(), null);
374 spec.applyTypeDefinitions("types");
375 specialization.addError("Guard with method name '%s' not found. Expected signature: %n%s", guardDefinition, spec.toSignatureString("guard"));
396 } 376 }
397 } 377 }
398 378
399 specialization.setGuards(foundGuards); 379 specialization.setGuards(foundGuards);
400 380
571 551
572 private static void verifySpecializationParameters(NodeData nodeData) { 552 private static void verifySpecializationParameters(NodeData nodeData) {
573 boolean valid = true; 553 boolean valid = true;
574 int args = -1; 554 int args = -1;
575 for (SpecializationData specializationData : nodeData.getSpecializations()) { 555 for (SpecializationData specializationData : nodeData.getSpecializations()) {
576 int specializationArgs = 0; 556 int signatureArgs = 0;
577 for (ActualParameter param : specializationData.getParameters()) { 557 for (ActualParameter param : specializationData.getParameters()) {
578 if (!param.getSpecification().isOptional()) { 558 if (param.getSpecification().isSignature()) {
579 specializationArgs++; 559 signatureArgs++;
580 } 560 }
581 } 561 }
582 if (args != -1 && args != specializationArgs) { 562 if (args != -1 && args != signatureArgs) {
583 valid = false; 563 valid = false;
584 break; 564 break;
585 } 565 }
586 args = specializationArgs; 566 args = signatureArgs;
587 } 567 }
588 if (!valid) { 568 if (!valid) {
589 for (SpecializationData specialization : nodeData.getSpecializations()) { 569 for (SpecializationData specialization : nodeData.getSpecializations()) {
590 specialization.addError("All specializations must have the same number of arguments."); 570 specialization.addError("All specializations must have the same number of arguments.");
591 } 571 }
757 kind = FieldKind.FIELD; 737 kind = FieldKind.FIELD;
758 } 738 }
759 739
760 NodeFieldData fieldData = new NodeFieldData(var, findAccessElement(var), mirror, kind, execution); 740 NodeFieldData fieldData = new NodeFieldData(var, findAccessElement(var), mirror, kind, execution);
761 if (type != null && mirror != null) { 741 if (type != null && mirror != null) {
762 NodeData fieldNodeData = resolveNode(Utils.fromTypeMirror(type)); 742 TypeElement typeElement = Utils.fromTypeMirror(type);
743 if (typeElement == null) {
744 return null;
745 }
746 NodeData fieldNodeData = resolveNode(typeElement);
763 fieldData.setNode(fieldNodeData); 747 fieldData.setNode(fieldNodeData);
764 748
765 if (fieldNodeData == null) { 749 if (fieldNodeData == null) {
766 fieldData.addError("Node type '%s' is invalid.", Utils.getQualifiedName(type)); 750 fieldData.addError("Node type '%s' is invalid.", Utils.getQualifiedName(type));
767 } else if (fieldNodeData.findGenericExecutableTypes(context).isEmpty()) { 751 } else if (fieldNodeData.findGenericExecutableTypes(context).isEmpty()) {
972 List<SpecializationData> specializations = node.getSpecializations(); 956 List<SpecializationData> specializations = node.getSpecializations();
973 for (int i = 0; i < specializations.size(); i++) { 957 for (int i = 0; i < specializations.size(); i++) {
974 SpecializationData m1 = specializations.get(i); 958 SpecializationData m1 = specializations.get(i);
975 for (int j = i + 1; j < specializations.size(); j++) { 959 for (int j = i + 1; j < specializations.size(); j++) {
976 SpecializationData m2 = specializations.get(j); 960 SpecializationData m2 = specializations.get(j);
977 int inferredOrder = compareSpecialization(m1, m2); 961 int inferredOrder = m1.compareBySignature(m2);
978 962
979 if (m1.getOrder() != Specialization.DEFAULT_ORDER && m2.getOrder() != Specialization.DEFAULT_ORDER) { 963 if (m1.getOrder() != Specialization.DEFAULT_ORDER && m2.getOrder() != Specialization.DEFAULT_ORDER) {
980 int specOrder = m1.getOrder() - m2.getOrder(); 964 int specOrder = m1.getOrder() - m2.getOrder();
981 if (specOrder == 0) { 965 if (specOrder == 0) {
982 m1.addError("Order value %d used multiple times", m1.getOrder()); 966 m1.addError("Order value %d used multiple times", m1.getOrder());
1012 } 996 }
1013 } 997 }
1014 } 998 }
1015 } 999 }
1016 1000
1017 private static int compareSpecialization(SpecializationData m1, SpecializationData m2) {
1018 if (m1 == m2) {
1019 return 0;
1020 }
1021
1022 if (m1.getOrder() != Specialization.DEFAULT_ORDER && m2.getOrder() != Specialization.DEFAULT_ORDER) {
1023 return m1.getOrder() - m2.getOrder();
1024 } else if (m1.isUninitialized() ^ m2.isUninitialized()) {
1025 return m1.isUninitialized() ? -1 : 1;
1026 } else if (m1.isGeneric() ^ m2.isGeneric()) {
1027 return m1.isGeneric() ? 1 : -1;
1028 }
1029
1030 if (m1.getTemplate() != m2.getTemplate()) {
1031 throw new UnsupportedOperationException("Cannot compare two specializations with different templates.");
1032 }
1033
1034 return m1.compareBySignature(m2);
1035 }
1036
1037 @Override 1001 @Override
1038 public Class<? extends Annotation> getAnnotationType() { 1002 public Class<? extends Annotation> getAnnotationType() {
1039 return null; 1003 return null;
1040 } 1004 }
1041 1005