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