| 1 |
// |
| 2 |
// Copyright (C) 2010 Wayne Meissner |
| 3 |
// |
| 4 |
// Permission is hereby granted, free of charge, to any person |
| 5 |
// obtaining a copy of this software and associated documentation |
| 6 |
// files (the "Software"), to deal in the Software without |
| 7 |
// restriction, including without limitation the rights to use, |
| 8 |
// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
// copies of the Software, and to permit persons to whom the |
| 10 |
// Software is furnished to do so, subject to the following |
| 11 |
// conditions: |
| 12 |
// |
| 13 |
// The above copyright notice and this permission notice shall be |
| 14 |
// included in all copies or substantial portions of the Software. |
| 15 |
// |
| 16 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 |
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 |
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 |
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 |
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 21 |
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 22 |
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 23 |
// OTHER DEALINGS IN THE SOFTWARE. |
| 24 |
|
| 25 |
package com.kenai.jnr.x86asm; |
| 26 |
|
| 27 |
import java.nio.ByteBuffer; |
| 28 |
import java.nio.ByteOrder; |
| 29 |
|
| 30 |
/** |
| 31 |
* |
| 32 |
*/ |
| 33 |
@Deprecated |
| 34 |
final class CodeBuffer { |
| 35 |
private ByteBuffer buf; |
| 36 |
|
| 37 |
public CodeBuffer() { |
| 38 |
buf = ByteBuffer.allocate(128).order(ByteOrder.LITTLE_ENDIAN); |
| 39 |
} |
| 40 |
|
| 41 |
public final void ensureSpace() { |
| 42 |
if (buf.remaining() < 16) { |
| 43 |
grow(); |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
public void grow() { |
| 48 |
int newSize = buf.capacity() * 2; |
| 49 |
ByteBuffer newBuffer = ByteBuffer.allocate(newSize).order(ByteOrder.LITTLE_ENDIAN); |
| 50 |
|
| 51 |
// Copy the data over |
| 52 |
buf.flip(); |
| 53 |
newBuffer.put(buf); |
| 54 |
buf = newBuffer; |
| 55 |
} |
| 56 |
|
| 57 |
final void copyTo(ByteBuffer dst) { |
| 58 |
ByteBuffer dup = buf.duplicate(); |
| 59 |
dup.flip(); |
| 60 |
dst.put(dup); |
| 61 |
} |
| 62 |
|
| 63 |
public final int offset() { |
| 64 |
return buf.position(); |
| 65 |
} |
| 66 |
|
| 67 |
public int capacity() { |
| 68 |
return buf.capacity(); |
| 69 |
} |
| 70 |
|
| 71 |
public final void emitByte(byte x) { |
| 72 |
buf.put(x); |
| 73 |
} |
| 74 |
|
| 75 |
public final void emitWord(short x) { |
| 76 |
buf.putShort(x); |
| 77 |
} |
| 78 |
|
| 79 |
public final void emitDWord(int x) { |
| 80 |
buf.putInt(x); |
| 81 |
} |
| 82 |
|
| 83 |
public final void emitQWord(long x) { |
| 84 |
buf.putLong(x); |
| 85 |
} |
| 86 |
|
| 87 |
public final void emitData(ByteBuffer data, int len) { |
| 88 |
|
| 89 |
ByteBuffer dup = data.duplicate(); |
| 90 |
if (dup.remaining() > len) { |
| 91 |
dup.limit(dup.position() + len); |
| 92 |
} |
| 93 |
|
| 94 |
buf.put(dup); |
| 95 |
} |
| 96 |
|
| 97 |
public final byte getByteAt(int pos) { |
| 98 |
return buf.get(pos); |
| 99 |
} |
| 100 |
|
| 101 |
public final short getWordAt(int pos) { |
| 102 |
return buf.getShort(pos); |
| 103 |
} |
| 104 |
|
| 105 |
public final int getDWordAt(int pos) { |
| 106 |
return buf.getInt(pos); |
| 107 |
} |
| 108 |
|
| 109 |
public final long getQWordAt(int pos) { |
| 110 |
return buf.getLong(pos); |
| 111 |
} |
| 112 |
|
| 113 |
public final void setByteAt(int pos, byte x) { |
| 114 |
buf.put(pos, x); |
| 115 |
} |
| 116 |
|
| 117 |
public final void setWordAt(int pos, short x) { |
| 118 |
buf.putShort(pos, x); |
| 119 |
} |
| 120 |
|
| 121 |
public final void setDWordAt(int pos, int x) { |
| 122 |
buf.putInt(pos, x); |
| 123 |
} |
| 124 |
|
| 125 |
public final void setQWordAt(int pos, long x) { |
| 126 |
buf.putLong(pos, x); |
| 127 |
} |
| 128 |
|
| 129 |
} |