BraIA64.c 1.8 KB

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