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