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