value.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /**
  2. * \file libyasm/value.h
  3. * \brief YASM value interface.
  4. *
  5. * \license
  6. * Copyright (C) 2006-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_VALUE_H
  31. #define YASM_VALUE_H
  32. #ifndef YASM_LIB_DECL
  33. #define YASM_LIB_DECL
  34. #endif
  35. /** Initialize a #yasm_value with just an expression. No processing is
  36. * performed, the expression is simply stuck into value.abs and the other
  37. * fields are initialized. Use yasm_expr_extract_value() to perform "smart"
  38. * processing into a #yasm_value. This function is intended for use during
  39. * parsing simply to ensure all fields of the value are initialized; after
  40. * the parse is complete, yasm_value_extract() should be called to finalize
  41. * the value. The value defaults to unsigned.
  42. * \param value value to be initialized
  43. * \param e expression (kept)
  44. * \param size value size (in bits)
  45. */
  46. YASM_LIB_DECL
  47. void yasm_value_initialize(/*@out@*/ yasm_value *value,
  48. /*@null@*/ /*@kept@*/ yasm_expr *e,
  49. unsigned int size);
  50. /** Initialize a #yasm_value with just a symrec. No processing is performed,
  51. * the symrec is simply stuck into value.rel and the other fields are
  52. * initialized.
  53. * \param value value to be initialized
  54. * \param sym symrec
  55. * \param size value size (in bits)
  56. */
  57. YASM_LIB_DECL
  58. void yasm_value_init_sym(/*@out@*/ yasm_value *value,
  59. /*@null@*/ yasm_symrec *sym, unsigned int size);
  60. /** Initialize a #yasm_value as a copy of another yasm_value. Any expressions
  61. * within orig are copied, so it's safe to delete the copy.
  62. * \param value value (copy to create)
  63. * \param orig original value
  64. */
  65. YASM_LIB_DECL
  66. void yasm_value_init_copy(yasm_value *value, const yasm_value *orig);
  67. /** Frees any memory inside value; does not free value itself.
  68. * \param value value
  69. */
  70. YASM_LIB_DECL
  71. void yasm_value_delete(yasm_value *value);
  72. /** Set a value to be relative to the current assembly position rather than
  73. * relative to the section start.
  74. * \param value value
  75. * \param bc bytecode containing value
  76. * \param ip_rel if nonzero, indicates IP-relative data relocation,
  77. * sometimes used to generate special relocations
  78. * \note If value is just an absolute value, will get an absolute symrec to
  79. * reference to (via bc's symbol table).
  80. */
  81. YASM_LIB_DECL
  82. void yasm_value_set_curpos_rel(yasm_value *value, yasm_bytecode *bc,
  83. unsigned int ip_rel);
  84. /** Perform yasm_value_finalize_expr() on a value that already exists from
  85. * being initialized with yasm_value_initialize().
  86. * \param value value
  87. * \param precbc previous bytecode to bytecode containing value
  88. * \return Nonzero if value could not be split.
  89. */
  90. YASM_LIB_DECL
  91. int yasm_value_finalize(yasm_value *value, /*@null@*/ yasm_bytecode *precbc);
  92. /** Break a #yasm_expr into a #yasm_value constituent parts. Extracts
  93. * the relative portion of the value, SEG and WRT portions, and top-level
  94. * right shift, if any. Places the remaining expr into the absolute
  95. * portion of the value. Essentially a combination of yasm_value_initialize()
  96. * and yasm_value_finalize(). First expands references to symrecs in
  97. * absolute sections by expanding with the absolute section start plus the
  98. * symrec offset within the absolute section.
  99. * \param value value to store split portions into
  100. * \param e expression input
  101. * \param precbc previous bytecode to bytecode containing expression
  102. * \param size value size (in bits)
  103. * \return Nonzero if the expr could not be split into a value for some
  104. * reason (e.g. the relative portion was not added, but multiplied,
  105. * etc).
  106. * \warning Do not use e after this call. Even if an error is returned, e
  107. * is stored into value.
  108. * \note This should only be called after the parse is complete. Calling
  109. * before the parse is complete will usually result in an error return.
  110. */
  111. YASM_LIB_DECL
  112. int yasm_value_finalize_expr(/*@out@*/ yasm_value *value,
  113. /*@null@*/ /*@kept@*/ yasm_expr *e,
  114. /*@null@*/ yasm_bytecode *precbc,
  115. unsigned int size);
  116. /** Get value if absolute or PC-relative section-local relative. Returns NULL
  117. * otherwise.
  118. * \param value value
  119. * \param bc current bytecode (for PC-relative calculation); if
  120. * NULL, NULL is returned for PC-relative values.
  121. * \param calc_bc_dist if nonzero, calculates bytecode distances in absolute
  122. * portion of value
  123. * \note Adds in value.rel (correctly) if PC-relative and in the same section
  124. * as bc (and there is no WRT or SEG).
  125. * \return Intnum if can be resolved to integer value, otherwise NULL.
  126. */
  127. YASM_LIB_DECL
  128. /*@null@*/ /*@only@*/ yasm_intnum *yasm_value_get_intnum
  129. (yasm_value *value, /*@null@*/ yasm_bytecode *bc, int calc_bc_dist);
  130. /** Output value if constant or PC-relative section-local. This should be
  131. * used from objfmt yasm_output_value_func() functions.
  132. * functions.
  133. * \param value value
  134. * \param buf buffer for byte representation
  135. * \param destsize destination size (in bytes)
  136. * \param bc current bytecode (usually passed into higher-level
  137. * calling function)
  138. * \param warn enables standard warnings: zero for none;
  139. * nonzero for overflow/underflow floating point and
  140. * integer warnings
  141. * \param arch architecture
  142. * \note Adds in value.rel (correctly) if PC-relative and in the same section
  143. * as bc (and there is no WRT or SEG); if this is not the desired
  144. * behavior, e.g. a reloc is needed in this case, don't use this
  145. * function!
  146. * \return 0 if no value output due to value needing relocation;
  147. * 1 if value output; -1 if error.
  148. */
  149. YASM_LIB_DECL
  150. int yasm_value_output_basic
  151. (yasm_value *value, /*@out@*/ unsigned char *buf, size_t destsize,
  152. yasm_bytecode *bc, int warn, yasm_arch *arch);
  153. /** Print a value. For debugging purposes.
  154. * \param value value
  155. * \param indent_level indentation level
  156. * \param f file
  157. */
  158. YASM_LIB_DECL
  159. void yasm_value_print(const yasm_value *value, FILE *f, int indent_level);
  160. #endif