< prev index next >

src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.core.common/src/org/graalvm/compiler/core/common/spi/JavaConstantFieldProvider.java

Print this page




   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.core.common.spi;
  26 


  27 import org.graalvm.compiler.debug.GraalError;
  28 import org.graalvm.compiler.options.Option;
  29 import org.graalvm.compiler.options.OptionKey;
  30 
  31 import jdk.vm.ci.meta.JavaConstant;
  32 import jdk.vm.ci.meta.JavaType;
  33 import jdk.vm.ci.meta.MetaAccessProvider;
  34 import jdk.vm.ci.meta.ResolvedJavaField;
  35 import jdk.vm.ci.meta.ResolvedJavaType;
  36 
  37 /**
  38  * Utility for default constant folding semantics for Java fields.
  39  */
  40 public abstract class JavaConstantFieldProvider implements ConstantFieldProvider {
  41 
  42     static class Options {
  43         @Option(help = "Determines whether to treat final fields with default values as constant.")//
  44         public static final OptionKey<Boolean> TrustFinalDefaultFields = new OptionKey<>(true);
  45     }
  46 
  47     protected JavaConstantFieldProvider(MetaAccessProvider metaAccess) {
  48         try {
  49             this.stringValueField = metaAccess.lookupJavaField(String.class.getDeclaredField("value"));
  50             this.stringHashField = metaAccess.lookupJavaField(String.class.getDeclaredField("hash"));
  51         } catch (NoSuchFieldException | SecurityException e) {

















  52             throw new GraalError(e);
  53         }
  54     }
  55 
  56     @Override
  57     public <T> T readConstantField(ResolvedJavaField field, ConstantFieldTool<T> tool) {
  58         if (isStableField(field, tool)) {
  59             JavaConstant value = tool.readValue();
  60             if (value != null && isStableFieldValueConstant(field, value, tool)) {
  61                 return foldStableArray(value, field, tool);
  62             }
  63         }
  64         if (isFinalField(field, tool)) {
  65             JavaConstant value = tool.readValue();
  66             if (value != null && isFinalFieldValueConstant(field, value, tool)) {
  67                 return tool.foldConstant(value);
  68             }
  69         }
  70         return null;
  71     }




   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.core.common.spi;
  26 
  27 import java.util.Arrays;
  28 
  29 import org.graalvm.compiler.debug.GraalError;
  30 import org.graalvm.compiler.options.Option;
  31 import org.graalvm.compiler.options.OptionKey;
  32 
  33 import jdk.vm.ci.meta.JavaConstant;
  34 import jdk.vm.ci.meta.JavaType;
  35 import jdk.vm.ci.meta.MetaAccessProvider;
  36 import jdk.vm.ci.meta.ResolvedJavaField;
  37 import jdk.vm.ci.meta.ResolvedJavaType;
  38 
  39 /**
  40  * Utility for default constant folding semantics for Java fields.
  41  */
  42 public abstract class JavaConstantFieldProvider implements ConstantFieldProvider {
  43 
  44     static class Options {
  45         @Option(help = "Determines whether to treat final fields with default values as constant.")//
  46         public static final OptionKey<Boolean> TrustFinalDefaultFields = new OptionKey<>(true);
  47     }
  48 
  49     protected JavaConstantFieldProvider(MetaAccessProvider metaAccess) {
  50         try {
  51             ResolvedJavaType stringType = metaAccess.lookupJavaType(String.class);
  52             ResolvedJavaField[] stringFields = stringType.getInstanceFields(false);
  53             ResolvedJavaField valueField = null;
  54             ResolvedJavaField hashField = null;
  55             for (ResolvedJavaField field : stringFields) {
  56                 if (field.getName().equals("value")) {
  57                     valueField = field;
  58                 } else if (field.getName().equals("hash")) {
  59                     hashField = field;
  60                 }
  61             }
  62             if (valueField == null) {
  63                 throw new GraalError("missing field value " + Arrays.toString(stringFields));
  64             }
  65             if (hashField == null) {
  66                 throw new GraalError("missing field hash " + Arrays.toString(stringFields));
  67             }
  68             stringValueField = valueField;
  69             stringHashField = hashField;
  70         } catch (SecurityException e) {
  71             throw new GraalError(e);
  72         }
  73     }
  74 
  75     @Override
  76     public <T> T readConstantField(ResolvedJavaField field, ConstantFieldTool<T> tool) {
  77         if (isStableField(field, tool)) {
  78             JavaConstant value = tool.readValue();
  79             if (value != null && isStableFieldValueConstant(field, value, tool)) {
  80                 return foldStableArray(value, field, tool);
  81             }
  82         }
  83         if (isFinalField(field, tool)) {
  84             JavaConstant value = tool.readValue();
  85             if (value != null && isFinalFieldValueConstant(field, value, tool)) {
  86                 return tool.foldConstant(value);
  87             }
  88         }
  89         return null;
  90     }


< prev index next >