1 /*
   2  * Copyright (c) 2016, 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.lir.asm;
  24 
  25 import java.nio.ByteBuffer;
  26 import java.nio.ByteOrder;
  27 import java.util.Arrays;
  28 
  29 import org.graalvm.compiler.core.common.type.DataPointerConstant;
  30 
  31 /**
  32  * Class for chunks of data that go into the data section.
  33  */
  34 public class ArrayDataPointerConstant extends DataPointerConstant {
  35 
  36     private final byte[] data;
  37 
  38     public ArrayDataPointerConstant(byte[] array, int alignment) {
  39         super(alignment);
  40         data = array.clone();
  41     }
  42 
  43     public ArrayDataPointerConstant(short[] array, int alignment) {
  44         super(alignment);
  45         ByteBuffer byteBuffer = ByteBuffer.allocate(array.length * 2);
  46         byteBuffer.order(ByteOrder.nativeOrder());
  47         byteBuffer.asShortBuffer().put(array);
  48         data = byteBuffer.array();
  49     }
  50 
  51     public ArrayDataPointerConstant(int[] array, int alignment) {
  52         super(alignment);
  53         ByteBuffer byteBuffer = ByteBuffer.allocate(array.length * 4);
  54         byteBuffer.order(ByteOrder.nativeOrder());
  55         byteBuffer.asIntBuffer().put(array);
  56         data = byteBuffer.array();
  57     }
  58 
  59     public ArrayDataPointerConstant(float[] array, int alignment) {
  60         super(alignment);
  61         ByteBuffer byteBuffer = ByteBuffer.allocate(array.length * 4);
  62         byteBuffer.order(ByteOrder.nativeOrder());
  63         byteBuffer.asFloatBuffer().put(array);
  64         data = byteBuffer.array();
  65     }
  66 
  67     public ArrayDataPointerConstant(double[] array, int alignment) {
  68         super(alignment);
  69         ByteBuffer byteBuffer = ByteBuffer.allocate(array.length * 8);
  70         byteBuffer.order(ByteOrder.nativeOrder());
  71         byteBuffer.asDoubleBuffer().put(array);
  72         data = byteBuffer.array();
  73     }
  74 
  75     public ArrayDataPointerConstant(long[] array, int alignment) {
  76         super(alignment);
  77         ByteBuffer byteBuffer = ByteBuffer.allocate(array.length * 8);
  78         byteBuffer.order(ByteOrder.nativeOrder());
  79         byteBuffer.asLongBuffer().put(array);
  80         data = byteBuffer.array();
  81     }
  82 
  83     @Override
  84     public boolean isDefaultForKind() {
  85         return false;
  86     }
  87 
  88     @Override
  89     public void serialize(ByteBuffer buffer) {
  90         buffer.put(data);
  91     }
  92 
  93     @Override
  94     public int getSerializedSize() {
  95         return data.length;
  96     }
  97 
  98     @Override
  99     public String toValueString() {
 100         return "ArrayDataPointerConstant" + Arrays.toString(data);
 101     }
 102 }