001/* 002 * Copyright (c) 2014, 2014, Oracle and/or its affiliates. All rights reserved. 003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 004 * 005 * This code is free software; you can redistribute it and/or modify it 006 * under the terms of the GNU General Public License version 2 only, as 007 * published by the Free Software Foundation. 008 * 009 * This code is distributed in the hope that it will be useful, but WITHOUT 010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 011 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 012 * version 2 for more details (a copy is included in the LICENSE file that 013 * accompanied this code). 014 * 015 * You should have received a copy of the GNU General Public License version 016 * 2 along with this work; if not, write to the Free Software Foundation, 017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 018 * 019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 020 * or visit www.oracle.com if you need additional information or have any 021 * questions. 022 */ 023package com.oracle.graal.hotspot.meta; 024 025import jdk.internal.jvmci.hotspot.*; 026import jdk.internal.jvmci.meta.*; 027 028import com.oracle.graal.api.replacements.*; 029import com.oracle.graal.hotspot.*; 030 031public class HotSpotSnippetReflectionProvider implements SnippetReflectionProvider { 032 033 private final HotSpotGraalRuntimeProvider runtime; 034 035 public HotSpotSnippetReflectionProvider(HotSpotGraalRuntimeProvider runtime) { 036 this.runtime = runtime; 037 } 038 039 @Override 040 public JavaConstant forObject(Object object) { 041 return HotSpotObjectConstantImpl.forObject(object); 042 } 043 044 @Override 045 public Object asObject(ResolvedJavaType type, JavaConstant constant) { 046 if (constant.isNull()) { 047 return null; 048 } 049 HotSpotObjectConstant hsConstant = (HotSpotObjectConstant) constant; 050 return hsConstant.asObject(type); 051 } 052 053 @Override 054 public <T> T asObject(Class<T> type, JavaConstant constant) { 055 if (constant.isNull()) { 056 return null; 057 } 058 HotSpotObjectConstant hsConstant = (HotSpotObjectConstant) constant; 059 return hsConstant.asObject(type); 060 } 061 062 @Override 063 public JavaConstant forBoxed(Kind kind, Object value) { 064 return HotSpotObjectConstantImpl.forBoxedValue(kind, value); 065 } 066 067 public Object getSubstitutionGuardParameter(Class<?> type) { 068 if (type.isInstance(runtime)) { 069 return runtime; 070 } 071 if (type.isInstance(runtime.getConfig())) { 072 return runtime.getConfig(); 073 } 074 return null; 075 } 076 077 // Lazily initialized 078 private ResolvedJavaType wordTypesType; 079 private ResolvedJavaType runtimeType; 080 private ResolvedJavaType configType; 081 082 public Object getInjectedNodeIntrinsicParameter(ResolvedJavaType type) { 083 // Need to test all fields since there no guarantee under the JMM 084 // about the order in which these fields are written. 085 if (configType == null || wordTypesType == null || configType == null) { 086 MetaAccessProvider metaAccess = runtime.getHostProviders().getMetaAccess(); 087 wordTypesType = metaAccess.lookupJavaType(runtime.getHostProviders().getWordTypes().getClass()); 088 runtimeType = metaAccess.lookupJavaType(runtime.getClass()); 089 configType = metaAccess.lookupJavaType(runtime.getConfig().getClass()); 090 } 091 092 if (type.isAssignableFrom(wordTypesType)) { 093 return runtime.getHostProviders().getWordTypes(); 094 } 095 if (type.isAssignableFrom(runtimeType)) { 096 return runtime; 097 } 098 if (type.isAssignableFrom(configType)) { 099 return runtime.getConfig(); 100 } 101 return null; 102 } 103}