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