1 /* 2 * Copyright (c) 2016, 2018, 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.hotspot.meta; 26 27 import org.graalvm.compiler.core.common.spi.JavaConstantFieldProvider; 28 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 29 import org.graalvm.compiler.options.OptionValues; 30 31 import jdk.vm.ci.hotspot.HotSpotResolvedJavaField; 32 import jdk.vm.ci.meta.MetaAccessProvider; 33 import jdk.vm.ci.meta.ResolvedJavaField; 34 import jdk.vm.ci.meta.ResolvedJavaType; 35 36 /** 37 * Implements the default constant folding semantics for Java fields in the HotSpot VM. 38 */ 39 public class HotSpotConstantFieldProvider extends JavaConstantFieldProvider { 40 41 private final GraalHotSpotVMConfig config; 42 HotSpotConstantFieldProvider(GraalHotSpotVMConfig config, MetaAccessProvider metaAccess)43 public HotSpotConstantFieldProvider(GraalHotSpotVMConfig config, MetaAccessProvider metaAccess) { 44 super(metaAccess); 45 this.config = config; 46 } 47 48 @Override isStableField(ResolvedJavaField field, ConstantFieldTool<?> tool)49 protected boolean isStableField(ResolvedJavaField field, ConstantFieldTool<?> tool) { 50 if (!config.foldStableValues) { 51 return false; 52 } 53 if (field.isStatic() && !isStaticFieldConstant(field, tool.getOptions())) { 54 return false; 55 } 56 57 if (((HotSpotResolvedJavaField) field).isStable()) { 58 return true; 59 } 60 return super.isStableField(field, tool); 61 } 62 63 @Override isFinalField(ResolvedJavaField field, ConstantFieldTool<?> tool)64 protected boolean isFinalField(ResolvedJavaField field, ConstantFieldTool<?> tool) { 65 if (field.isStatic() && !isStaticFieldConstant(field, tool.getOptions())) { 66 return false; 67 } 68 69 return super.isFinalField(field, tool); 70 } 71 72 private static final String SystemClassName = "Ljava/lang/System;"; 73 isStaticFieldConstant(ResolvedJavaField field, @SuppressWarnings(R) OptionValues options)74 protected boolean isStaticFieldConstant(ResolvedJavaField field, @SuppressWarnings("unused") OptionValues options) { 75 ResolvedJavaType declaringClass = field.getDeclaringClass(); 76 return declaringClass.isInitialized() && !declaringClass.getName().equals(SystemClassName); 77 } 78 } 79