1 /*
   2  * Copyright (c) 2011, 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;
  26 
  27 import org.graalvm.compiler.api.replacements.SnippetReflectionProvider;
  28 import org.graalvm.compiler.core.common.spi.ForeignCallsProvider;
  29 import org.graalvm.compiler.core.common.type.Stamp;
  30 import org.graalvm.compiler.core.common.type.StampFactory;
  31 import org.graalvm.compiler.core.common.type.TypeReference;
  32 import org.graalvm.compiler.debug.GraalError;
  33 import org.graalvm.compiler.nodes.graphbuilderconf.NodeIntrinsicPluginFactory.InjectionProvider;
  34 import org.graalvm.compiler.replacements.arraycopy.ArrayCopyForeignCalls;
  35 import org.graalvm.compiler.word.WordTypes;
  36 
  37 import jdk.vm.ci.meta.JavaKind;
  38 import jdk.vm.ci.meta.MetaAccessProvider;
  39 import jdk.vm.ci.meta.ResolvedJavaType;
  40 
  41 public class NodeIntrinsificationProvider implements InjectionProvider {
  42 
  43     private final MetaAccessProvider metaAccess;
  44     private final SnippetReflectionProvider snippetReflection;
  45     private final ForeignCallsProvider foreignCalls;
  46     private final WordTypes wordTypes;
  47 
  48     public NodeIntrinsificationProvider(MetaAccessProvider metaAccess, SnippetReflectionProvider snippetReflection, ForeignCallsProvider foreignCalls, WordTypes wordTypes) {
  49         this.metaAccess = metaAccess;
  50         this.snippetReflection = snippetReflection;
  51         this.foreignCalls = foreignCalls;
  52         this.wordTypes = wordTypes;
  53     }
  54 
  55     @Override
  56     public Stamp getInjectedStamp(Class<?> type, boolean nonNull) {
  57         JavaKind kind = JavaKind.fromJavaClass(type);
  58         if (kind == JavaKind.Object) {
  59             ResolvedJavaType returnType = metaAccess.lookupJavaType(type);
  60             if (wordTypes.isWord(returnType)) {
  61                 return wordTypes.getWordStamp(returnType);
  62             } else {
  63                 return StampFactory.object(TypeReference.createWithoutAssumptions(returnType), nonNull);
  64             }
  65         } else {
  66             return StampFactory.forKind(kind);
  67         }
  68     }
  69 
  70     @Override
  71     public <T> T getInjectedArgument(Class<T> type) {
  72         T injected = snippetReflection.getInjectedNodeIntrinsicParameter(type);
  73         if (injected != null) {
  74             return injected;
  75         } else if (type.equals(ForeignCallsProvider.class) || type.equals(ArrayCopyForeignCalls.class)) {
  76             return type.cast(foreignCalls);
  77         } else if (type.equals(SnippetReflectionProvider.class)) {
  78             return type.cast(snippetReflection);
  79         } else if (type.equals(WordTypes.class)) {
  80             return type.cast(wordTypes);
  81         } else {
  82             throw new GraalError("Cannot handle injected argument of type %s.", type.getName());
  83         }
  84     }
  85 }