Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
1.7 KiB

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