x86_operand_list.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <stdlib.h>
  2. #include <cassert>
  3. #include "libdis.h"
  4. void x86_insn_t::x86_oplist_append( x86_oplist_t *op ) {
  5. x86_oplist_t *list;
  6. assert(this);
  7. list = operands;
  8. if (! list ) {
  9. operand_count = 1;
  10. /* Note that we have no way of knowing if this is an
  11. * exlicit operand or not, since the caller fills
  12. * the x86_op_t after we return. We increase the
  13. * explicit count automatically, and ia32_insn_implicit_ops
  14. * decrements it */
  15. explicit_count = 1;
  16. operands = op;
  17. return;
  18. }
  19. /* get to end of list */
  20. for ( ; list->next; list = list->next )
  21. ;
  22. operand_count = operand_count + 1;
  23. explicit_count = explicit_count + 1;
  24. list->next = op;
  25. return;
  26. }
  27. bool x86_insn_t::containsFlag(x86_eflags tofind, x86_flag_status in)
  28. {
  29. switch(tofind)
  30. {
  31. case insn_eflag_carry:
  32. return (in & (insn_carry_set | insn_carry_or_zero_set | insn_carry_clear))!=0;
  33. case insn_eflag_zero:
  34. return (in & (insn_zero_set | insn_carry_or_zero_set |
  35. insn_zero_set_or_sign_ne_oflow | insn_zero_clear))!=0;
  36. case insn_eflag_overflow:
  37. return (in & (insn_oflow_set | insn_zero_set_or_sign_ne_oflow |
  38. insn_oflow_clear | insn_sign_eq_oflow |
  39. insn_sign_ne_oflow))!=0;
  40. case insn_eflag_direction:
  41. return (in & (insn_dir_set | insn_dir_clear))!=0;
  42. case insn_eflag_sign:
  43. return (in & (insn_sign_set | insn_sign_clear | insn_zero_set_or_sign_ne_oflow |
  44. insn_sign_eq_oflow | insn_sign_ne_oflow))!=0;
  45. case insn_eflag_parity:
  46. return (in & (insn_parity_set | insn_parity_clear))!=0;
  47. }
  48. return false;
  49. }
  50. x86_op_t * x86_insn_t::x86_operand_new( ) {
  51. x86_oplist_t *op;
  52. assert(this);
  53. op = (x86_oplist_t *)calloc( sizeof(x86_oplist_t), 1 );
  54. op->op.insn = this;
  55. x86_oplist_append( op );
  56. return( &(op->op) );
  57. }
  58. void x86_insn_t::x86_oplist_free( )
  59. {
  60. x86_oplist_t *op, *list;
  61. assert(this);
  62. for ( list = operands; list; ) {
  63. op = list;
  64. list = list->next;
  65. free(op);
  66. }
  67. operands = NULL;
  68. operand_count = 0;
  69. explicit_count = 0;
  70. return;
  71. }
  72. /* ================================================== LIBDISASM API */
  73. /* these could probably just be #defines, but that means exposing the
  74. enum... yet one more confusing thing in the API */
  75. int x86_insn_t::x86_operand_foreach( x86_operand_fn func, void *arg,
  76. enum x86_op_foreach_type type ){
  77. x86_oplist_t *list;
  78. char _explicit = 1, implicit = 1;
  79. assert(this);
  80. if ( ! func ) {
  81. return 0;
  82. }
  83. /* note: explicit and implicit can be ORed together to
  84. * allow an "all" limited by access type, even though the
  85. * user is stupid to do this since it is default behavior :) */
  86. if ( (type & op_explicit) && ! (type & op_implicit) ) {
  87. implicit = 0;
  88. }
  89. if ( (type & op_implicit) && ! (type & op_explicit) ) {
  90. _explicit = 0;
  91. }
  92. type = (x86_op_foreach_type)((int)type & 0x0F); /* mask out explicit/implicit operands */
  93. for ( list = operands; list; list = list->next ) {
  94. if (! implicit && (list->op.flags.op_implied) ) {
  95. /* operand is implicit */
  96. continue;
  97. }
  98. if (! _explicit && ! (list->op.flags.op_implied) ) {
  99. /* operand is not implicit */
  100. continue;
  101. }
  102. switch ( type ) {
  103. case op_any:
  104. break;
  105. case op_dest:
  106. if (! (list->op.access & op_write) ) {
  107. continue;
  108. }
  109. break;
  110. case op_src:
  111. if (! (list->op.access & op_read) ) {
  112. continue;
  113. }
  114. break;
  115. case op_ro:
  116. if (! (list->op.access & op_read) ||
  117. (list->op.access & op_write ) ) {
  118. continue;
  119. }
  120. break;
  121. case op_wo:
  122. if (! (list->op.access & op_write) ||
  123. (list->op.access & op_read ) ) {
  124. continue;
  125. }
  126. break;
  127. case op_xo:
  128. if (! (list->op.access & op_execute) ) {
  129. continue;
  130. }
  131. break;
  132. case op_rw:
  133. if (! (list->op.access & op_write) ||
  134. ! (list->op.access & op_read ) ) {
  135. continue;
  136. }
  137. break;
  138. case op_implicit: case op_explicit: /* make gcc happy */
  139. break;
  140. }
  141. /* any non-continue ends up here: invoke the callback */
  142. (*func)( &list->op, this, arg );
  143. }
  144. return 1;
  145. }
  146. static void count_operand( x86_op_t *op, x86_insn_t *insn, void *arg ) {
  147. size_t * count = (size_t *) arg;
  148. *count = *count + 1;
  149. }
  150. size_t x86_insn_t::x86_operand_count( enum x86_op_foreach_type type ) {
  151. size_t count = 0;
  152. /* save us a list traversal for common counts... */
  153. if ( type == op_any ) {
  154. return operand_count;
  155. } else if ( type == op_explicit ) {
  156. return explicit_count;
  157. }
  158. x86_operand_foreach( count_operand, &count, type );
  159. return count;
  160. }
  161. /* accessor functions */
  162. x86_op_t * x86_insn_t::x86_operand_1st() {
  163. if (! explicit_count ) {
  164. return NULL;
  165. }
  166. return &(operands->op);
  167. }
  168. x86_op_t * x86_insn_t::x86_operand_2nd( ) {
  169. if ( explicit_count < 2 ) {
  170. return NULL;
  171. }
  172. return &(operands->next->op);
  173. }
  174. x86_op_t * x86_insn_t::x86_operand_3rd( ) {
  175. if ( explicit_count < 3 ) {
  176. return NULL;
  177. }
  178. return &(operands->next->next->op);
  179. }