Mercurial > hg > graal-compiler
comparison graal/com.oracle.jvmci.asm/src/com/oracle/jvmci/asm/Buffer.java @ 21708:6df25b1418be
moved com.oracle.asm.** to jvmci-util.jar (JBS:GRAAL-53)
author | Doug Simon <doug.simon@oracle.com> |
---|---|
date | Wed, 03 Jun 2015 18:06:44 +0200 |
parents | graal/com.oracle.graal.asm/src/com/oracle/graal/asm/Buffer.java@5a7b82c1514e |
children |
comparison
equal
deleted
inserted
replaced
21707:e0f311284930 | 21708:6df25b1418be |
---|---|
1 /* | |
2 * Copyright (c) 2009, 2014, 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 com.oracle.jvmci.asm; | |
24 | |
25 import java.util.*; | |
26 | |
27 /** | |
28 * Code buffer management for the assembler. Support for little endian and big endian architectures | |
29 * is implemented using subclasses. | |
30 */ | |
31 abstract class Buffer { | |
32 | |
33 protected byte[] data; | |
34 protected int position; | |
35 | |
36 public Buffer() { | |
37 data = new byte[AsmOptions.InitialCodeBufferSize]; | |
38 } | |
39 | |
40 public int position() { | |
41 return position; | |
42 } | |
43 | |
44 public void setPosition(int position) { | |
45 assert position >= 0 && position <= data.length; | |
46 this.position = position; | |
47 } | |
48 | |
49 /** | |
50 * Closes this buffer. No extra data can be written to this buffer after this call. | |
51 * | |
52 * @param trimmedCopy if {@code true}, then a copy of the underlying byte array up to (but not | |
53 * including) {@code position()} is returned | |
54 * @return the data in this buffer or a trimmed copy if {@code trimmedCopy} is {@code true} | |
55 */ | |
56 public byte[] close(boolean trimmedCopy) { | |
57 byte[] result = trimmedCopy ? Arrays.copyOf(data, position()) : data; | |
58 data = null; | |
59 return result; | |
60 } | |
61 | |
62 public byte[] copyData(int start, int end) { | |
63 if (data == null) { | |
64 return null; | |
65 } | |
66 return Arrays.copyOfRange(data, start, end); | |
67 } | |
68 | |
69 /** | |
70 * Copies the data from this buffer into a given array. | |
71 * | |
72 * @param dst the destination array | |
73 * @param off starting position in {@code dst} | |
74 * @param len number of bytes to copy | |
75 */ | |
76 public void copyInto(byte[] dst, int off, int len) { | |
77 System.arraycopy(data, 0, dst, off, len); | |
78 } | |
79 | |
80 protected void ensureSize(int length) { | |
81 if (length >= data.length) { | |
82 data = Arrays.copyOf(data, length * 4); | |
83 } | |
84 } | |
85 | |
86 public void emitBytes(byte[] arr, int off, int len) { | |
87 ensureSize(position + len); | |
88 System.arraycopy(arr, off, data, position, len); | |
89 position += len; | |
90 } | |
91 | |
92 public void emitByte(int b) { | |
93 position = emitByte(b, position); | |
94 } | |
95 | |
96 public void emitShort(int b) { | |
97 position = emitShort(b, position); | |
98 } | |
99 | |
100 public void emitInt(int b) { | |
101 position = emitInt(b, position); | |
102 } | |
103 | |
104 public void emitLong(long b) { | |
105 position = emitLong(b, position); | |
106 } | |
107 | |
108 public int emitBytes(byte[] arr, int pos) { | |
109 final int len = arr.length; | |
110 final int newPos = pos + len; | |
111 ensureSize(newPos); | |
112 System.arraycopy(arr, 0, data, pos, len); | |
113 return newPos; | |
114 } | |
115 | |
116 public int emitByte(int b, int pos) { | |
117 assert NumUtil.isUByte(b) || NumUtil.isByte(b); | |
118 int newPos = pos + 1; | |
119 ensureSize(newPos); | |
120 data[pos] = (byte) (b & 0xFF); | |
121 return newPos; | |
122 } | |
123 | |
124 public abstract int emitShort(int b, int pos); | |
125 | |
126 public abstract int emitInt(int b, int pos); | |
127 | |
128 public abstract int emitLong(long b, int pos); | |
129 | |
130 public int getByte(int pos) { | |
131 return data[pos] & 0xff; | |
132 } | |
133 | |
134 public abstract int getShort(int pos); | |
135 | |
136 public abstract int getInt(int pos); | |
137 | |
138 public static final class BigEndian extends Buffer { | |
139 | |
140 @Override | |
141 public int emitShort(int b, int pos) { | |
142 assert NumUtil.isUShort(b) || NumUtil.isShort(b); | |
143 int newPos = pos + 2; | |
144 ensureSize(pos + 2); | |
145 data[pos] = (byte) ((b >> 8) & 0xFF); | |
146 data[pos + 1] = (byte) (b & 0xFF); | |
147 return newPos; | |
148 } | |
149 | |
150 @Override | |
151 public int emitInt(int b, int pos) { | |
152 int newPos = pos + 4; | |
153 ensureSize(newPos); | |
154 data[pos] = (byte) ((b >> 24) & 0xFF); | |
155 data[pos + 1] = (byte) ((b >> 16) & 0xFF); | |
156 data[pos + 2] = (byte) ((b >> 8) & 0xFF); | |
157 data[pos + 3] = (byte) (b & 0xFF); | |
158 return newPos; | |
159 } | |
160 | |
161 @Override | |
162 public int emitLong(long b, int pos) { | |
163 int newPos = pos + 8; | |
164 ensureSize(newPos); | |
165 data[pos] = (byte) ((b >> 56) & 0xFF); | |
166 data[pos + 1] = (byte) ((b >> 48) & 0xFF); | |
167 data[pos + 2] = (byte) ((b >> 40) & 0xFF); | |
168 data[pos + 3] = (byte) ((b >> 32) & 0xFF); | |
169 data[pos + 4] = (byte) ((b >> 24) & 0xFF); | |
170 data[pos + 5] = (byte) ((b >> 16) & 0xFF); | |
171 data[pos + 6] = (byte) ((b >> 8) & 0xFF); | |
172 data[pos + 7] = (byte) (b & 0xFF); | |
173 return newPos; | |
174 } | |
175 | |
176 @Override | |
177 public int getShort(int pos) { | |
178 return (data[pos + 0] & 0xff) << 8 | (data[pos + 1] & 0xff) << 0; | |
179 } | |
180 | |
181 @Override | |
182 public int getInt(int pos) { | |
183 return (data[pos + 0] & 0xff) << 24 | (data[pos + 1] & 0xff) << 16 | (data[pos + 2] & 0xff) << 8 | (data[pos + 3] & 0xff) << 0; | |
184 } | |
185 } | |
186 | |
187 public static final class LittleEndian extends Buffer { | |
188 | |
189 @Override | |
190 public int emitShort(int b, int pos) { | |
191 assert NumUtil.isUShort(b) || NumUtil.isShort(b); | |
192 int newPos = pos + 2; | |
193 ensureSize(newPos); | |
194 data[pos] = (byte) (b & 0xFF); | |
195 data[pos + 1] = (byte) ((b >> 8) & 0xFF); | |
196 return newPos; | |
197 } | |
198 | |
199 @Override | |
200 public int emitInt(int b, int pos) { | |
201 int newPos = pos + 4; | |
202 ensureSize(newPos); | |
203 data[pos] = (byte) (b & 0xFF); | |
204 data[pos + 1] = (byte) ((b >> 8) & 0xFF); | |
205 data[pos + 2] = (byte) ((b >> 16) & 0xFF); | |
206 data[pos + 3] = (byte) ((b >> 24) & 0xFF); | |
207 return newPos; | |
208 } | |
209 | |
210 @Override | |
211 public int emitLong(long b, int pos) { | |
212 int newPos = pos + 8; | |
213 ensureSize(newPos); | |
214 data[pos] = (byte) (b & 0xFF); | |
215 data[pos + 1] = (byte) ((b >> 8) & 0xFF); | |
216 data[pos + 2] = (byte) ((b >> 16) & 0xFF); | |
217 data[pos + 3] = (byte) ((b >> 24) & 0xFF); | |
218 data[pos + 4] = (byte) ((b >> 32) & 0xFF); | |
219 data[pos + 5] = (byte) ((b >> 40) & 0xFF); | |
220 data[pos + 6] = (byte) ((b >> 48) & 0xFF); | |
221 data[pos + 7] = (byte) ((b >> 56) & 0xFF); | |
222 return newPos; | |
223 } | |
224 | |
225 @Override | |
226 public int getShort(int pos) { | |
227 return (data[pos + 1] & 0xff) << 8 | (data[pos + 0] & 0xff) << 0; | |
228 } | |
229 | |
230 @Override | |
231 public int getInt(int pos) { | |
232 return (data[pos + 3] & 0xff) << 24 | (data[pos + 2] & 0xff) << 16 | (data[pos + 1] & 0xff) << 8 | (data[pos + 0] & 0xff) << 0; | |
233 } | |
234 } | |
235 | |
236 public void reset() { | |
237 position = 0; | |
238 } | |
239 } |