1 /*
   2  * Copyright (c) 2011, 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 
  29 import java.lang.reflect.Method;
  30 import java.util.EnumMap;
  31 
  32 import org.graalvm.compiler.api.directives.GraalDirectives;
  33 import org.graalvm.compiler.api.replacements.Snippet;
  34 import org.graalvm.compiler.debug.GraalError;
  35 import org.graalvm.compiler.replacements.arraycopy.ArrayCopyCallNode;
  36 import org.graalvm.compiler.nodes.java.DynamicNewArrayNode;
  37 import org.graalvm.compiler.nodes.java.NewArrayNode;
  38 import org.graalvm.compiler.replacements.Snippets;
  39 
  40 import jdk.vm.ci.meta.JavaKind;
  41 
  42 public class ObjectCloneSnippets implements Snippets {
  43 
  44     public static final EnumMap<JavaKind, Method> arrayCloneMethods = new EnumMap<>(JavaKind.class);
  45 
  46     static {
  47         arrayCloneMethods.put(JavaKind.Boolean, getCloneMethod("booleanArrayClone", boolean[].class));
  48         arrayCloneMethods.put(JavaKind.Byte, getCloneMethod("byteArrayClone", byte[].class));
  49         arrayCloneMethods.put(JavaKind.Char, getCloneMethod("charArrayClone", char[].class));
  50         arrayCloneMethods.put(JavaKind.Short, getCloneMethod("shortArrayClone", short[].class));
  51         arrayCloneMethods.put(JavaKind.Int, getCloneMethod("intArrayClone", int[].class));
  52         arrayCloneMethods.put(JavaKind.Float, getCloneMethod("floatArrayClone", float[].class));
  53         arrayCloneMethods.put(JavaKind.Long, getCloneMethod("longArrayClone", long[].class));
  54         arrayCloneMethods.put(JavaKind.Double, getCloneMethod("doubleArrayClone", double[].class));
  55         arrayCloneMethods.put(JavaKind.Object, getCloneMethod("objectArrayClone", Object[].class));
  56     }
  57 
  58     private static Method getCloneMethod(String name, Class<?> param) {
  59         try {
  60             return ObjectCloneSnippets.class.getDeclaredMethod(name, param);
  61         } catch (SecurityException | NoSuchMethodException e) {
  62             throw new GraalError(e);
  63         }
  64     }
  65 
  66     @Snippet
  67     public static boolean[] booleanArrayClone(boolean[] src) {
  68         boolean[] result = (boolean[]) NewArrayNode.newUninitializedArray(Boolean.TYPE, src.length);
  69         ArrayCopyCallNode.disjointArraycopy(src, 0, result, 0, src.length, JavaKind.Boolean, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG));
  70         return result;
  71     }
  72 
  73     @Snippet
  74     public static byte[] byteArrayClone(byte[] src) {
  75         byte[] result = (byte[]) NewArrayNode.newUninitializedArray(Byte.TYPE, src.length);
  76         ArrayCopyCallNode.disjointArraycopy(src, 0, result, 0, src.length, JavaKind.Byte, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG));
  77         return result;
  78     }
  79 
  80     @Snippet
  81     public static short[] shortArrayClone(short[] src) {
  82         short[] result = (short[]) NewArrayNode.newUninitializedArray(Short.TYPE, src.length);
  83         ArrayCopyCallNode.disjointArraycopy(src, 0, result, 0, src.length, JavaKind.Short, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG));
  84         return result;
  85     }
  86 
  87     @Snippet
  88     public static char[] charArrayClone(char[] src) {
  89         char[] result = (char[]) NewArrayNode.newUninitializedArray(Character.TYPE, src.length);
  90         ArrayCopyCallNode.disjointArraycopy(src, 0, result, 0, src.length, JavaKind.Char, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG));
  91         return result;
  92     }
  93 
  94     @Snippet
  95     public static int[] intArrayClone(int[] src) {
  96         int[] result = (int[]) NewArrayNode.newUninitializedArray(Integer.TYPE, src.length);
  97         ArrayCopyCallNode.disjointArraycopy(src, 0, result, 0, src.length, JavaKind.Int, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG));
  98         return result;
  99     }
 100 
 101     @Snippet
 102     public static float[] floatArrayClone(float[] src) {
 103         float[] result = (float[]) NewArrayNode.newUninitializedArray(Float.TYPE, src.length);
 104         ArrayCopyCallNode.disjointArraycopy(src, 0, result, 0, src.length, JavaKind.Float, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG));
 105         return result;
 106     }
 107 
 108     @Snippet
 109     public static long[] longArrayClone(long[] src) {
 110         long[] result = (long[]) NewArrayNode.newUninitializedArray(Long.TYPE, src.length);
 111         ArrayCopyCallNode.disjointArraycopy(src, 0, result, 0, src.length, JavaKind.Long, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG));
 112         return result;
 113     }
 114 
 115     @Snippet
 116     public static double[] doubleArrayClone(double[] src) {
 117         double[] result = (double[]) NewArrayNode.newUninitializedArray(Double.TYPE, src.length);
 118         ArrayCopyCallNode.disjointArraycopy(src, 0, result, 0, src.length, JavaKind.Double, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG));
 119         return result;
 120     }
 121 
 122     @Snippet
 123     public static Object[] objectArrayClone(Object[] src) {
 124         /* Since this snippet is lowered early the array must be initialized */
 125         Object[] result = (Object[]) DynamicNewArrayNode.newArray(GraalDirectives.guardingNonNull(src.getClass().getComponentType()), src.length, JavaKind.Object);
 126         ArrayCopyCallNode.disjointUninitializedArraycopy(src, 0, result, 0, src.length, JavaKind.Object, HotSpotReplacementsUtil.getHeapWordSize(INJECTED_VMCONFIG));
 127         return result;
 128     }
 129 }