intnum.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /**
  2. * \file libyasm/intnum.h
  3. * \brief YASM integer number interface.
  4. *
  5. * \license
  6. * Copyright (C) 2001-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_INTNUM_H
  31. #define YASM_INTNUM_H
  32. #ifndef YASM_LIB_DECL
  33. #define YASM_LIB_DECL
  34. #endif
  35. /** Initialize intnum internal data structures. */
  36. YASM_LIB_DECL
  37. void yasm_intnum_initialize(void);
  38. /** Clean up internal intnum allocations. */
  39. YASM_LIB_DECL
  40. void yasm_intnum_cleanup(void);
  41. /** Create a new intnum from a decimal string.
  42. * \param str decimal string
  43. * \return Newly allocated intnum.
  44. */
  45. YASM_LIB_DECL
  46. /*@only@*/ yasm_intnum *yasm_intnum_create_dec(char *str);
  47. /** Create a new intnum from a binary string.
  48. * \param str binary string
  49. * \return Newly allocated intnum.
  50. */
  51. YASM_LIB_DECL
  52. /*@only@*/ yasm_intnum *yasm_intnum_create_bin(char *str);
  53. /** Create a new intnum from an octal string.
  54. * \param str octal string
  55. * \return Newly allocated intnum.
  56. */
  57. YASM_LIB_DECL
  58. /*@only@*/ yasm_intnum *yasm_intnum_create_oct(char *str);
  59. /** Create a new intnum from a hexidecimal string.
  60. * \param str hexidecimal string
  61. * \return Newly allocated intnum.
  62. */
  63. YASM_LIB_DECL
  64. /*@only@*/ yasm_intnum *yasm_intnum_create_hex(char *str);
  65. /** Convert character constant to integer value, using NASM rules. NASM syntax
  66. * supports automatic conversion from strings such as 'abcd' to a 32-bit
  67. * integer value (little endian order). This function performs those conversions.
  68. * \param str character constant string
  69. * \return Newly allocated intnum.
  70. */
  71. YASM_LIB_DECL
  72. /*@only@*/ yasm_intnum *yasm_intnum_create_charconst_nasm(const char *str);
  73. /** Convert character constant to integer value, using TASM rules. TASM syntax
  74. * supports automatic conversion from strings such as 'abcd' to a 32-bit
  75. * integer value (big endian order). This function performs those conversions.
  76. * \param str character constant string
  77. * \return Newly allocated intnum.
  78. */
  79. YASM_LIB_DECL
  80. /*@only@*/ yasm_intnum *yasm_intnum_create_charconst_tasm(const char *str);
  81. /** Create a new intnum from an unsigned integer value.
  82. * \param i unsigned integer value
  83. * \return Newly allocated intnum.
  84. */
  85. YASM_LIB_DECL
  86. /*@only@*/ yasm_intnum *yasm_intnum_create_uint(unsigned long i);
  87. /** Create a new intnum from an signed integer value.
  88. * \param i signed integer value
  89. * \return Newly allocated intnum.
  90. */
  91. YASM_LIB_DECL
  92. /*@only@*/ yasm_intnum *yasm_intnum_create_int(long i);
  93. /** Create a new intnum from LEB128-encoded form.
  94. * \param ptr pointer to start of LEB128 encoded form
  95. * \param sign signed (1) or unsigned (0) LEB128 format
  96. * \param size number of bytes read from ptr (output)
  97. * \return Newly allocated intnum. Number of bytes read returned into
  98. * bytes_read parameter.
  99. */
  100. YASM_LIB_DECL
  101. /*@only@*/ yasm_intnum *yasm_intnum_create_leb128
  102. (const unsigned char *ptr, int sign, /*@out@*/ unsigned long *size);
  103. /** Create a new intnum from a little-endian or big-endian buffer.
  104. * In little endian, the LSB is in ptr[0].
  105. * \param ptr pointer to start of buffer
  106. * \param sign signed (1) or unsigned (0) source
  107. * \param srcsize source buffer size (in bytes)
  108. * \param bigendian endianness (nonzero=big, zero=little)
  109. */
  110. YASM_LIB_DECL
  111. /*@only@*/ yasm_intnum *yasm_intnum_create_sized
  112. (unsigned char *ptr, int sign, size_t srcsize, int bigendian);
  113. /** Duplicate an intnum.
  114. * \param intn intnum
  115. * \return Newly allocated intnum with the same value as intn.
  116. */
  117. YASM_LIB_DECL
  118. /*@only@*/ yasm_intnum *yasm_intnum_copy(const yasm_intnum *intn);
  119. /** Destroy (free allocated memory for) an intnum.
  120. * \param intn intnum
  121. */
  122. YASM_LIB_DECL
  123. void yasm_intnum_destroy(/*@only@*/ yasm_intnum *intn);
  124. /** Floating point calculation function: acc = acc op operand.
  125. * \note Not all operations in yasm_expr_op may be supported; unsupported
  126. * operations will result in an error.
  127. * \param acc intnum accumulator
  128. * \param op operation
  129. * \param operand intnum operand
  130. * \return Nonzero if error occurred.
  131. */
  132. YASM_LIB_DECL
  133. int yasm_intnum_calc(yasm_intnum *acc, yasm_expr_op op, yasm_intnum *operand);
  134. /** Compare two intnums.
  135. * \param intn1 first intnum
  136. * \param intn2 second intnum
  137. * \return -1 if intn1 < intn2, 0 if intn1 == intn2, 1 if intn1 > intn2.
  138. */
  139. YASM_LIB_DECL
  140. int yasm_intnum_compare(const yasm_intnum *intn1, const yasm_intnum *intn2);
  141. /** Zero an intnum.
  142. * \param intn intnum
  143. */
  144. YASM_LIB_DECL
  145. void yasm_intnum_zero(yasm_intnum *intn);
  146. /** Set an intnum to the value of another intnum.
  147. * \param intn intnum
  148. * \param val intnum to get value from
  149. */
  150. YASM_LIB_DECL
  151. void yasm_intnum_set(yasm_intnum *intn, const yasm_intnum *val);
  152. /** Set an intnum to an unsigned integer.
  153. * \param intn intnum
  154. * \param val integer value
  155. */
  156. YASM_LIB_DECL
  157. void yasm_intnum_set_uint(yasm_intnum *intn, unsigned long val);
  158. /** Set an intnum to an signed integer.
  159. * \param intn intnum
  160. * \param val integer value
  161. */
  162. YASM_LIB_DECL
  163. void yasm_intnum_set_int(yasm_intnum *intn, long val);
  164. /** Simple value check for 0.
  165. * \param acc intnum
  166. * \return Nonzero if acc==0.
  167. */
  168. YASM_LIB_DECL
  169. int yasm_intnum_is_zero(const yasm_intnum *acc);
  170. /** Simple value check for 1.
  171. * \param acc intnum
  172. * \return Nonzero if acc==1.
  173. */
  174. YASM_LIB_DECL
  175. int yasm_intnum_is_pos1(const yasm_intnum *acc);
  176. /** Simple value check for -1.
  177. * \param acc intnum
  178. * \return Nonzero if acc==-1.
  179. */
  180. YASM_LIB_DECL
  181. int yasm_intnum_is_neg1(const yasm_intnum *acc);
  182. /** Simple sign check.
  183. * \param acc intnum
  184. * \return -1 if negative, 0 if zero, +1 if positive
  185. */
  186. YASM_LIB_DECL
  187. int yasm_intnum_sign(const yasm_intnum *acc);
  188. /** Convert an intnum to an unsigned 32-bit value. The value is in "standard"
  189. * C format (eg, of unknown endian).
  190. * \note Parameter intnum is truncated to fit into 32 bits. Use
  191. * intnum_check_size() to check for overflow.
  192. * \param intn intnum
  193. * \return Unsigned 32-bit value of intn.
  194. */
  195. YASM_LIB_DECL
  196. unsigned long yasm_intnum_get_uint(const yasm_intnum *intn);
  197. /** Convert an intnum to a signed 32-bit value. The value is in "standard" C
  198. * format (eg, of unknown endian).
  199. * \note Parameter intnum is truncated to fit into 32 bits. Use
  200. * intnum_check_size() to check for overflow.
  201. * \param intn intnum
  202. * \return Signed 32-bit value of intn.
  203. */
  204. YASM_LIB_DECL
  205. long yasm_intnum_get_int(const yasm_intnum *intn);
  206. /** Output #yasm_intnum to buffer in little-endian or big-endian. Puts the
  207. * value into the least significant bits of the destination, or may be shifted
  208. * into more significant bits by the shift parameter. The destination bits are
  209. * cleared before being set. [0] should be the first byte output to the file.
  210. * \param intn intnum
  211. * \param ptr pointer to storage for size bytes of output
  212. * \param destsize destination size (in bytes)
  213. * \param valsize size (in bits)
  214. * \param shift left shift (in bits); may be negative to specify right
  215. * shift (standard warnings include truncation to boundary)
  216. * \param bigendian endianness (nonzero=big, zero=little)
  217. * \param warn enables standard warnings (value doesn't fit into valsize
  218. * bits): <0=signed warnings, >0=unsigned warnings, 0=no warn
  219. */
  220. YASM_LIB_DECL
  221. void yasm_intnum_get_sized(const yasm_intnum *intn, unsigned char *ptr,
  222. size_t destsize, size_t valsize, int shift,
  223. int bigendian, int warn);
  224. /** Check to see if intnum will fit without overflow into size bits.
  225. * \param intn intnum
  226. * \param size number of bits of output space
  227. * \param rshift right shift
  228. * \param rangetype signed/unsigned range selection:
  229. * 0 => (0, unsigned max);
  230. * 1 => (signed min, signed max);
  231. * 2 => (signed min, unsigned max)
  232. * \return Nonzero if intnum will fit.
  233. */
  234. YASM_LIB_DECL
  235. int yasm_intnum_check_size(const yasm_intnum *intn, size_t size,
  236. size_t rshift, int rangetype);
  237. /** Check to see if intnum will fit into a particular numeric range.
  238. * \param intn intnum
  239. * \param low low end of range (inclusive)
  240. * \param high high end of range (inclusive)
  241. * \return Nonzero if intnum is within range.
  242. */
  243. YASM_LIB_DECL
  244. int yasm_intnum_in_range(const yasm_intnum *intn, long low, long high);
  245. /** Output #yasm_intnum to buffer in LEB128-encoded form.
  246. * \param intn intnum
  247. * \param ptr pointer to storage for output bytes
  248. * \param sign signedness of LEB128 encoding (0=unsigned, 1=signed)
  249. * \return Number of bytes generated.
  250. */
  251. YASM_LIB_DECL
  252. unsigned long yasm_intnum_get_leb128(const yasm_intnum *intn,
  253. unsigned char *ptr, int sign);
  254. /** Calculate number of bytes LEB128-encoded form of #yasm_intnum will take.
  255. * \param intn intnum
  256. * \param sign signedness of LEB128 encoding (0=unsigned, 1=signed)
  257. * \return Number of bytes.
  258. */
  259. YASM_LIB_DECL
  260. unsigned long yasm_intnum_size_leb128(const yasm_intnum *intn, int sign);
  261. /** Output integer to buffer in signed LEB128-encoded form.
  262. * \param v integer
  263. * \param ptr pointer to storage for output bytes
  264. * \return Number of bytes generated.
  265. */
  266. YASM_LIB_DECL
  267. unsigned long yasm_get_sleb128(long v, unsigned char *ptr);
  268. /** Calculate number of bytes signed LEB128-encoded form of integer will take.
  269. * \param v integer
  270. * \return Number of bytes.
  271. */
  272. YASM_LIB_DECL
  273. unsigned long yasm_size_sleb128(long v);
  274. /** Output integer to buffer in unsigned LEB128-encoded form.
  275. * \param v integer
  276. * \param ptr pointer to storage for output bytes
  277. * \return Number of bytes generated.
  278. */
  279. YASM_LIB_DECL
  280. unsigned long yasm_get_uleb128(unsigned long v, unsigned char *ptr);
  281. /** Calculate number of bytes unsigned LEB128-encoded form of integer will take.
  282. * \param v integer
  283. * \return Number of bytes.
  284. */
  285. YASM_LIB_DECL
  286. unsigned long yasm_size_uleb128(unsigned long v);
  287. /** Get an intnum as a signed decimal string. The returned string will
  288. * contain a leading '-' if the intnum is negative.
  289. * \param intn intnum
  290. * \return Newly allocated string containing the decimal representation of
  291. * the intnum.
  292. */
  293. YASM_LIB_DECL
  294. /*@only@*/ char *yasm_intnum_get_str(const yasm_intnum *intn);
  295. /** Print an intnum. For debugging purposes.
  296. * \param f file
  297. * \param intn intnum
  298. */
  299. YASM_LIB_DECL
  300. void yasm_intnum_print(const yasm_intnum *intn, FILE *f);
  301. #endif