Create a new node with the right mode instead of changing the mode.
[libfirm] / include / libfirm / tv.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Representation of and static computations on target machine
23  *           values.
24  * @date     2003
25  * @author   Mathias Heil
26  * @version  $Id$
27  * @brief
28  *   Tarvals represent target machine values.  They are typed by modes.
29  *   Tarvals only represent values of mode_sort:
30  *    - int_number,
31  *    - float_number,
32  *    - boolean,
33  *    - reference,
34  *    - character
35  *
36  *   In case of references the module accepts an entity to represent the
37  *   value.
38  *   Furthermore, computations and conversions of these values can
39  *   be performed.
40  *
41  * HISTORY
42  *    The original tv module originated in the fiasco compiler written ...
43  *    This is the new version, described in the tech report 1999-14 by ...
44  *
45  * @sa
46  *    Techreport 1999-14
47  *    irmode.h for the modes definitions
48  *    irnode.h for the pn_Cmp table
49  */
50 #ifndef FIRM_TV_TV_H
51 #define FIRM_TV_TV_H
52
53 #include "firm_types.h"
54 #include "irnode.h"
55
56 /* ************************ Constructors for tarvals ************************ */
57
58 /**
59  * Constructor function for new tarvals.
60  *
61  * @param str   The string representing the target value
62  * @param len   The length of the string
63  * @param mode  The mode requested for the result tarval
64  *
65  * This function creates a new tarval representing the value represented
66  * by a CString, aka char array. If a tarval representing this value already
67  * exists, this tarval is returned instead of a new one. So tarvals are
68  * directly comparable since their representation is unique.
69  *
70  * This function accepts the following strings:
71  *
72  * if mode is int_number:
73  *  - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
74  *  - 0[0-7]*            (octal representation)
75  *  - (+|-)?[1-9][0-9]*  (decimal representation)
76  *
77  * if mode is float_number:
78  *  - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
79  *
80  * if mode is boolean: true, True, TRUE ... False... 0, 1,
81  *
82  * if mode is reference: hexadecimal of decimal number as int
83  *
84  * if mode is character: hex or dec
85  *
86  * Leading and/or trailing spaces are ignored
87  *
88  * @return
89  *   A tarval of proper type representing the requested value is returned.
90  *   Tarvals are unique, so for any value/mode pair at most one tarval will
91  *   exist, which will be returned upon further requests with an identical
92  *   value/mode pair.
93  *
94  * @note
95  *   If the string is not representable in the given mode an assertion is
96  *   thrown in assert build.
97  *
98  * @sa
99  *   irmode.h for predefined modes
100  *   new_tarval_from_long()
101  *   new_tarval_from_double()
102  */
103 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
104
105 /**
106  * Construct a new tarval from a given string.
107  *
108  * @param str   The string representing the target value
109  * @param len   The length of the string
110  * @param sign  is -1 or 1 depending on the numbers sign
111  * @param base  number system base.
112  *              binary(2), octal(8), decimal(10) and hexadecimal(16) numbers
113  *              are supported.
114  * @param mode  The mode requested for the result tarval
115  *
116  * @return
117  *   A tarval with the given mode. If overflow settings are set to
118  *   TV_OVERFLOW_BAD then a tarval_bad is returned if the number can't be
119  *   represented in the given mode.
120  *   Return bad if the number couldn't successfully be parsed.
121  */
122 tarval *new_integer_tarval_from_str(const char *str, size_t len, char sign,
123                                     unsigned char base, ir_mode *mode);
124
125 /**
126  * Constructor function for new tarvals
127  *
128  * @param l     The long representing the value
129  * @param mode  The mode requested for the result tarval
130  *
131  * This function creates a new tarval representing the value represented
132  * by a long integer. If a tarval representing this value already exists,
133  * this tarval is returned instead of a new one. So tarvals are directly
134  * comparable since their representation is unique.
135  *
136  * @return
137  *   A tarval of proper type representing the requested value is returned.
138  *   Tarvals are unique, so for any value/mode pair at most one tarval will
139  *   exist, which will be returned upon further requests with an identical
140  *   value/mode pair.
141  *
142  * @note
143  *   If the long is not representable in the given mode an assertion is
144  *   thrown in assert build.
145  *
146  * @sa
147  *   irmode.h for predefined modes
148  *   new_tarval_from_str()
149  *   new_tarval_from_double()
150  *
151  */
152 tarval *new_tarval_from_long(long l, ir_mode *mode);
153
154 /** Return value as long if possible.
155  *
156  * This returns a long int with the value represented value, or
157  * gibberish, depending on the size of long int and the size of the
158  * stored value. It works for e.g. 1 as mode_Ls, but might not work for
159  * get_mode_max(mode_Ls).
160  * This will overflow silently, so use only if you know what
161  * you are doing! (better check with tarval_is_long()...)
162  * Works only for int modes, even not for character modes!
163  */
164 long get_tarval_long(tarval *tv);
165
166 /**
167  * This validates if get_tarval_long() will return a satisfying
168  * result. I.e. if tv is an int_number and between min, max
169  * of long int (signed!)
170  *
171  * @param tv    the tarval
172  */
173 int tarval_is_long(tarval *tv);
174
175 /**
176  * Constructor function for new tarvals.
177  *
178  * @param d     The (long) double representing the value
179  * @param mode  The mode requested for the result tarval
180  *
181  * This function creates a new tarval representing the value represented
182  * by a (long) double. If a tarval representing this value already exists,
183  * this tarval is returned instead of a new one. So tarvals are directly
184  * comparable since their representation is unique.
185  * Only modes of sort float_number can be constructed this way.
186  *
187  * @return
188  *   A tarval of proper type representing the requested value is returned.
189  *   Tarvals are unique, so for any value/mode pair at most one tarval will
190  *   exist, which will be returned upon further requests with an identical
191  *   value/mode pair.
192  *
193  * @note
194  *   If the (long) double is not representable in the given mode an assertion
195  *   is thrown. This will happen for any mode not of sort float_number.
196  *
197  * @sa
198  *   irmode.h for predefined values
199  *   new_tarval_from_str()
200  *   new_tarval_from_long()
201  */
202 tarval *new_tarval_from_double(long double d, ir_mode *mode);
203
204 /**
205  * This returns a double with the value represented value, or
206  * gibberish, depending on the size of double and the size of the
207  * stored value.
208  * This will overflow silently, so use only if you know what
209  * you are doing! (better check with tarval_is_long...)
210  *
211  * @param tv    the tarval
212  */
213 long double get_tarval_double(tarval *tv);
214
215 /**
216  * This validates if tarval_to_double() will return a satisfying
217  * result. I.e. if tv is an float_number and between min, max
218  * of double
219  *
220  * @param tv    the tarval
221  */
222 int tarval_is_double(tarval *tv);
223
224
225 /** ********** Access routines for tarval fields ********** **/
226
227 /*
228  * NAME
229  *   get_tarval_mode
230  *   get_tarval_ ...
231  *
232  * SYNOPSIS
233  *   ir_mode *get_tarval_mode(tarval *tv)
234  *   ...
235  *
236  * DESCRIPTION
237  *    These are access function for tarval struct members. It is encouraged
238  *   to use them instead of direct access to the struct fields.
239  *
240  * PARAMETERS
241  *   tv - The tarval to access fields of
242  *
243  * RESULT
244  *   get_tv_mode: The mode of the tarval
245  *
246  * SEE ALSO
247  *   the struct tarval
248  */
249
250 /**
251  * Returns the mode of the tarval.
252  *
253  * @param tv    the tarval
254  */
255 ir_mode *get_tarval_mode(const tarval *tv);
256
257 /** Returns the contents of the 'link' field of the tarval */
258 /* void *get_tarval_link (tarval*); */
259
260 /* Testing properties of the represented values */
261
262 /**
263  * Returns 1 if tv is negative
264  *
265  * @param tv    the tarval
266  */
267 int tarval_is_negative(tarval *tv);
268
269 /**
270  * Returns 1 if tv is null
271  *
272  * @param tv    the tarval
273  */
274 int tarval_is_null(tarval *tv);
275
276 /**
277  * Returns 1 if tv is the "one"
278  *
279  * @param tv    the tarval
280  */
281 int tarval_is_one(tarval *tv);
282
283 /**
284  * Returns 1 if tv is the "minus one"
285  *
286  * @param tv    the tarval
287  */
288 int tarval_is_minus_one(tarval *tv);
289
290 /**
291  * returns non-zero if all bits in the tarval are set
292  */
293 int tarval_is_all_one(tarval *tv);
294
295 /**
296  * Return non-zero if the tarval is a constant (ie. NOT
297  * a reserved tarval like bad, undef, reachable etc.)
298  */
299 int tarval_is_constant(tarval *tv);
300
301 /** The 'bad' tarval. */
302 extern tarval *tarval_bad;
303 /** Returns the 'bad' tarval. */
304 tarval *get_tarval_bad(void);
305
306 /** The 'undefined' tarval. */
307 extern tarval *tarval_undefined;
308 /** Returns the 'undefined' tarval. */
309 tarval *get_tarval_undefined(void);
310
311 /** The mode_b tarval 'false'. */
312 extern tarval *tarval_b_false;
313 /** Returns the mode_b tarval 'false'. */
314 tarval *get_tarval_b_false(void);
315
316 /** The mode_b tarval 'true'. */
317 extern tarval *tarval_b_true;
318 /** Returns the mode_b tarval 'true'. */
319 tarval *get_tarval_b_true(void);
320
321 /** The mode_X tarval 'unreachable'. */
322 extern tarval *tarval_unreachable;
323 /** Returns the mode_X tarval 'unreachable'. */
324 tarval *get_tarval_unreachable(void);
325
326 /** The mode_X tarval 'reachable'. */
327 extern tarval *tarval_reachable;
328 /** Returns the mode_X tarval 'reachable'. */
329 tarval *get_tarval_reachable(void);
330
331 /** The 'top' tarval. This is just another name for the 'undefined' tarval. */
332 #define tarval_top          tarval_undefined
333 /** Returns the 'top' tarval. */
334 #define get_tarval_top()    get_tarval_undefined()
335
336 /** The 'bottom' tarval. This is just another name for the 'bad' tarval. */
337 #define tarval_bottom       tarval_bad
338 /** Returns the 'bottom' tarval. */
339 #define get_tarval_bottom() get_tarval_bad()
340
341 /* These functions calculate and return a tarval representing the requested
342  * value.
343  * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
344  * functions, but these are stored on initialization of the irmode module and
345  * therefore the irmode functions should be preferred to the functions below. */
346
347 /** Returns the maximum value of a given mode. */
348 tarval *get_tarval_max(ir_mode *mode);
349
350 /** Returns the minimum value of a given mode. */
351 tarval *get_tarval_min(ir_mode *mode);
352
353 /** Returns the 0 value (additive neutral) of a given mode.
354     For reference modes, the NULL value is returned (old tarval_P_void) */
355 tarval *get_tarval_null(ir_mode *mode);
356
357 /** Returns the 1 value (multiplicative neutral) of a given mode. */
358 tarval *get_tarval_one(ir_mode *mode);
359
360 /** Returns the -1 value (multiplicative neutral) of a given mode.
361  *  Returns tarval bad for unsigned modes */
362 tarval *get_tarval_minus_one(ir_mode *mode);
363
364 /** returns the value where all bits are 1 of a given mode.
365  * returns tarval_bad for float modes */
366 tarval *get_tarval_all_one(ir_mode *mode);
367
368 /** Return quite nan for float_number modes. */
369 tarval *get_tarval_nan(ir_mode *mode);
370
371 /** Return +inf for float_number modes. */
372 tarval *get_tarval_plus_inf(ir_mode *mode);
373
374 /** Return -inf for float_number modes. */
375 tarval *get_tarval_minus_inf(ir_mode *mode);
376
377 /* ******************** Arithmetic operations on tarvals ******************** */
378
379 typedef enum _tarval_int_overflow_mode_t {
380         TV_OVERFLOW_BAD,      /**< tarval module will return tarval_bad if a overflow occurs */
381         TV_OVERFLOW_WRAP,     /**< tarval module will overflow will be ignored, wrap around occurs */
382         TV_OVERFLOW_SATURATE  /**< tarval module will saturate the overflow */
383 } tarval_int_overflow_mode_t;
384
385 /**
386  * Sets the overflow mode for integer operations.
387  *
388  * @param ov_mode  one of teh overflow modes
389  */
390 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
391
392 /**
393  * Get the overflow mode for integer operations.
394  */
395 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
396
397 /**
398  * Compares two tarvals
399  *
400  * Compare a with b and return a pn_Cmp describing the relation
401  * between a and b.  This is either pn_Cmp_Uo, pn_Cmp_Lt, pn_Cmp_Eq, pn_Cmp_Gt,
402  * or pn_Cmp_False if a or b are symbolic pointers which can not be compared at all.
403  *
404  * @param a   the first tarval to be compared
405  * @param b   the second tarval to be compared
406  *
407  * @return
408  *   The pn_Cmp best describing the relation between a and b is returned.
409  *   This means the mode with the least bits set is returned, e.g. if the
410  *   tarvals are equal the pn_Cmp 'pn_Cmp_Eq' is returned, not 'pn_Cmp_Ge' which
411  *   indicates 'greater or equal'
412  *
413  * @sa
414  *    irnode.h for the definition of pn_Cmp
415  */
416 pn_Cmp tarval_cmp(tarval *a, tarval *b);
417
418 /**
419  * Converts a tarval to another mode.
420  *
421  * Convert tarval 'src' to mode 'mode', this will succeed if and only if mode
422  * 'mode' is wider than the mode of src, as defined in the firm documentation
423  * and as returned by the function mode_is_smaller defined in irmode.h.
424  *
425  * @param src    The tarval to convert
426  * @param mode   Tho mode to convert to
427  *
428  * @return
429  *   If a tarval of mode 'mode' with the result of the conversion of the 'src'
430  *   tarvals value already exists, it will be returned, else a new tarval is
431  *   constructed and returned
432  *
433  * @note
434  *    Illegal convertions will trigger a panic
435  *
436  * @sa
437  *    FIRM documentation for conversion rules
438  *    mode_is_smaller defined in irmode.h
439  */
440 tarval *tarval_convert_to(tarval *src, ir_mode *mode);
441
442 /*
443  * These function implement basic computations representable as opcodes
444  * in FIRM nodes.
445  *
446  * PARAMETERS
447  *    tarval_neg:
448  *    traval_abs:
449  *      a - the tarval to operate on
450  *
451  *    all others:
452  *      a - the first operand tarval
453  *      b - the second operand tarval
454  *
455  * RESULT
456  *    If necessary a new tarval is constructed for the resulting value,
457  *   or the one already carrying the computation result is retrieved and
458  *   returned as result.
459  *
460  * NOTES
461  *   The order the arguments are given in is important, imagine postfix
462  *   notation.
463  *   Illegal operations will trigger an assertion.
464  *   The sort member of the struct mode defines which operations are valid
465  */
466
467 /**
468  * Bitwise Negation of a tarval.
469  *
470  * @param a  the first tarval
471  *
472  * @return ~a or tarval_bad
473  */
474 tarval *tarval_not(tarval *a);
475
476 /**
477  * Arithmetic Negation of a tarval.
478  *
479  * @param a  the first tarval
480  *
481  * @return -a or tarval_bad
482  */
483 tarval *tarval_neg(tarval *a);
484
485 /**
486  * Addition of two tarvals.
487  *
488  * @param a  the first tarval
489  * @param b  the second tarval
490  *
491  * @return a + b or tarval_bad
492  */
493 tarval *tarval_add(tarval *a, tarval *b);
494
495 /**
496  * Subtraction from a tarval.
497  *
498  * @param a         the first tarval
499  * @param b         the second tarval
500  * @param dst_mode  the mode of the result, needed for mode_P - mode_P, else NULL
501  *
502  * @return a - b or tarval_bad
503  */
504 tarval *tarval_sub(tarval *a, tarval *b, ir_mode *dst_mode);
505
506 /**
507  * Multiplication of tarvals.
508  *
509  * @param a  the first tarval
510  * @param b  the second tarval
511  *
512  * @return a * b or tarval_bad
513  */
514 tarval *tarval_mul(tarval *a, tarval *b);
515
516 /**
517  * Division of two floating point tarvals.
518  *
519  * @param a  the first tarval
520  * @param b  the second tarval
521  *
522  * @return a / b or tarval_bad
523  */
524 tarval *tarval_quo(tarval *a, tarval *b);
525
526 /**
527  * Integer division of two tarvals.
528  *
529  * @param a  the first tarval
530  * @param b  the second tarval
531  *
532  * @return a / b or tarval_bad
533  */
534 tarval *tarval_div(tarval *a, tarval *b);
535
536 /**
537  * Remainder of integer division.
538  *
539  * @param a  the first tarval
540  * @param b  the second tarval
541  *
542  * @return a % b or tarval_bad
543  */
544 tarval *tarval_mod(tarval *a, tarval *b);
545
546 /**
547  * Integer division AND remainder.
548  *
549  * @param a        the first tarval
550  * @param b        the second tarval
551  * @param mod_res  after return, contains the remainder result, a % b or tarval_bad
552  *
553  * @return a / b or tarval_bad
554  */
555 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod_res);
556
557 /**
558  * Absolute value of a tarval.
559  *
560  * @param a  the first tarval
561  *
562  * @return |a| or tarval_bad
563  */
564 tarval *tarval_abs(tarval *a);
565
566 /**
567  * Bitwise and of two integer tarvals.
568  *
569  * @param a  the first tarval
570  * @param b  the second tarval
571  *
572  * @return a & b or tarval_bad
573  */
574 tarval *tarval_and(tarval *a, tarval *b);
575
576 /**
577  * Bitwise and not of two integer tarvals.
578  *
579  * @param a  the first tarval
580  * @param b  the second tarval
581  *
582  * @return a & ~b or tarval_bad
583  */
584 tarval *tarval_andnot(tarval *a, tarval *b);
585
586 /**
587  * Bitwise or of two integer tarvals.
588  *
589  * @param a  the first tarval
590  * @param b  the second tarval
591  *
592  * @return a | b or tarval_bad
593  */
594 tarval *tarval_or(tarval *a, tarval *b);
595
596 /**
597  * Bitwise exclusive or of two integer tarvals.
598  *
599  * @param a  the first tarval
600  * @param b  the second tarval
601  *
602  * @return a ^ b or tarval_bad
603  */
604 tarval *tarval_eor(tarval *a, tarval *b);
605
606 /**
607  * Logical Left shift.
608  *
609  * @param a  the first tarval
610  * @param b  the second tarval
611  *
612  * @return a << b or tarval_bad
613  */
614 tarval *tarval_shl(tarval *a, tarval *b);
615
616 /**
617  * Unsigned (logical) right shift.
618  *
619  * @param a  the first tarval
620  * @param b  the second tarval
621  *
622  * @return a >>u b or tarval_bad
623  */
624 tarval *tarval_shr(tarval *a, tarval *b);
625
626 /**
627  * Signed (arithmetic) right shift.
628  *
629  * @param a  the first tarval
630  * @param b  the second tarval
631  *
632  * @return a >>s b or tarval_bad
633  */
634 tarval *tarval_shrs(tarval *a, tarval *b);
635
636 /**
637  * Rotation to left.
638  *
639  * @param a  the first tarval
640  * @param b  the second tarval
641  *
642  * @return a \<\<L\>\> b or tarval_bad
643  */
644 tarval *tarval_rotl(tarval *a, tarval *b);
645
646 /**
647  * Returns the carry flag of the last operation.
648  */
649 int tarval_carry(void);
650
651 /* *********** Output of tarvals *********** */
652
653 /**
654  * The output mode for tarval values.
655  *
656  * Some modes allow more that one representation, for instance integers
657  * can be represented hex or decimal. Of course it would be enough to have
658  * one and let every backend convert it into the 'right' one.
659  * However, we can do this in the tarval much simpler...
660  */
661 typedef enum {
662         TVO_NATIVE,       /**< the default output mode, depends on the mode */
663         TVO_HEX,          /**< use hex representation, always possible */
664         TVO_DECIMAL,      /**< use decimal representation */
665         TVO_OCTAL,        /**< use octal representation */
666         TVO_BINARY,       /**< use binary representation */
667         TVO_FLOAT,        /**< use floating point representation (i.e 1.342e-2)*/
668         TVO_HEXFLOAT      /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
669 } tv_output_mode;
670
671 /**
672  * This structure contains helper information to format the output
673  * of a tarval of a mode.
674  */
675 typedef struct tarval_mode_info {
676         tv_output_mode mode_output;  /**< if != TVO_NATIVE select a special mode */
677         const char *mode_prefix;     /**< if set, this prefix will be printed
678                                           before a value of this mode */
679         const char *mode_suffix;     /**< if set, this suffix will be printed
680                                           after a value of this mode */
681 } tarval_mode_info;
682
683 /**
684  * Specify the output options of one mode.
685  *
686  * This functions stores the mode info, so DO NOT DESTROY it.
687  *
688  * @param mode      a ir_mode that should be associated
689  * @param modeinfo  the output format info
690  *
691  * @return zero on success.
692  */
693 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
694
695 /**
696  * Returns the output options of one mode.
697  *
698  * This functions returns the mode info of a given mode.
699  *
700  * @param mode      a ir_mode that should be associated
701  *
702  * @return the output option
703  */
704 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
705
706 /**
707  * Returns Bit representation of a tarval value, as string of '0' and '1'
708  *
709  * @param tv   The tarval
710  *
711  * This function returns a printable bit representation of any value
712  * stored as tarval. This representation is a null terminated C string.
713  *
714  * @return
715  *   As usual in C a pointer to a char is returned. The length of the
716  *   returned string if fixed, just read as many chars as the mode defines
717  *   as size.
718  *
719  * @note
720  *   The string is allocated using malloc() and is free()ed on the next call
721  *   of this function.
722  *   The string consists of the ASCII characters '0' and '1' and is
723  *   null terminated
724  *
725  * @sa
726  *    irmode.h for the definition of the ir_mode struct
727  *    the size member of aforementioned struct
728  */
729 char *get_tarval_bitpattern(tarval *tv);
730
731 /**
732  * Returns the bitpattern of the bytes_ofs byte.
733  *
734  * This function succeeds even if the mode of the tarval uses lesser bits
735  * than requested, in that case the bitpattern is filled with zero bits.
736  *
737  * To query a 32bit value the following code can be used:
738  *
739  * val0 = tarval_sub_bits(tv, 0);  <- lowest bits
740  * val1 = tarval_sub_bits(tv, 1);
741  * val2 = tarval_sub_bits(tv, 2);
742  * val3 = tarval_sub_bits(tv, 3);  <- highest bits
743  *
744  * Because this is the bit representation of the target machine, only the following
745  * operations are legal on the result:
746  *
747  * - concatenation (endian dependence MUST be handled by the CALLER)
748  * - bitwise logical operations to select/mask bits
749  *
750  * @param tv        the tarval
751  * @param byte_ofs  the byte offset from lower to higher
752  *
753  * @note
754  *   The result of this function is undefined if the mode is neither integer nor float.
755  */
756 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs);
757
758 /**
759  * Returns non-zero if a given (integer) tarval has only one single bit
760  * set.
761  *
762  * @param tv    the tarval
763  */
764 int tarval_is_single_bit(tarval *tv);
765
766 /**
767  * Return the number of set bits in a given (integer) tarval.
768  *
769  * @param tv    the tarval
770  *
771  * @return number of set bits or -1 on error
772  */
773 int get_tarval_popcount(tarval *tv);
774
775 /**
776  * Return the number of the lowest set bit in a given (integer) tarval.
777  *
778  * @param tv    the tarval
779  *
780  * @return number of lowest set bit or -1 on error
781  */
782 int get_tarval_lowest_bit(tarval *tv);
783
784 /**
785  * Output a tarval to a string buffer.
786  *
787  * @param buf     the output buffer
788  * @param buflen  the length of the buffer
789  * @param tv      the tarval
790  */
791 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
792
793 /**
794  * Output a tarval to stdio.
795  *
796  * @param tv    the tarval
797  */
798 int tarval_printf(tarval *tv);
799
800 /**
801  * Returns non-zero if the mantissa of a floating point IEEE-754
802  * tarval is zero (i.e. 1.0Exxx)
803  *
804  * @param tv    the tarval
805  */
806 int tarval_ieee754_zero_mantissa(tarval *tv);
807
808 /**
809  * Returns the exponent of a floating point IEEE-754
810  * tarval.
811  *
812  * @param tv    the tarval
813  */
814 int tarval_ieee754_get_exponent(tarval *tv);
815
816 /**
817  * Check if the tarval can be converted to the given mode without
818  * precision loss.
819  *
820  * @param tv    the tarval
821  * @param mode  the mode to convert to
822  */
823 int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode);
824
825 /**
826  * Set the immediate precision for IEEE-754 results. Set this to
827  * 0 to get the same precision as the operands.
828  * For x87 compatibility, set this to 80.
829  *
830  * @return the old setting
831  */
832 unsigned tarval_ieee754_set_immediate_precision(unsigned bits);
833
834 /**
835  *  Returns non-zero if the result of the last IEEE-754 operation was exact.
836  */
837 unsigned tarval_ieee754_get_exact(void);
838
839 /**
840  * Return the size of the mantissa in bits (including possible
841  * implicit bits) for the given mode.
842  */
843 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode);
844
845 /**
846  * Enable/Disable floating point constant folding.
847  */
848 void tarval_enable_fp_ops(int enable);
849
850 /** returns 0/1 if floating point folding is enable/disabled */
851 int tarval_fp_ops_enabled(void);
852
853 /**
854  * Check if its the a floating point NaN.
855  *
856  * @param tv    the tarval
857  */
858 int tarval_is_NaN(tarval *tv);
859
860 /**
861  * Check if its the a floating point +inf.
862  *
863  * @param tv    the tarval
864  */
865 int tarval_is_plus_inf(tarval *tv);
866
867 /**
868  * Check if its the a floating point -inf.
869  *
870  * @param tv    the tarval
871  */
872 int tarval_is_minus_inf(tarval *tv);
873
874 /**
875  * Check if the tarval represents a finite value, ie neither NaN nor inf.
876  *
877  * @param tv    the tarval
878  */
879 int tarval_is_finite(tarval *tv);
880
881 /**
882  *   Checks whether a pointer points to a tarval.
883  *
884  *   @param thing     an arbitrary pointer
885  *
886  *   @return
887  *       true if the thing is a tarval, else false
888  */
889 int is_tarval(const void *thing);
890
891 #endif  /* FIRM_TV_TV_H */