comparison graal/com.oracle.truffle.codegen.processor/src/com/oracle/truffle/codegen/processor/node/NodeCodeGenerator.java @ 7530:5e3d1a68664e

applied mx eclipseformat to all Java files
author Doug Simon <doug.simon@oracle.com>
date Wed, 23 Jan 2013 16:34:57 +0100
parents 6343a09b2ec1
children 5f3cba05c2fa
comparison
equal deleted inserted replaced
7529:4a11124a3563 7530:5e3d1a68664e
35 import com.oracle.truffle.codegen.processor.ast.*; 35 import com.oracle.truffle.codegen.processor.ast.*;
36 import com.oracle.truffle.codegen.processor.node.NodeFieldData.ExecutionKind; 36 import com.oracle.truffle.codegen.processor.node.NodeFieldData.ExecutionKind;
37 import com.oracle.truffle.codegen.processor.template.*; 37 import com.oracle.truffle.codegen.processor.template.*;
38 import com.oracle.truffle.codegen.processor.typesystem.*; 38 import com.oracle.truffle.codegen.processor.typesystem.*;
39 39
40
41 public class NodeCodeGenerator extends CompilationUnitFactory<NodeData> { 40 public class NodeCodeGenerator extends CompilationUnitFactory<NodeData> {
42 41
43 private static final String THIS_NODE_LOCAL_VAR_NAME = "thisNode"; 42 private static final String THIS_NODE_LOCAL_VAR_NAME = "thisNode";
44 43
45 public NodeCodeGenerator(ProcessorContext context) { 44 public NodeCodeGenerator(ProcessorContext context) {
65 if (name.startsWith("do")) { 64 if (name.startsWith("do")) {
66 name = name.substring(2); 65 name = name.substring(2);
67 } 66 }
68 } 67 }
69 name += nodeClassName(specialization.getNode()); 68 name += nodeClassName(specialization.getNode());
70 if (name.equals(Utils.getSimpleName(specialization.getNode().getNodeType())) 69 if (name.equals(Utils.getSimpleName(specialization.getNode().getNodeType())) || name.equals(Utils.getSimpleName(specialization.getNode().getTemplateType()))) {
71 || name.equals(Utils.getSimpleName(specialization.getNode().getTemplateType()))) {
72 name = name + "Impl"; 70 name = name + "Impl";
73 } 71 }
74 72
75 return name; 73 return name;
76 } 74 }
147 } 145 }
148 body.string("."); 146 body.string(".");
149 body.startCall(method.getMethodName()); 147 body.startCall(method.getMethodName());
150 } 148 }
151 149
152 private static void startCallTypeSystemMethod(ProcessorContext context, CodeTreeBuilder body, NodeData node, String methodName) { 150 private static void startCallTypeSystemMethod(ProcessorContext context, CodeTreeBuilder body, NodeData node, String methodName) {
153 VariableElement singleton = TypeSystemCodeGenerator.findSingleton(context, node.getTypeSystem()); 151 VariableElement singleton = TypeSystemCodeGenerator.findSingleton(context, node.getTypeSystem());
154 assert singleton != null; 152 assert singleton != null;
155 153
156 body.startGroup(); 154 body.startGroup();
157 body.staticReference(singleton.getEnclosingElement().asType(), singleton.getSimpleName().toString()); 155 body.staticReference(singleton.getEnclosingElement().asType(), singleton.getSimpleName().toString());
168 if (type == null || type.isGeneric()) { 166 if (type == null || type.isGeneric()) {
169 continue; 167 continue;
170 } 168 }
171 169
172 body.string(andOperator); 170 body.string(andOperator);
173 startCallTypeSystemMethod(context, body, specialization.getNode(), 171 startCallTypeSystemMethod(context, body, specialization.getNode(), TypeSystemCodeGenerator.isTypeMethodName(type));
174 TypeSystemCodeGenerator.isTypeMethodName(type));
175 body.string(valueName(specialization, param)); 172 body.string(valueName(specialization, param));
176 body.end().end(); // call 173 body.end().end(); // call
177 andOperator = " && "; 174 andOperator = " && ";
178 } 175 }
179 176
180 if (specialization.getGuards().length > 0) { 177 if (specialization.getGuards().length > 0) {
181 // Explicitly specified guards 178 // Explicitly specified guards
182 for (SpecializationGuardData guard : specialization.getGuards()) { 179 for (SpecializationGuardData guard : specialization.getGuards()) {
183 if ((guard.isOnSpecialization() && onSpecialization) 180 if ((guard.isOnSpecialization() && onSpecialization) || (guard.isOnExecution() && !onSpecialization)) {
184 || (guard.isOnExecution() && !onSpecialization)) {
185 body.string(andOperator); 181 body.string(andOperator);
186 182
187 startCallOperationMethod(body, guard.getGuardDeclaration()); 183 startCallOperationMethod(body, guard.getGuardDeclaration());
188 184
189 if (needsCast) { 185 if (needsCast) {
234 230
235 for (ExecutableElement executable : ElementFilter.constructorsIn(node.getTemplateType().getEnclosedElements())) { 231 for (ExecutableElement executable : ElementFilter.constructorsIn(node.getTemplateType().getEnclosedElements())) {
236 CodeExecutableElement superConstructor = createSuperConstructor(clazz, executable); 232 CodeExecutableElement superConstructor = createSuperConstructor(clazz, executable);
237 233
238 if (superConstructor != null) { 234 if (superConstructor != null) {
239 if (superConstructor.getParameters().size() == 1 235 if (superConstructor.getParameters().size() == 1 && Utils.typeEquals(superConstructor.getParameters().get(0).asType(), node.getTemplateType().asType())) {
240 && Utils.typeEquals(superConstructor.getParameters().get(0).asType(), node.getTemplateType().asType())) {
241 String originalName = superConstructor.getParameters().get(0).getSimpleName().toString(); 236 String originalName = superConstructor.getParameters().get(0).getSimpleName().toString();
242 superConstructor.getParameters().clear(); 237 superConstructor.getParameters().clear();
243 superConstructor.getParameters().add(new CodeVariableElement(clazz.asType(), originalName)); 238 superConstructor.getParameters().add(new CodeVariableElement(clazz.asType(), originalName));
244 } 239 }
245 clazz.add(superConstructor); 240 clazz.add(superConstructor);
326 if (constructor.getModifiers().contains(PRIVATE)) { 321 if (constructor.getModifiers().contains(PRIVATE)) {
327 continue; 322 continue;
328 } 323 }
329 324
330 // skip node rewrite constructor 325 // skip node rewrite constructor
331 if (constructor.getParameters().size() == 1 326 if (constructor.getParameters().size() == 1 && typeEquals(constructor.getParameters().get(0).asType(), node.getNodeType())) {
332 && typeEquals(constructor.getParameters().get(0).asType(), node.getNodeType())) {
333 continue; 327 continue;
334 } 328 }
335 329
336 clazz.add(createCreateMethod(node, createVisibility, constructor)); 330 clazz.add(createCreateMethod(node, createVisibility, constructor));
337 } 331 }
425 body.startThrow().startNew(getContext().getType(IllegalArgumentException.class)).end().end(); 419 body.startThrow().startNew(getContext().getType(IllegalArgumentException.class)).end().end();
426 420
427 return method; 421 return method;
428 } 422 }
429 423
430
431 private CodeExecutableElement createGeneratedGenericMethod(NodeData node) { 424 private CodeExecutableElement createGeneratedGenericMethod(NodeData node) {
432 CodeExecutableElement method = new CodeExecutableElement(modifiers(PRIVATE, STATIC), node.getGenericSpecialization().getReturnType().getActualType(), "generatedGeneric"); 425 CodeExecutableElement method = new CodeExecutableElement(modifiers(PRIVATE, STATIC), node.getGenericSpecialization().getReturnType().getActualType(), "generatedGeneric");
433 method.addParameter(new CodeVariableElement(node.getNodeType(), THIS_NODE_LOCAL_VAR_NAME)); 426 method.addParameter(new CodeVariableElement(node.getNodeType(), THIS_NODE_LOCAL_VAR_NAME));
434 addValueParameters(method, node.getGenericSpecialization(), true); 427 addValueParameters(method, node.getGenericSpecialization(), true);
435 428
479 } 472 }
480 } 473 }
481 } 474 }
482 475
483 private class SpecializedNodeFactory extends ClassElementFactory<SpecializationData> { 476 private class SpecializedNodeFactory extends ClassElementFactory<SpecializationData> {
484
485 477
486 public SpecializedNodeFactory(ProcessorContext context) { 478 public SpecializedNodeFactory(ProcessorContext context) {
487 super(context); 479 super(context);
488 } 480 }
489 481
673 } 665 }
674 666
675 private void buildGenericValueExecute(CodeTreeBuilder builder, SpecializationData specialization, NodeFieldData field, NodeFieldData exceptionSpec) { 667 private void buildGenericValueExecute(CodeTreeBuilder builder, SpecializationData specialization, NodeFieldData field, NodeFieldData exceptionSpec) {
676 ActualParameter specParameter = specialization.findParameter(field.getName()); 668 ActualParameter specParameter = specialization.findParameter(field.getName());
677 669
678 boolean shortCircuit = startShortCircuit(builder, specialization, 670 boolean shortCircuit = startShortCircuit(builder, specialization, field, exceptionSpec);
679 field, exceptionSpec);
680 671
681 builder.startStatement(); 672 builder.startStatement();
682 if (!shortCircuit) { 673 if (!shortCircuit) {
683 builder.type(specialization.getNode().getTypeSystem().getGenericType()); 674 builder.type(specialization.getNode().getTypeSystem().getGenericType());
684 builder.string(" "); 675 builder.string(" ");
713 builder.string("frame"); 704 builder.string("frame");
714 } 705 }
715 builder.end(); 706 builder.end();
716 } 707 }
717 708
718
719 private void buildSpecializedValueExecute(CodeTreeBuilder builder, SpecializationData specialization, NodeFieldData field) { 709 private void buildSpecializedValueExecute(CodeTreeBuilder builder, SpecializationData specialization, NodeFieldData field) {
720 ActualParameter param = specialization.findParameter(field.getName()); 710 ActualParameter param = specialization.findParameter(field.getName());
721 boolean shortCircuit = startShortCircuit(builder, specialization, field, null); 711 boolean shortCircuit = startShortCircuit(builder, specialization, field, null);
722 712
723 if (!shortCircuit) { 713 if (!shortCircuit) {
731 } 721 }
732 722
733 builder.startStatement().string(valueName(field)).string(" = "); 723 builder.startStatement().string(valueName(field)).string(" = ");
734 buildExecute(builder, field, execType); 724 buildExecute(builder, field, execType);
735 builder.end(); 725 builder.end();
736
737 726
738 if (execType.hasUnexpectedValue(getContext())) { 727 if (execType.hasUnexpectedValue(getContext())) {
739 builder.end().startCatchBlock(getUnexpectedValueException(), "ex"); 728 builder.end().startCatchBlock(getUnexpectedValueException(), "ex");
740 boolean execute = false; 729 boolean execute = false;
741 for (NodeFieldData exField : specialization.getNode().getFields()) { 730 for (NodeFieldData exField : specialization.getNode().getFields()) {
754 743
755 endShortCircuit(builder, shortCircuit); 744 endShortCircuit(builder, shortCircuit);
756 builder.newLine(); 745 builder.newLine();
757 } 746 }
758 747
759 748 private boolean startShortCircuit(CodeTreeBuilder builder, SpecializationData specialization, NodeFieldData forField, NodeFieldData exceptionField) {
760 private boolean startShortCircuit(CodeTreeBuilder builder, SpecializationData specialization,
761 NodeFieldData forField, NodeFieldData exceptionField) {
762 if (forField.getExecutionKind() != ExecutionKind.SHORT_CIRCUIT) { 749 if (forField.getExecutionKind() != ExecutionKind.SHORT_CIRCUIT) {
763 return false; 750 return false;
764 } 751 }
765 752
766 ActualParameter parameter = specialization.findParameter(forField.getName()); 753 ActualParameter parameter = specialization.findParameter(forField.getName());
783 addValueParameterNames(builder, shortCircuitData, exceptionField != null ? exceptionField.getName() : null, false); 770 addValueParameterNames(builder, shortCircuitData, exceptionField != null ? exceptionField.getName() : null, false);
784 builder.end().end(); // call operation 771 builder.end().end(); // call operation
785 772
786 builder.end(); // statement 773 builder.end(); // statement
787 774
788 builder.declaration(parameter.getActualType(), valueName(specialization, parameter), 775 builder.declaration(parameter.getActualType(), valueName(specialization, parameter), CodeTreeBuilder.createBuilder().defaultValue(parameter.getActualType()));
789 CodeTreeBuilder.createBuilder().defaultValue(parameter.getActualType()));
790 builder.startIf().string(shortCircuitParam.getSpecification().getName()).end(); 776 builder.startIf().string(shortCircuitParam.getSpecification().getName()).end();
791 builder.startBlock(); 777 builder.startBlock();
792 778
793 return true; 779 return true;
794 } 780 }
795
796 781
797 private void endShortCircuit(CodeTreeBuilder builder, boolean shortCircuit) { 782 private void endShortCircuit(CodeTreeBuilder builder, boolean shortCircuit) {
798 if (shortCircuit) { 783 if (shortCircuit) {
799 builder.end(); 784 builder.end();
800 } 785 }