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