BranchIA64.c 1.7 KB

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