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.ArrayOffsetProvider; 29 import org.graalvm.compiler.core.common.spi.ForeignCallsProvider; 30 import org.graalvm.compiler.core.common.type.Stamp; 31 import org.graalvm.compiler.core.common.type.StampFactory; 32 import org.graalvm.compiler.core.common.type.TypeReference; 33 import org.graalvm.compiler.debug.GraalError; 34 import org.graalvm.compiler.nodes.graphbuilderconf.NodeIntrinsicPluginFactory.InjectionProvider; 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 ArrayOffsetProvider arrayOffsetProvider; 47 private final WordTypes wordTypes; 48 49 public NodeIntrinsificationProvider(MetaAccessProvider metaAccess, SnippetReflectionProvider snippetReflection, ForeignCallsProvider foreignCalls, ArrayOffsetProvider arrayOffsetProvider, 50 WordTypes wordTypes) { 51 this.metaAccess = metaAccess; 52 this.snippetReflection = snippetReflection; 53 this.foreignCalls = foreignCalls; 54 this.arrayOffsetProvider = arrayOffsetProvider; 55 this.wordTypes = wordTypes; 56 } 57 58 @Override 59 public Stamp getInjectedStamp(Class<?> type, boolean nonNull) { 60 JavaKind kind = JavaKind.fromJavaClass(type); 61 if (kind == JavaKind.Object) { 62 ResolvedJavaType returnType = metaAccess.lookupJavaType(type); 63 if (wordTypes.isWord(returnType)) { 64 return wordTypes.getWordStamp(returnType); 65 } else { 66 return StampFactory.object(TypeReference.createWithoutAssumptions(returnType), nonNull); 67 } 68 } else { 69 return StampFactory.forKind(kind); 70 } 71 } 72 73 @Override 74 public <T> T getInjectedArgument(Class<T> type) { 75 T injected = snippetReflection.getInjectedNodeIntrinsicParameter(type); 76 if (injected != null) { 77 return injected; 78 } else if (type.equals(ForeignCallsProvider.class)) { 79 return type.cast(foreignCalls); 80 } else if (type.equals(SnippetReflectionProvider.class)) { 81 return type.cast(snippetReflection); 82 } else if (type.equals(ArrayOffsetProvider.class)) { 83 return type.cast(arrayOffsetProvider); 84 } else { 85 throw new GraalError("Cannot handle injected argument of type %s.", type.getName()); 86 } 87 } 88 }