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