insn.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. /**
  2. * \file libyasm/insn.h
  3. * \brief YASM mnenomic instruction.
  4. *
  5. * \license
  6. * Copyright (C) 2002-2007 Peter Johnson
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  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 copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. * \endlicense
  29. */
  30. #ifndef YASM_INSN_H
  31. #define YASM_INSN_H
  32. #ifndef YASM_LIB_DECL
  33. #define YASM_LIB_DECL
  34. #endif
  35. /** Base structure for an effective address. As with all base
  36. * structures, must be present as the first element in any
  37. * #yasm_arch implementation of an effective address.
  38. */
  39. struct yasm_effaddr {
  40. yasm_value disp; /**< address displacement */
  41. /** Segment register override (0 if none). */
  42. uintptr_t segreg;
  43. /** 1 if length of disp must be >0. */
  44. unsigned int need_nonzero_len:1;
  45. /** 1 if a displacement should be present in the output. */
  46. unsigned int need_disp:1;
  47. /** 1 if reg*2 should not be split into reg+reg. (0 if not).
  48. * This flag indicates (for architectures that support complex effective
  49. * addresses such as x86) if various types of complex effective addresses
  50. * can be split into different forms in order to minimize instruction
  51. * length.
  52. */
  53. unsigned int nosplit:1;
  54. /** 1 if effective address is /definitely/ an effective address.
  55. * This is used in e.g. the GAS parser to differentiate
  56. * between "expr" (which might or might not be an effective address) and
  57. * "expr(,1)" (which is definitely an effective address).
  58. */
  59. unsigned int strong:1;
  60. /** 1 if effective address is forced PC-relative. */
  61. unsigned int pc_rel:1;
  62. /** 1 if effective address is forced non-PC-relative. */
  63. unsigned int not_pc_rel:1;
  64. /** length of pointed data (in bytes), 0 if unknown. */
  65. unsigned int data_len;
  66. };
  67. /** An instruction operand (opaque type). */
  68. typedef struct yasm_insn_operand yasm_insn_operand;
  69. /** The type of an instruction operand. */
  70. typedef enum yasm_insn_operand_type {
  71. YASM_INSN__OPERAND_REG = 1, /**< A register. */
  72. YASM_INSN__OPERAND_SEGREG, /**< A segment register. */
  73. YASM_INSN__OPERAND_MEMORY, /**< An effective address
  74. * (memory reference). */
  75. YASM_INSN__OPERAND_IMM /**< An immediate or jump target. */
  76. } yasm_insn_operand_type;
  77. /** An instruction operand. */
  78. struct yasm_insn_operand {
  79. /** Link for building linked list of operands. \internal */
  80. /*@reldef@*/ STAILQ_ENTRY(yasm_insn_operand) link;
  81. /** Operand data. */
  82. union {
  83. uintptr_t reg; /**< Arch data for reg/segreg. */
  84. yasm_effaddr *ea; /**< Effective address for memory references. */
  85. yasm_expr *val; /**< Value of immediate or jump target. */
  86. } data;
  87. yasm_expr *seg; /**< Segment expression */
  88. uintptr_t targetmod; /**< Arch target modifier, 0 if none. */
  89. /** Specified size of the operand, in bits. 0 if not user-specified. */
  90. unsigned int size:16;
  91. /** Nonzero if dereference. Used for "*foo" in GAS.
  92. * The reason for this is that by default in GAS, an unprefixed value
  93. * is a memory address, except for jumps/calls, in which case it needs a
  94. * "*" prefix to become a memory address (otherwise it's an immediate).
  95. * This isn't knowable in the parser stage, so the parser sets this flag
  96. * to indicate the "*" prefix has been used, and the arch needs to adjust
  97. * the operand type appropriately depending on the instruction type.
  98. */
  99. unsigned int deref:1;
  100. /** Nonzero if strict. Used for "strict foo" in NASM.
  101. * This is used to inhibit optimization on otherwise "sized" values.
  102. * For example, the user may just want to be explicit with the size on
  103. * "push dword 4", but not actually want to force the immediate size to
  104. * 4 bytes (rather wanting the optimizer to optimize it down to 1 byte as
  105. * though "dword" was not specified). To indicate the immediate should
  106. * actually be forced to 4 bytes, the user needs to write
  107. * "push strict dword 4", which sets this flag.
  108. */
  109. unsigned int strict:1;
  110. /** Operand type. */
  111. unsigned int type:4;
  112. };
  113. /** Base structure for "instruction" bytecodes. These are the mnenomic
  114. * (rather than raw) representation of instructions. As with all base
  115. * structures, must be present as the first element in any
  116. * #yasm_arch implementation of mnenomic instruction bytecodes.
  117. */
  118. struct yasm_insn {
  119. /** Linked list of operands. */
  120. /*@reldef@*/ STAILQ_HEAD(yasm_insn_operands, yasm_insn_operand) operands;
  121. /** Array of prefixes. */
  122. /*@null@*/ uintptr_t *prefixes;
  123. /** Array of segment prefixes. */
  124. /*@null@*/ uintptr_t *segregs;
  125. unsigned int num_operands; /**< Number of operands. */
  126. unsigned int num_prefixes; /**< Number of prefixes. */
  127. unsigned int num_segregs; /**< Number of segment prefixes. */
  128. };
  129. /** Set segment override for an effective address.
  130. * Some architectures (such as x86) support segment overrides on effective
  131. * addresses. A override of an override will result in a warning.
  132. * \param ea effective address
  133. * \param segreg segment register (0 if none)
  134. */
  135. YASM_LIB_DECL
  136. void yasm_ea_set_segreg(yasm_effaddr *ea, uintptr_t segreg);
  137. /** Create an instruction operand from a register.
  138. * \param reg register
  139. * \return Newly allocated operand.
  140. */
  141. YASM_LIB_DECL
  142. yasm_insn_operand *yasm_operand_create_reg(uintptr_t reg);
  143. /** Create an instruction operand from a segment register.
  144. * \param segreg segment register
  145. * \return Newly allocated operand.
  146. */
  147. YASM_LIB_DECL
  148. yasm_insn_operand *yasm_operand_create_segreg(uintptr_t segreg);
  149. /** Create an instruction operand from an effective address.
  150. * \param ea effective address
  151. * \return Newly allocated operand.
  152. */
  153. YASM_LIB_DECL
  154. yasm_insn_operand *yasm_operand_create_mem(/*@only@*/ yasm_effaddr *ea);
  155. /** Create an instruction operand from an immediate expression.
  156. * Looks for cases of a single register and creates a register variant of
  157. * #yasm_insn_operand.
  158. * \param val immediate expression
  159. * \return Newly allocated operand.
  160. */
  161. YASM_LIB_DECL
  162. yasm_insn_operand *yasm_operand_create_imm(/*@only@*/ yasm_expr *val);
  163. /** Get the first operand in an instruction.
  164. * \param insn instruction
  165. * \return First operand (NULL if no operands).
  166. */
  167. yasm_insn_operand *yasm_insn_ops_first(yasm_insn *insn);
  168. #define yasm_insn_ops_first(insn) STAILQ_FIRST(&((insn)->operands))
  169. /** Get the next operand in an instruction.
  170. * \param op previous operand
  171. * \return Next operand (NULL if op was the last operand).
  172. */
  173. yasm_insn_operand *yasm_insn_op_next(yasm_insn_operand *op);
  174. #define yasm_insn_op_next(cur) STAILQ_NEXT(cur, link)
  175. /** Add operand to the end of an instruction.
  176. * \note Does not make a copy of the operand; so don't pass this function
  177. * static or local variables, and discard the op pointer after calling
  178. * this function.
  179. * \param insn instruction
  180. * \param op operand (may be NULL)
  181. * \return If operand was actually appended (it wasn't NULL), the operand;
  182. * otherwise NULL.
  183. */
  184. YASM_LIB_DECL
  185. /*@null@*/ yasm_insn_operand *yasm_insn_ops_append
  186. (yasm_insn *insn,
  187. /*@returned@*/ /*@null@*/ yasm_insn_operand *op);
  188. /** Associate a prefix with an instruction.
  189. * \param insn instruction
  190. * \param prefix data that identifies the prefix
  191. */
  192. YASM_LIB_DECL
  193. void yasm_insn_add_prefix(yasm_insn *insn, uintptr_t prefix);
  194. /** Associate a segment prefix with an instruction.
  195. * \param insn instruction
  196. * \param segreg data that identifies the segment register
  197. */
  198. YASM_LIB_DECL
  199. void yasm_insn_add_seg_prefix(yasm_insn *insn, uintptr_t segreg);
  200. /** Initialize the common parts of an instruction.
  201. * \internal For use by yasm_arch implementations only.
  202. * \param insn instruction
  203. */
  204. YASM_LIB_DECL
  205. void yasm_insn_initialize(/*@out@*/ yasm_insn *insn);
  206. /** Delete the common parts of an instruction.
  207. * \internal For use by yasm_arch implementations only.
  208. * \param insn instruction
  209. * \param content if nonzero, deletes content of each operand
  210. * \param arch architecture
  211. */
  212. YASM_LIB_DECL
  213. void yasm_insn_delete(yasm_insn *insn,
  214. void (*ea_destroy) (/*@only@*/ yasm_effaddr *));
  215. /** Print a list of instruction operands. For debugging purposes.
  216. * \internal For use by yasm_arch implementations only.
  217. * \param insn instruction
  218. * \param f file
  219. * \param indent_level indentation level
  220. * \param arch architecture
  221. */
  222. YASM_LIB_DECL
  223. void yasm_insn_print(const yasm_insn *insn, FILE *f, int indent_level);
  224. /** Finalize the common parts of an instruction.
  225. * \internal For use by yasm_arch implementations only.
  226. * \param insn instruction
  227. */
  228. YASM_LIB_DECL
  229. void yasm_insn_finalize(yasm_insn *insn);
  230. #endif