Mercurial > hg > graal-jvmci-8
comparison graal/Compiler/src/com/sun/c1x/asm/Buffer.java @ 2507:9ec15d6914ca
Pull over of compiler from maxine repository.
author | Thomas Wuerthinger <thomas@wuerthinger.net> |
---|---|
date | Wed, 27 Apr 2011 11:43:22 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2506:4a3bf8a5bf41 | 2507:9ec15d6914ca |
---|---|
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.sun.c1x.asm; | |
24 | |
25 import java.util.*; | |
26 | |
27 import com.sun.c1x.*; | |
28 import com.sun.cri.bytecode.*; | |
29 import com.sun.cri.ci.CiArchitecture.ByteOrder; | |
30 | |
31 /** | |
32 * | |
33 * @author Thomas Wuerthinger | |
34 */ | |
35 public final class Buffer { | |
36 | |
37 private byte[] data; | |
38 private int position; | |
39 private int mark = -1; | |
40 | |
41 private final ByteOrder byteOrder; | |
42 | |
43 public Buffer(ByteOrder byteOrder) { | |
44 this.byteOrder = byteOrder; | |
45 this.data = new byte[C1XOptions.InitialCodeBufferSize]; | |
46 } | |
47 | |
48 public void reset() { | |
49 position = 0; | |
50 mark = -1; | |
51 } | |
52 | |
53 /** | |
54 * Closes this buffer. No extra data can be written to this buffer after this call. | |
55 * | |
56 * @param trimmedCopy if {@code true}, then a copy of the underlying byte array up to (but not including) | |
57 * {@code position()} is returned | |
58 * @return the data in this buffer or a trimmed copy if {@code trimmedCopy} is {@code true} | |
59 */ | |
60 public byte[] close(boolean trimmedCopy) { | |
61 byte[] result = trimmedCopy ? Arrays.copyOf(data, position()) : data; | |
62 data = null; | |
63 return result; | |
64 } | |
65 | |
66 public int emitBytes(byte[] arr, int off, int len) { | |
67 assert data != null : "must not use buffer after calling finished!"; | |
68 int oldPos = position; | |
69 ensureSize(position + len); | |
70 System.arraycopy(arr, off, data, position, len); | |
71 position += len; | |
72 return oldPos; | |
73 } | |
74 | |
75 public int emitByte(int b) { | |
76 int oldPos = position; | |
77 position = emitByte(b, oldPos); | |
78 return oldPos; | |
79 } | |
80 | |
81 public int emitShort(int b) { | |
82 int oldPos = position; | |
83 position = emitShort(b, oldPos); | |
84 return oldPos; | |
85 } | |
86 | |
87 public int emitInt(int b) { | |
88 int oldPos = position; | |
89 position = emitInt(b, oldPos); | |
90 return oldPos; | |
91 } | |
92 | |
93 public int emitLong(long b) { | |
94 int oldPos = position; | |
95 position = emitLong(b, oldPos); | |
96 return oldPos; | |
97 } | |
98 | |
99 private boolean isByte(int b) { | |
100 return b == (b & 0xFF); | |
101 } | |
102 | |
103 private boolean isShort(int s) { | |
104 return s == (s & 0xFFFF); | |
105 } | |
106 | |
107 /** | |
108 * Places a bookmark at the {@linkplain #position() current position}. | |
109 * | |
110 * @return the previously placed bookmark or {@code -1} if there was no bookmark | |
111 */ | |
112 public int mark() { | |
113 int mark = this.mark; | |
114 this.mark = position; | |
115 return mark; | |
116 } | |
117 | |
118 private void ensureSize(int length) { | |
119 if (length >= data.length) { | |
120 data = Arrays.copyOf(data, data.length * 4); | |
121 C1XMetrics.CodeBufferCopies++; | |
122 } | |
123 } | |
124 | |
125 public int emitByte(int b, int pos) { | |
126 assert data != null : "must not use buffer after calling finished!"; | |
127 assert isByte(b); | |
128 ensureSize(pos + 1); | |
129 data[pos++] = (byte) b; | |
130 return pos; | |
131 } | |
132 | |
133 public int emitShort(int b, int pos) { | |
134 assert data != null : "must not use buffer after calling finished!"; | |
135 assert isShort(b); | |
136 ensureSize(pos + 2); | |
137 if (byteOrder == ByteOrder.BigEndian) { | |
138 data[pos++] = (byte) ((b >> 8) & 0xFF); | |
139 data[pos++] = (byte) (b & 0xFF); | |
140 | |
141 } else { | |
142 assert byteOrder == ByteOrder.LittleEndian; | |
143 data[pos++] = (byte) (b & 0xFF); | |
144 data[pos++] = (byte) ((b >> 8) & 0xFF); | |
145 } | |
146 return pos; | |
147 } | |
148 | |
149 public int emitInt(int b, int pos) { | |
150 assert data != null : "must not use buffer after calling finished!"; | |
151 ensureSize(pos + 4); | |
152 if (byteOrder == ByteOrder.BigEndian) { | |
153 data[pos++] = (byte) ((b >> 24) & 0xFF); | |
154 data[pos++] = (byte) ((b >> 16) & 0xFF); | |
155 data[pos++] = (byte) ((b >> 8) & 0xFF); | |
156 data[pos++] = (byte) (b & 0xFF); | |
157 } else { | |
158 assert byteOrder == ByteOrder.LittleEndian; | |
159 data[pos++] = (byte) (b & 0xFF); | |
160 data[pos++] = (byte) ((b >> 8) & 0xFF); | |
161 data[pos++] = (byte) ((b >> 16) & 0xFF); | |
162 data[pos++] = (byte) ((b >> 24) & 0xFF); | |
163 } | |
164 return pos; | |
165 } | |
166 | |
167 public int emitLong(long b, int pos) { | |
168 assert data != null : "must not use buffer after calling finished!"; | |
169 ensureSize(pos + 8); | |
170 | |
171 if (byteOrder == ByteOrder.BigEndian) { | |
172 data[pos++] = (byte) ((b >> 56) & 0xFF); | |
173 data[pos++] = (byte) ((b >> 48) & 0xFF); | |
174 data[pos++] = (byte) ((b >> 40) & 0xFF); | |
175 data[pos++] = (byte) ((b >> 32) & 0xFF); | |
176 data[pos++] = (byte) ((b >> 24) & 0xFF); | |
177 data[pos++] = (byte) ((b >> 16) & 0xFF); | |
178 data[pos++] = (byte) ((b >> 8) & 0xFF); | |
179 data[pos++] = (byte) (b & 0xFF); | |
180 } else { | |
181 assert byteOrder == ByteOrder.LittleEndian; | |
182 data[pos++] = (byte) (b & 0xFF); | |
183 data[pos++] = (byte) ((b >> 8) & 0xFF); | |
184 data[pos++] = (byte) ((b >> 16) & 0xFF); | |
185 data[pos++] = (byte) ((b >> 24) & 0xFF); | |
186 data[pos++] = (byte) ((b >> 32) & 0xFF); | |
187 data[pos++] = (byte) ((b >> 40) & 0xFF); | |
188 data[pos++] = (byte) ((b >> 48) & 0xFF); | |
189 data[pos++] = (byte) ((b >> 56) & 0xFF); | |
190 } | |
191 return pos; | |
192 } | |
193 | |
194 public int position() { | |
195 return position; | |
196 } | |
197 | |
198 public void setPosition(int position) { | |
199 assert position >= 0 && position <= data.length; | |
200 this.position = position; | |
201 } | |
202 | |
203 public int getByte(int pos) { | |
204 return Bytes.beU1(data, pos); | |
205 } | |
206 | |
207 public int getShort(int pos) { | |
208 if (byteOrder == ByteOrder.BigEndian) { | |
209 return | |
210 (data[pos + 0] & 0xff) << 8 | | |
211 (data[pos + 1] & 0xff) << 0; | |
212 } else { | |
213 assert byteOrder == ByteOrder.LittleEndian; | |
214 return | |
215 (data[pos + 1] & 0xff) << 8 | | |
216 (data[pos + 0] & 0xff) << 0; | |
217 } | |
218 } | |
219 | |
220 public int getInt(int pos) { | |
221 if (byteOrder == ByteOrder.BigEndian) { | |
222 return | |
223 (data[pos + 0] & 0xff) << 24 | | |
224 (data[pos + 1] & 0xff) << 16 | | |
225 (data[pos + 2] & 0xff) << 8 | | |
226 (data[pos + 3] & 0xff) << 0; | |
227 } else { | |
228 assert byteOrder == ByteOrder.LittleEndian; | |
229 return | |
230 (data[pos + 3] & 0xff) << 24 | | |
231 (data[pos + 2] & 0xff) << 16 | | |
232 (data[pos + 1] & 0xff) << 8 | | |
233 (data[pos + 0] & 0xff) << 0; | |
234 } | |
235 } | |
236 | |
237 public byte[] copyData(int start, int end) { | |
238 return Arrays.copyOfRange(data, start, end); | |
239 } | |
240 | |
241 /** | |
242 * Copies the data from this buffer into a given array. | |
243 * | |
244 * @param dst the destination array | |
245 * @param off starting position in {@code dst} | |
246 * @param len number of bytes to copy | |
247 */ | |
248 public void copyInto(byte[] dst, int off, int len) { | |
249 System.arraycopy(data, 0, dst, off, len); | |
250 } | |
251 } |