| 1 |
// BranchIA64.c |
| 2 |
|
| 3 |
#include "BranchIA64.h" |
| 4 |
|
| 5 |
const Byte kBranchTable[32] = |
| 6 |
{ |
| 7 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 8 |
0, 0, 0, 0, 0, 0, 0, 0, |
| 9 |
4, 4, 6, 6, 0, 0, 7, 7, |
| 10 |
4, 4, 0, 0, 4, 4, 0, 0 |
| 11 |
}; |
| 12 |
|
| 13 |
UInt32 IA64_Convert(Byte *data, UInt32 size, UInt32 nowPos, int encoding) |
| 14 |
{ |
| 15 |
UInt32 i; |
| 16 |
for (i = 0; i + 16 <= size; i += 16) |
| 17 |
{ |
| 18 |
UInt32 instrTemplate = data[i] & 0x1F; |
| 19 |
UInt32 mask = kBranchTable[instrTemplate]; |
| 20 |
UInt32 bitPos = 5; |
| 21 |
for (int slot = 0; slot < 3; slot++, bitPos += 41) |
| 22 |
{ |
| 23 |
if (((mask >> slot) & 1) == 0) |
| 24 |
continue; |
| 25 |
UInt32 bytePos = (bitPos >> 3); |
| 26 |
UInt32 bitRes = bitPos & 0x7; |
| 27 |
// UInt64 instruction = *(UInt64 *)(data + i + bytePos); |
| 28 |
UInt64 instruction = 0; |
| 29 |
int j; |
| 30 |
for (j = 0; j < 6; j++) |
| 31 |
instruction += (UInt64)(data[i + j + bytePos]) << (8 * j); |
| 32 |
|
| 33 |
UInt64 instNorm = instruction >> bitRes; |
| 34 |
if (((instNorm >> 37) & 0xF) == 0x5 |
| 35 |
&& ((instNorm >> 9) & 0x7) == 0 |
| 36 |
// && (instNorm & 0x3F)== 0 |
| 37 |
) |
| 38 |
{ |
| 39 |
UInt32 src = UInt32((instNorm >> 13) & 0xFFFFF); |
| 40 |
src |= ((instNorm >> 36) & 1) << 20; |
| 41 |
|
| 42 |
src <<= 4; |
| 43 |
|
| 44 |
UInt32 dest; |
| 45 |
if (encoding) |
| 46 |
dest = nowPos + i + src; |
| 47 |
else |
| 48 |
dest = src - (nowPos + i); |
| 49 |
|
| 50 |
dest >>= 4; |
| 51 |
|
| 52 |
instNorm &= ~(UInt64(0x8FFFFF) << 13); |
| 53 |
instNorm |= (UInt64(dest & 0xFFFFF) << 13); |
| 54 |
instNorm |= (UInt64(dest & 0x100000) << (36 - 20)); |
| 55 |
|
| 56 |
instruction &= (1 << bitRes) - 1; |
| 57 |
instruction |= (instNorm << bitRes); |
| 58 |
// *(UInt64 *)(data + i + bytePos) = instruction; |
| 59 |
for (j = 0; j < 6; j++) |
| 60 |
data[i + j + bytePos] = Byte(instruction >> (8 * j)); |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
return i; |
| 65 |
} |