1 /*
   2  * Copyright (c) 2015, 2019, 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.FoldHandler.FOLD_CLASS_NAME;
  28 import static org.graalvm.compiler.replacements.processor.FoldHandler.INJECTED_PARAMETER_CLASS_NAME;
  29 
  30 import java.io.PrintWriter;
  31 import java.util.List;
  32 import java.util.Set;
  33 import java.util.TreeSet;
  34 
  35 import javax.lang.model.element.ExecutableElement;
  36 import javax.lang.model.element.Modifier;
  37 import javax.lang.model.element.TypeElement;
  38 import javax.lang.model.element.VariableElement;
  39 import javax.lang.model.type.DeclaredType;
  40 import javax.lang.model.type.TypeMirror;
  41 
  42 import org.graalvm.compiler.processor.AbstractProcessor;
  43 import org.graalvm.compiler.replacements.processor.InjectedDependencies.WellKnownDependency;
  44 
  45 /**
  46  * Create graph builder plugins for {@code Fold} methods.
  47  */
  48 public class GeneratedFoldPlugin extends GeneratedPlugin {
  49 
  50     public GeneratedFoldPlugin(ExecutableElement intrinsicMethod) {
  51         super(intrinsicMethod);
  52     }
  53 
  54     @Override
  55     protected TypeElement getAnnotationClass(AbstractProcessor processor) {
  56         return processor.getTypeElement(FOLD_CLASS_NAME);
  57     }
  58 
  59     @Override
  60     public void extraImports(Set<String> imports) {
  61         imports.add("jdk.vm.ci.meta.JavaConstant");
  62         imports.add("jdk.vm.ci.meta.JavaKind");
  63         imports.add("org.graalvm.compiler.nodes.ConstantNode");
  64     }
  65 
  66     @Override
  67     protected InjectedDependencies createExecute(AbstractProcessor processor, PrintWriter out) {
  68         InjectedDependencies deps = new InjectedDependencies();
  69         List<? extends VariableElement> params = intrinsicMethod.getParameters();
  70 
  71         int argCount = 0;
  72         Object receiver;
  73         if (intrinsicMethod.getModifiers().contains(Modifier.STATIC)) {
  74             receiver = intrinsicMethod.getEnclosingElement();
  75         } else {
  76             receiver = "arg0";
  77             TypeElement type = (TypeElement) intrinsicMethod.getEnclosingElement();
  78             constantArgument(processor, out, deps, argCount, type.asType(), argCount);
  79             argCount++;
  80         }
  81 
  82         int firstArg = argCount;
  83         for (VariableElement param : params) {
  84             if (processor.getAnnotation(param, processor.getType(INJECTED_PARAMETER_CLASS_NAME)) == null) {
  85                 constantArgument(processor, out, deps, argCount, param.asType(), argCount);
  86             } else {
  87                 out.printf("        if (!checkInjectedArgument(b, args[%d], targetMethod)) {\n", argCount);
  88                 out.printf("            return false;\n");
  89                 out.printf("        }\n", argCount);
  90                 out.printf("        %s arg%d = %s;\n", param.asType(), argCount, deps.use(processor, (DeclaredType) param.asType()));
  91             }
  92             argCount++;
  93         }
  94 
  95         Set<String> suppressWarnings = new TreeSet<>();
  96         if (intrinsicMethod.getAnnotation(Deprecated.class) != null) {
  97             suppressWarnings.add("deprecation");
  98         }
  99         if (hasRawtypeWarning(intrinsicMethod.getReturnType())) {
 100             suppressWarnings.add("rawtypes");
 101         }
 102         for (VariableElement param : params) {
 103             if (hasUncheckedWarning(param.asType())) {
 104                 suppressWarnings.add("unchecked");
 105             }
 106         }
 107         if (suppressWarnings.size() > 0) {
 108             out.printf("        @SuppressWarnings({");
 109             String sep = "";
 110             for (String suppressWarning : suppressWarnings) {
 111                 out.printf("%s\"%s\"", sep, suppressWarning);
 112                 sep = ", ";
 113             }
 114             out.printf("})\n");
 115         }
 116 
 117         out.printf("        %s result = %s.%s(", getErasedType(intrinsicMethod.getReturnType()), receiver, intrinsicMethod.getSimpleName());
 118         if (argCount > firstArg) {
 119             out.printf("arg%d", firstArg);
 120             for (int i = firstArg + 1; i < argCount; i++) {
 121                 out.printf(", arg%d", i);
 122             }
 123         }
 124         out.printf(");\n");
 125 
 126         TypeMirror returnType = intrinsicMethod.getReturnType();
 127         switch (returnType.getKind()) {
 128             case BOOLEAN:
 129                 out.printf("        JavaConstant constant = JavaConstant.forInt(result ? 1 : 0);\n");
 130                 break;
 131             case BYTE:
 132             case SHORT:
 133             case CHAR:
 134             case INT:
 135                 out.printf("        JavaConstant constant = JavaConstant.forInt(result);\n");
 136                 break;
 137             case LONG:
 138                 out.printf("        JavaConstant constant = JavaConstant.forLong(result);\n");
 139                 break;
 140             case FLOAT:
 141                 out.printf("        JavaConstant constant = JavaConstant.forFloat(result);\n");
 142                 break;
 143             case DOUBLE:
 144                 out.printf("        JavaConstant constant = JavaConstant.forDouble(result);\n");
 145                 break;
 146             case ARRAY:
 147             case TYPEVAR:
 148             case DECLARED:
 149                 if (returnType.equals(processor.getType("java.lang.String"))) {
 150                     out.printf("        JavaConstant constant = %s.forString(result);\n", deps.use(WellKnownDependency.CONSTANT_REFLECTION));
 151                 } else {
 152                     out.printf("        JavaConstant constant = %s.forObject(result);\n", deps.use(WellKnownDependency.SNIPPET_REFLECTION));
 153                 }
 154                 break;
 155             default:
 156                 throw new IllegalArgumentException(returnType.toString());
 157         }
 158 
 159         out.printf("        ConstantNode node = ConstantNode.forConstant(constant, %s, %s);\n", deps.use(WellKnownDependency.META_ACCESS), deps.use(WellKnownDependency.STRUCTURED_GRAPH));
 160         out.printf("        b.push(JavaKind.%s, node);\n", getReturnKind(intrinsicMethod));
 161         out.printf("        b.notifyReplacedCall(targetMethod, node);\n");
 162         out.printf("        return true;\n");
 163 
 164         return deps;
 165     }
 166 }