comparison graal/com.oracle.max.asm/src/com/oracle/max/asm/Buffer.java @ 3733:e233f5660da4

Added Java files from Maxine project.
author Thomas Wuerthinger <thomas.wuerthinger@oracle.com>
date Sat, 17 Dec 2011 19:59:18 +0100
parents
children bc8527f3071c
comparison
equal deleted inserted replaced
3732:3e2e8b8abdaf 3733:e233f5660da4
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 ensureSize(pos + 1);
116 data[pos++] = (byte) (b & 0xFF);
117 return pos;
118 }
119
120 public abstract int emitShort(int b, int pos);
121
122 public abstract int emitInt(int b, int pos);
123
124 public abstract int emitLong(long b, int pos);
125
126 public int getByte(int pos) {
127 return data[pos] & 0xff;
128 }
129
130 public abstract int getShort(int pos);
131
132 public abstract int getInt(int pos);
133
134 public static final class BigEndian extends Buffer {
135 @Override
136 public int emitShort(int b, int pos) {
137 assert NumUtil.isUShort(b);
138 ensureSize(pos + 2);
139 data[pos++] = (byte) ((b >> 8) & 0xFF);
140 data[pos++] = (byte) (b & 0xFF);
141 return pos;
142 }
143
144 @Override
145 public int emitInt(int b, int pos) {
146 ensureSize(pos + 4);
147 data[pos++] = (byte) ((b >> 24) & 0xFF);
148 data[pos++] = (byte) ((b >> 16) & 0xFF);
149 data[pos++] = (byte) ((b >> 8) & 0xFF);
150 data[pos++] = (byte) (b & 0xFF);
151 return pos;
152 }
153
154 @Override
155 public int emitLong(long b, int pos) {
156 ensureSize(pos + 8);
157 data[pos++] = (byte) ((b >> 56) & 0xFF);
158 data[pos++] = (byte) ((b >> 48) & 0xFF);
159 data[pos++] = (byte) ((b >> 40) & 0xFF);
160 data[pos++] = (byte) ((b >> 32) & 0xFF);
161 data[pos++] = (byte) ((b >> 24) & 0xFF);
162 data[pos++] = (byte) ((b >> 16) & 0xFF);
163 data[pos++] = (byte) ((b >> 8) & 0xFF);
164 data[pos++] = (byte) (b & 0xFF);
165 return pos;
166 }
167
168 @Override
169 public int getShort(int pos) {
170 return
171 (data[pos + 0] & 0xff) << 8 |
172 (data[pos + 1] & 0xff) << 0;
173 }
174
175 @Override
176 public int getInt(int pos) {
177 return
178 (data[pos + 0] & 0xff) << 24 |
179 (data[pos + 1] & 0xff) << 16 |
180 (data[pos + 2] & 0xff) << 8 |
181 (data[pos + 3] & 0xff) << 0;
182 }
183 }
184
185 public static final class LittleEndian extends Buffer {
186 @Override
187 public int emitShort(int b, int pos) {
188 assert NumUtil.isUShort(b);
189 ensureSize(pos + 2);
190 data[pos++] = (byte) (b & 0xFF);
191 data[pos++] = (byte) ((b >> 8) & 0xFF);
192 return pos;
193 }
194
195 @Override
196 public int emitInt(int b, int pos) {
197 ensureSize(pos + 4);
198 data[pos++] = (byte) (b & 0xFF);
199 data[pos++] = (byte) ((b >> 8) & 0xFF);
200 data[pos++] = (byte) ((b >> 16) & 0xFF);
201 data[pos++] = (byte) ((b >> 24) & 0xFF);
202 return pos;
203 }
204
205 @Override
206 public int emitLong(long b, int pos) {
207 ensureSize(pos + 8);
208 data[pos++] = (byte) (b & 0xFF);
209 data[pos++] = (byte) ((b >> 8) & 0xFF);
210 data[pos++] = (byte) ((b >> 16) & 0xFF);
211 data[pos++] = (byte) ((b >> 24) & 0xFF);
212 data[pos++] = (byte) ((b >> 32) & 0xFF);
213 data[pos++] = (byte) ((b >> 40) & 0xFF);
214 data[pos++] = (byte) ((b >> 48) & 0xFF);
215 data[pos++] = (byte) ((b >> 56) & 0xFF);
216 return pos;
217 }
218
219 @Override
220 public int getShort(int pos) {
221 return
222 (data[pos + 1] & 0xff) << 8 |
223 (data[pos + 0] & 0xff) << 0;
224 }
225
226 @Override
227 public int getInt(int pos) {
228 return
229 (data[pos + 3] & 0xff) << 24 |
230 (data[pos + 2] & 0xff) << 16 |
231 (data[pos + 1] & 0xff) << 8 |
232 (data[pos + 0] & 0xff) << 0;
233 }
234 }
235 }