x86_operand_list.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. /** free the operand list associated with an instruction -- useful for
  59. * preventing memory leaks when free()ing an x86_insn_t */
  60. void x86_insn_t::x86_oplist_free( )
  61. {
  62. x86_oplist_t *op, *list;
  63. assert(this);
  64. for ( list = operands; list; ) {
  65. op = list;
  66. list = list->next;
  67. free(op);
  68. }
  69. operands = NULL;
  70. operand_count = 0;
  71. explicit_count = 0;
  72. return;
  73. }
  74. /* ================================================== LIBDISASM API */
  75. /* these could probably just be #defines, but that means exposing the
  76. enum... yet one more confusing thing in the API */
  77. int x86_insn_t::x86_operand_foreach( x86_operand_fn func, void *arg, enum x86_op_foreach_type type )
  78. {
  79. x86_oplist_t *list;
  80. char _explicit = 1, implicit = 1;
  81. assert(this);
  82. if ( ! func ) {
  83. return 0;
  84. }
  85. /* note: explicit and implicit can be ORed together to
  86. * allow an "all" limited by access type, even though the
  87. * user is stupid to do this since it is default behavior :) */
  88. if ( (type & op_explicit) && ! (type & op_implicit) ) {
  89. implicit = 0;
  90. }
  91. if ( (type & op_implicit) && ! (type & op_explicit) ) {
  92. _explicit = 0;
  93. }
  94. type = (x86_op_foreach_type)((int)type & 0x0F); /* mask out explicit/implicit operands */
  95. for ( list = operands; list; list = list->next ) {
  96. if (! implicit && (list->op.flags.op_implied) ) {
  97. /* operand is implicit */
  98. continue;
  99. }
  100. if (! _explicit && ! (list->op.flags.op_implied) ) {
  101. /* operand is not implicit */
  102. continue;
  103. }
  104. switch ( type ) {
  105. case op_any:
  106. break;
  107. case op_dest:
  108. if (! (list->op.access & op_write) ) {
  109. continue;
  110. }
  111. break;
  112. case op_src:
  113. if (! (list->op.access & op_read) ) {
  114. continue;
  115. }
  116. break;
  117. case op_ro:
  118. if (! (list->op.access & op_read) ||
  119. (list->op.access & op_write ) ) {
  120. continue;
  121. }
  122. break;
  123. case op_wo:
  124. if (! (list->op.access & op_write) ||
  125. (list->op.access & op_read ) ) {
  126. continue;
  127. }
  128. break;
  129. case op_xo:
  130. if (! (list->op.access & op_execute) ) {
  131. continue;
  132. }
  133. break;
  134. case op_rw:
  135. if (! (list->op.access & op_write) ||
  136. ! (list->op.access & op_read ) ) {
  137. continue;
  138. }
  139. break;
  140. case op_implicit: case op_explicit: /* make gcc happy */
  141. break;
  142. }
  143. /* any non-continue ends up here: invoke the callback */
  144. (*func)( &list->op, this, arg );
  145. }
  146. return 1;
  147. }
  148. static void count_operand( x86_op_t */*op*/, x86_insn_t */*insn*/, void *arg ) {
  149. size_t * count = (size_t *) arg;
  150. *count = *count + 1;
  151. }
  152. size_t x86_insn_t::x86_operand_count( enum x86_op_foreach_type type ) {
  153. size_t count = 0;
  154. /* save us a list traversal for common counts... */
  155. if ( type == op_any ) {
  156. return operand_count;
  157. } else if ( type == op_explicit ) {
  158. return explicit_count;
  159. }
  160. x86_operand_foreach( count_operand, &count, type );
  161. return count;
  162. }
  163. /* accessor functions */
  164. x86_op_t * x86_insn_t::x86_operand_1st() {
  165. if (! explicit_count ) {
  166. return NULL;
  167. }
  168. return &(operands->op);
  169. }
  170. x86_op_t * x86_insn_t::x86_operand_2nd( ) {
  171. if ( explicit_count < 2 ) {
  172. return NULL;
  173. }
  174. return &(operands->next->op);
  175. }
  176. x86_op_t * x86_insn_t::x86_operand_3rd( ) {
  177. if ( explicit_count < 3 ) {
  178. return NULL;
  179. }
  180. return &(operands->next->next->op);
  181. }