1 /*
   2  * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 
  25 package org.graalvm.compiler.replacements.processor;
  26 
  27 import static org.graalvm.compiler.replacements.processor.NodeIntrinsicHandler.CONSTANT_NODE_PARAMETER_CLASS_NAME;
  28 import static org.graalvm.compiler.replacements.processor.NodeIntrinsicHandler.INJECTED_NODE_PARAMETER_CLASS_NAME;
  29 import static org.graalvm.compiler.replacements.processor.NodeIntrinsicHandler.VALUE_NODE_CLASS_NAME;
  30 
  31 import java.io.PrintWriter;
  32 import java.util.List;
  33 import java.util.Set;
  34 
  35 import javax.lang.model.element.ExecutableElement;
  36 import javax.lang.model.element.TypeElement;
  37 import javax.lang.model.element.VariableElement;
  38 import javax.lang.model.type.DeclaredType;
  39 import javax.lang.model.type.TypeKind;
  40 import javax.lang.model.type.TypeMirror;
  41 
  42 import org.graalvm.compiler.processor.AbstractProcessor;
  43 
  44 /**
  45  * Create graph builder plugins for {@code NodeIntrinsic} methods.
  46  */
  47 public abstract class GeneratedNodeIntrinsicPlugin extends GeneratedPlugin {
  48 
  49     private final TypeMirror[] signature;
  50 
  51     public GeneratedNodeIntrinsicPlugin(ExecutableElement intrinsicMethod, TypeMirror[] signature) {
  52         super(intrinsicMethod);
  53         this.signature = signature;
  54     }
  55 
  56     @Override
  57     protected TypeElement getAnnotationClass(AbstractProcessor processor) {
  58         return processor.getTypeElement(NodeIntrinsicHandler.NODE_INTRINSIC_CLASS_NAME);
  59     }
  60 
  61     protected abstract List<? extends VariableElement> getParameters();
  62 
  63     protected abstract void factoryCall(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps, int argCount);
  64 
  65     @Override
  66     protected InjectedDependencies createExecute(AbstractProcessor processor, PrintWriter out) {
  67         InjectedDependencies deps = new InjectedDependencies();
  68 
  69         List<? extends VariableElement> params = getParameters();
  70 
  71         int idx = 0;
  72         for (; idx < params.size(); idx++) {
  73             VariableElement param = params.get(idx);
  74             if (processor.getAnnotation(param, processor.getType(INJECTED_NODE_PARAMETER_CLASS_NAME)) == null) {
  75                 break;
  76             }
  77 
  78             out.printf("            %s arg%d = %s;\n", param.asType(), idx, deps.use(processor, (DeclaredType) param.asType()));
  79         }
  80 
  81         for (int i = 0; i < signature.length; i++, idx++) {
  82             if (processor.getAnnotation(intrinsicMethod.getParameters().get(i), processor.getType(CONSTANT_NODE_PARAMETER_CLASS_NAME)) != null) {
  83                 constantArgument(processor, out, deps, idx, signature[i], i);
  84             } else {
  85                 if (signature[i].equals(processor.getType(VALUE_NODE_CLASS_NAME))) {
  86                     out.printf("            ValueNode arg%d = args[%d];\n", idx, i);
  87                 } else {
  88                     out.printf("            %s arg%d = (%s) args[%d];\n", signature[i], idx, signature[i], i);
  89                 }
  90             }
  91         }
  92 
  93         factoryCall(processor, out, deps, idx);
  94 
  95         return deps;
  96     }
  97 
  98     public static class ConstructorPlugin extends GeneratedNodeIntrinsicPlugin {
  99 
 100         private final ExecutableElement constructor;
 101 
 102         public ConstructorPlugin(ExecutableElement intrinsicMethod, ExecutableElement constructor, TypeMirror[] signature) {
 103             super(intrinsicMethod, signature);
 104             this.constructor = constructor;
 105         }
 106 
 107         @Override
 108         public void extraImports(Set<String> imports) {
 109             if (intrinsicMethod.getReturnType().getKind() != TypeKind.VOID) {
 110                 imports.add("jdk.vm.ci.meta.JavaKind");
 111             }
 112         }
 113 
 114         @Override
 115         protected List<? extends VariableElement> getParameters() {
 116             return constructor.getParameters();
 117         }
 118 
 119         @Override
 120         protected void factoryCall(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps, int argCount) {
 121             out.printf("            %s node = new %s(", constructor.getEnclosingElement(), constructor.getEnclosingElement());
 122             if (argCount > 0) {
 123                 out.printf("arg0");
 124                 for (int i = 1; i < argCount; i++) {
 125                     out.printf(", arg%d", i);
 126                 }
 127             }
 128             out.printf(");\n");
 129 
 130             if (intrinsicMethod.getReturnType().getKind() == TypeKind.VOID) {
 131                 out.printf("            b.add(node);\n");
 132             } else {
 133                 out.printf("            b.addPush(JavaKind.%s, node);\n", getReturnKind(intrinsicMethod));
 134             }
 135             out.printf("            return true;\n");
 136         }
 137     }
 138 
 139     public static class CustomFactoryPlugin extends GeneratedNodeIntrinsicPlugin {
 140 
 141         private final ExecutableElement customFactory;
 142 
 143         public CustomFactoryPlugin(ExecutableElement intrinsicMethod, ExecutableElement customFactory, TypeMirror[] signature) {
 144             super(intrinsicMethod, signature);
 145             this.customFactory = customFactory;
 146         }
 147 
 148         @Override
 149         public void extraImports(Set<String> imports) {
 150         }
 151 
 152         @Override
 153         protected List<? extends VariableElement> getParameters() {
 154             List<? extends VariableElement> ret = customFactory.getParameters();
 155             // remove initial GraphBuilderContext and ResolvedJavaMethod parameters
 156             return ret.subList(2, ret.size());
 157         }
 158 
 159         @Override
 160         protected void factoryCall(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps, int argCount) {
 161             out.printf("            return %s.%s(b, targetMethod", customFactory.getEnclosingElement(), customFactory.getSimpleName());
 162             for (int i = 0; i < argCount; i++) {
 163                 out.printf(", arg%d", i);
 164             }
 165             out.printf(");\n");
 166         }
 167     }
 168 }