disp8.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /* ----------------------------------------------------------------------- *
  2. *
  3. * Copyright 1996-2013 The NASM Authors - All Rights Reserved
  4. * See the file AUTHORS included with the NASM distribution for
  5. * the specific copyright holders.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following
  9. * conditions are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. * ----------------------------------------------------------------------- */
  33. /*
  34. * disp8.c : Contains a common logic for EVEX compressed displacement
  35. */
  36. #include "disp8.h"
  37. /*
  38. * Find N value for compressed displacement (disp8 * N)
  39. */
  40. uint8_t get_disp8N(insn *ins)
  41. {
  42. static const uint8_t fv_n[2][2][VLMAX] = {{{16, 32, 64}, {4, 4, 4}},
  43. {{16, 32, 64}, {8, 8, 8}}};
  44. static const uint8_t hv_n[2][VLMAX] = {{8, 16, 32}, {4, 4, 4}};
  45. static const uint8_t dup_n[VLMAX] = {8, 32, 64};
  46. bool evex_b = (ins->evex_p[2] & EVEX_P2B) >> 4;
  47. enum ttypes tuple = ins->evex_tuple;
  48. enum vectlens vectlen = (ins->evex_p[2] & EVEX_P2LL) >> 5;
  49. bool evex_w = (ins->evex_p[1] & EVEX_P1W) >> 7;
  50. uint8_t n = 0;
  51. switch(tuple) {
  52. case FV:
  53. n = fv_n[evex_w][evex_b][vectlen];
  54. break;
  55. case HV:
  56. n = hv_n[evex_b][vectlen];
  57. break;
  58. case FVM:
  59. /* 16, 32, 64 for VL 128, 256, 512 respectively*/
  60. n = 1 << (vectlen + 4);
  61. break;
  62. case T1S8: /* N = 1 */
  63. case T1S16: /* N = 2 */
  64. n = tuple - T1S8 + 1;
  65. break;
  66. case T1S:
  67. /* N = 4 for 32bit, 8 for 64bit */
  68. n = evex_w ? 8 : 4;
  69. break;
  70. case T1F32:
  71. case T1F64:
  72. /* N = 4 for 32bit, 8 for 64bit */
  73. n = (tuple == T1F32 ? 4 : 8);
  74. break;
  75. case T2:
  76. case T4:
  77. case T8:
  78. if (vectlen + 7 <= (evex_w + 5) + (tuple - T2 + 1))
  79. n = 0;
  80. else
  81. n = 1 << (tuple - T2 + evex_w + 3);
  82. break;
  83. case HVM:
  84. case QVM:
  85. case OVM:
  86. n = 1 << (OVM - tuple + vectlen + 1);
  87. break;
  88. case M128:
  89. n = 16;
  90. break;
  91. case DUP:
  92. n = dup_n[vectlen];
  93. break;
  94. default:
  95. break;
  96. }
  97. return n;
  98. }
  99. /*
  100. * Check if offset is a multiple of N with corresponding tuple type
  101. * if Disp8*N is available, compressed displacement is stored in compdisp
  102. */
  103. bool is_disp8n(operand *input, insn *ins, int8_t *compdisp)
  104. {
  105. int32_t off = input->offset;
  106. uint8_t n;
  107. int32_t disp8;
  108. n = get_disp8N(ins);
  109. if (n && !(off & (n - 1))) {
  110. disp8 = off / n;
  111. /* if it fits in Disp8 */
  112. if (disp8 >= -128 && disp8 <= 127) {
  113. *compdisp = disp8;
  114. return true;
  115. }
  116. }
  117. *compdisp = 0;
  118. return false;
  119. }