1 /**************************************************************************
3 * Copyright 2013-2014 RAD Game Tools and Valve Software
4 * Copyright 2010-2014 Rich Geldreich and Tenacious Software LLC
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 **************************************************************************/
27 /* BraIA64.c -- Converter for IA-64 code
28 2008-10-04 : Igor Pavlov : Public domain */
29 #include "vogl_core.h"
35 static const Byte kBranchTable[32] =
37 0, 0, 0, 0, 0, 0, 0, 0,
38 0, 0, 0, 0, 0, 0, 0, 0,
39 4, 4, 6, 6, 0, 0, 7, 7,
40 4, 4, 0, 0, 4, 4, 0, 0
43 SizeT IA64_Convert(Byte *data, SizeT size, UInt32 ip, int encoding)
49 for (i = 0; i <= size; i += 16)
51 UInt32 instrTemplate = data[i] & 0x1F;
52 UInt32 mask = kBranchTable[instrTemplate];
55 for (slot = 0; slot < 3; slot++, bitPos += 41)
57 UInt32 bytePos, bitRes;
58 UInt64 instruction, instNorm;
60 if (((mask >> slot) & 1) == 0)
62 bytePos = (bitPos >> 3);
63 bitRes = bitPos & 0x7;
65 for (j = 0; j < 6; j++)
66 instruction += (UInt64)data[i + j + bytePos] << (8 * j);
68 instNorm = instruction >> bitRes;
69 if (((instNorm >> 37) & 0xF) == 0x5 && ((instNorm >> 9) & 0x7) == 0)
71 UInt32 src = (UInt32)((instNorm >> 13) & 0xFFFFF);
73 src |= ((UInt32)(instNorm >> 36) & 1) << 20;
78 dest = ip + (UInt32)i + src;
80 dest = src - (ip + (UInt32)i);
84 instNorm &= ~((UInt64)(0x8FFFFF) << 13);
85 instNorm |= ((UInt64)(dest & 0xFFFFF) << 13);
86 instNorm |= ((UInt64)(dest & 0x100000) << (36 - 20));
88 instruction &= (1 << bitRes) - 1;
89 instruction |= (instNorm << bitRes);
90 for (j = 0; j < 6; j++)
91 data[i + j + bytePos] = (Byte)(instruction >> (8 * j));