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.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("            assert checkInjectedArgument(b, args[%d], targetMethod);\n", argCount);
  88                 out.printf("            %s arg%d = %s;\n", param.asType(), argCount, deps.use(processor, (DeclaredType) param.asType()));
  89             }
  90             argCount++;
  91         }
  92 
  93         Set<String> suppressWarnings = new TreeSet<>();
  94         if (intrinsicMethod.getAnnotation(Deprecated.class) != null) {
  95             suppressWarnings.add("deprecation");
  96         }
  97         if (hasRawtypeWarning(intrinsicMethod.getReturnType())) {
  98             suppressWarnings.add("rawtypes");
  99         }
 100         for (VariableElement param : params) {
 101             if (hasUncheckedWarning(param.asType())) {
 102                 suppressWarnings.add("unchecked");
 103             }
 104         }
 105         if (suppressWarnings.size() > 0) {
 106             out.printf("            @SuppressWarnings({");
 107             String sep = "";
 108             for (String suppressWarning : suppressWarnings) {
 109                 out.printf("%s\"%s\"", sep, suppressWarning);
 110                 sep = ", ";
 111             }
 112             out.printf("})\n");
 113         }
 114 
 115         out.printf("            %s result = %s.%s(", getErasedType(intrinsicMethod.getReturnType()), receiver, intrinsicMethod.getSimpleName());
 116         if (argCount > firstArg) {
 117             out.printf("arg%d", firstArg);
 118             for (int i = firstArg + 1; i < argCount; i++) {
 119                 out.printf(", arg%d", i);
 120             }
 121         }
 122         out.printf(");\n");
 123 
 124         TypeMirror returnType = intrinsicMethod.getReturnType();
 125         switch (returnType.getKind()) {
 126             case BOOLEAN:
 127                 out.printf("            JavaConstant constant = JavaConstant.forInt(result ? 1 : 0);\n");
 128                 break;
 129             case BYTE:
 130             case SHORT:
 131             case CHAR:
 132             case INT:
 133                 out.printf("            JavaConstant constant = JavaConstant.forInt(result);\n");
 134                 break;
 135             case LONG:
 136                 out.printf("            JavaConstant constant = JavaConstant.forLong(result);\n");
 137                 break;
 138             case FLOAT:
 139                 out.printf("            JavaConstant constant = JavaConstant.forFloat(result);\n");
 140                 break;
 141             case DOUBLE:
 142                 out.printf("            JavaConstant constant = JavaConstant.forDouble(result);\n");
 143                 break;
 144             case ARRAY:
 145             case TYPEVAR:
 146             case DECLARED:
 147                 if (returnType.equals(processor.getType("java.lang.String"))) {
 148                     out.printf("            JavaConstant constant = %s.forString(result);\n", deps.use(WellKnownDependency.CONSTANT_REFLECTION));
 149                 } else {
 150                     out.printf("            JavaConstant constant = %s.forObject(result);\n", deps.use(WellKnownDependency.SNIPPET_REFLECTION));
 151                 }
 152                 break;
 153             default:
 154                 throw new IllegalArgumentException(returnType.toString());
 155         }
 156 
 157         out.printf("            ConstantNode node = ConstantNode.forConstant(constant, %s, %s);\n", deps.use(WellKnownDependency.META_ACCESS), deps.use(WellKnownDependency.STRUCTURED_GRAPH));
 158         out.printf("            b.push(JavaKind.%s, node);\n", getReturnKind(intrinsicMethod));
 159         out.printf("            b.notifyReplacedCall(targetMethod, node);\n");
 160         out.printf("            return true;\n");
 161 
 162         return deps;
 163     }
 164 }