1/* 2 * Copyright (c) 2012, 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 */ 23package org.graalvm.compiler.hotspot.replacements; 24 25import static org.graalvm.compiler.hotspot.replacements.HotSpotReplacementsUtil.arrayBaseOffset; 26 27import java.util.zip.CRC32; 28 29import org.graalvm.compiler.api.replacements.ClassSubstitution; 30import org.graalvm.compiler.api.replacements.Fold; 31import org.graalvm.compiler.api.replacements.Fold.InjectedParameter; 32import org.graalvm.compiler.api.replacements.MethodSubstitution; 33import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; 34import org.graalvm.compiler.graph.Node.ConstantNodeParameter; 35import org.graalvm.compiler.graph.Node.NodeIntrinsic; 36import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 37import org.graalvm.compiler.hotspot.nodes.ComputeObjectAddressNode; 38import org.graalvm.compiler.hotspot.nodes.GraalHotSpotVMConfigNode; 39import org.graalvm.compiler.nodes.extended.ForeignCallNode; 40import org.graalvm.compiler.word.Word; 41import org.graalvm.word.Pointer; 42import org.graalvm.word.WordBase; 43import org.graalvm.word.WordFactory; 44 45import jdk.vm.ci.meta.JavaKind; 46 47// JaCoCo Exclude 48 49/** 50 * Substitutions for {@link CRC32}. 51 */ 52@ClassSubstitution(CRC32.class) 53public class CRC32Substitutions { 54 55 /** 56 * Gets the address of {@code StubRoutines::x86::_crc_table} in {@code stubRoutines_x86.hpp}. 57 */ 58 @Fold 59 static long crcTableAddress(@InjectedParameter GraalHotSpotVMConfig config) { 60 return config.crcTableAddress; 61 } 62 63 /** 64 * Removed in 9. 65 */ 66 @MethodSubstitution(optional = true) 67 static int update(int crc, int b) { 68 final Pointer crcTableRawAddress = WordFactory.pointer(GraalHotSpotVMConfigNode.crcTableAddress()); 69 70 int c = ~crc; 71 int index = (b ^ c) & 0xFF; 72 int offset = index << 2; 73 int result = crcTableRawAddress.readInt(offset); 74 result = result ^ (c >>> 8); 75 return ~result; 76 } 77 78 /** 79 * Removed in 9. 80 */ 81 @MethodSubstitution(optional = true) 82 static int updateBytes(int crc, byte[] buf, int off, int len) { 83 Word bufAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(buf, arrayBaseOffset(JavaKind.Byte) + off)); 84 return updateBytesCRC32(UPDATE_BYTES_CRC32, crc, bufAddr, len); 85 } 86 87 /** 88 * @since 9 89 */ 90 @MethodSubstitution(optional = true) 91 static int updateBytes0(int crc, byte[] buf, int off, int len) { 92 Word bufAddr = WordFactory.unsigned(ComputeObjectAddressNode.get(buf, arrayBaseOffset(JavaKind.Byte) + off)); 93 return updateBytesCRC32(UPDATE_BYTES_CRC32, crc, bufAddr, len); 94 } 95 96 /** 97 * Removed in 9. 98 */ 99 @MethodSubstitution(optional = true) 100 static int updateByteBuffer(int crc, long addr, int off, int len) { 101 WordBase bufAddr = WordFactory.unsigned(addr).add(off); 102 return updateBytesCRC32(UPDATE_BYTES_CRC32, crc, bufAddr, len); 103 } 104 105 /** 106 * @since 9 107 */ 108 @MethodSubstitution(optional = true) 109 static int updateByteBuffer0(int crc, long addr, int off, int len) { 110 WordBase bufAddr = WordFactory.unsigned(addr).add(off); 111 return updateBytesCRC32(UPDATE_BYTES_CRC32, crc, bufAddr, len); 112 } 113 114 public static final ForeignCallDescriptor UPDATE_BYTES_CRC32 = new ForeignCallDescriptor("updateBytesCRC32", int.class, int.class, WordBase.class, int.class); 115 116 @NodeIntrinsic(ForeignCallNode.class) 117 public static native int updateBytesCRC32(@ConstantNodeParameter ForeignCallDescriptor descriptor, int crc, WordBase buf, int length); 118} 119