1 /* 2 * Copyright (c) 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.replacements; 26 27 import static org.graalvm.compiler.hotspot.GraalHotSpotVMConfigBase.INJECTED_VMCONFIG; 28 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.SLOW_PATH_PROBABILITY; 29 import static org.graalvm.compiler.nodes.extended.BranchProbabilityNode.probability; 30 31 import jdk.vm.ci.meta.DeoptimizationAction; 32 import jdk.vm.ci.meta.DeoptimizationReason; 33 import jdk.vm.ci.meta.JavaKind; 34 import org.graalvm.compiler.api.replacements.ClassSubstitution; 35 import org.graalvm.compiler.api.replacements.MethodSubstitution; 36 import org.graalvm.compiler.nodes.DeoptimizeNode; 37 import org.graalvm.compiler.nodes.java.NewArrayNode; 38 import org.graalvm.compiler.replacements.arraycopy.ArrayCopyCallNode; 39 40 // JaCoCo Exclude 41 42 /** 43 * Substitutions for {@code StringUTF16} methods for JDK9 and later. 44 */ 45 @ClassSubstitution(className = "java.lang.StringUTF16", optional = true) 46 public class StringUTF16Substitutions { 47 48 private static final int MAX_LENGTH = Integer.MAX_VALUE >> 1; 49 50 @MethodSubstitution 51 public static byte[] toBytes(char[] value, int srcBegin, int length) { 52 if (probability(SLOW_PATH_PROBABILITY, srcBegin < 0) || 53 probability(SLOW_PATH_PROBABILITY, length < 0) || 54 probability(SLOW_PATH_PROBABILITY, length > MAX_LENGTH) || 55 probability(SLOW_PATH_PROBABILITY, srcBegin > value.length - length)) { 56 DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.BoundsCheckException); 57 } 58 byte[] val = (byte[]) NewArrayNode.newUninitializedArray(Byte.TYPE, length << 1); 59 // the intrinsic does not perform bounds/type checks, so it can be used here. 60 // Using KillsAny variant since we are reading and writing 2 different types. 61 ArrayCopyCallNode.disjointArraycopyKillsAny(value, srcBegin, val, 0, length, JavaKind.Char, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG)); 62 return val; 63 } 64 65 @MethodSubstitution 66 public static void getChars(byte[] value, int srcBegin, int srcEnd, char[] dst, int dstBegin) { 67 int length = srcEnd - srcBegin; 68 if (probability(SLOW_PATH_PROBABILITY, srcBegin < 0) || 69 probability(SLOW_PATH_PROBABILITY, length < 0) || 70 probability(SLOW_PATH_PROBABILITY, srcBegin > (value.length >> 1) - length) || 71 probability(SLOW_PATH_PROBABILITY, dstBegin > dst.length - length)) { 72 DeoptimizeNode.deopt(DeoptimizationAction.None, DeoptimizationReason.BoundsCheckException); 73 } 74 // The intrinsic does not perform bounds/type checks, so it can be used here. 75 // Using KillsAny variant since we are reading and writing 2 different types. 76 ArrayCopyCallNode.disjointArraycopyKillsAny(value, srcBegin, dst, dstBegin, length, JavaKind.Char, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG)); 77 } 78 }