2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Representation of and static computations on target machine
25 * @author Mathias Heil
28 * Tarvals represent target machine values. They are typed by modes.
29 * Tarvals only represent values of mode_sort:
36 * In case of references the module accepts an entity to represent the
38 * Furthermore, computations and conversions of these values can
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 ...
47 * irmode.h for the modes definitions
48 * irnode.h for the pn_Cmp table
53 #include "firm_types.h"
56 /* ************************ Constructors for tarvals ************************ */
59 * Constructor function for new tarvals.
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
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.
70 * This function accepts the following strings:
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)
77 * if mode is float_number:
78 * - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
80 * if mode is boolean: true, True, TRUE ... False... 0, 1,
82 * if mode is reference: hexadecimal of decimal number as int
84 * if mode is character: hex or dec
86 * Leading and/or trailing spaces are ignored
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
95 * If the string is not representable in the given mode an assertion is
96 * thrown in assert build.
99 * irmode.h for predefined modes
100 * new_tarval_from_long()
101 * new_tarval_from_double()
103 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
106 * Construct a new tarval from a given string.
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
114 * @param mode The mode requested for the result tarval
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.
122 tarval *new_integer_tarval_from_str(const char *str, size_t len, char sign,
123 unsigned char base, ir_mode *mode);
126 * Constructor function for new tarvals
128 * @param l The long representing the value
129 * @param mode The mode requested for the result tarval
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.
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
143 * If the long is not representable in the given mode an assertion is
144 * thrown in assert build.
147 * irmode.h for predefined modes
148 * new_tarval_from_str()
149 * new_tarval_from_double()
152 tarval *new_tarval_from_long(long l, ir_mode *mode);
154 /** Return value as long if possible.
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!
164 long get_tarval_long(tarval *tv);
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!)
171 * @param tv the tarval
173 int tarval_is_long(tarval *tv);
176 * Constructor function for new tarvals.
178 * @param d The (long) double representing the value
179 * @param mode The mode requested for the result tarval
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.
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
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.
198 * irmode.h for predefined values
199 * new_tarval_from_str()
200 * new_tarval_from_long()
202 tarval *new_tarval_from_double(long double d, ir_mode *mode);
205 * This returns a double with the value represented value, or
206 * gibberish, depending on the size of double and the size of the
208 * This will overflow silently, so use only if you know what
209 * you are doing! (better check with tarval_is_long...)
211 * @param tv the tarval
213 long double get_tarval_double(tarval *tv);
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
220 * @param tv the tarval
222 int tarval_is_double(tarval *tv);
225 /** ********** Access routines for tarval fields ********** **/
233 * ir_mode *get_tarval_mode(tarval *tv)
237 * These are access function for tarval struct members. It is encouraged
238 * to use them instead of direct access to the struct fields.
241 * tv - The tarval to access fields of
244 * get_tv_mode: The mode of the tarval
251 * Returns the mode of the tarval.
253 * @param tv the tarval
255 ir_mode *get_tarval_mode(const tarval *tv);
257 /** Returns the contents of the 'link' field of the tarval */
258 /* void *get_tarval_link (tarval*); */
260 /* Testing properties of the represented values */
263 * Returns 1 if tv is negative
265 * @param tv the tarval
267 int tarval_is_negative(tarval *tv);
270 * Returns 1 if tv is null
272 * @param tv the tarval
274 int tarval_is_null(tarval *tv);
277 * Returns 1 if tv is the "one"
279 * @param tv the tarval
281 int tarval_is_one(tarval *tv);
284 * Returns 1 if tv is the "minus one"
286 * @param tv the tarval
288 int tarval_is_minus_one(tarval *tv);
291 * returns non-zero if all bits in the tarval are set
293 int tarval_is_all_one(tarval *tv);
296 * Return non-zero if the tarval is a constant (ie. NOT
297 * a reserved tarval like bad, undef, reachable etc.)
299 int tarval_is_constant(tarval *tv);
301 /** The 'bad' tarval. */
302 extern tarval *tarval_bad;
303 /** Returns the 'bad' tarval. */
304 tarval *get_tarval_bad(void);
306 /** The 'undefined' tarval. */
307 extern tarval *tarval_undefined;
308 /** Returns the 'undefined' tarval. */
309 tarval *get_tarval_undefined(void);
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);
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);
321 /** The mode_X tarval 'unreachable'. */
322 extern tarval *tarval_unreachable;
323 /** Returns the mode_X tarval 'unreachable'. */
324 tarval *get_tarval_unreachable(void);
326 /** The mode_X tarval 'reachable'. */
327 extern tarval *tarval_reachable;
328 /** Returns the mode_X tarval 'reachable'. */
329 tarval *get_tarval_reachable(void);
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()
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()
341 /* These functions calculate and return a tarval representing the requested
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. */
347 /** Returns the maximum value of a given mode. */
348 tarval *get_tarval_max(ir_mode *mode);
350 /** Returns the minimum value of a given mode. */
351 tarval *get_tarval_min(ir_mode *mode);
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);
357 /** Returns the 1 value (multiplicative neutral) of a given mode. */
358 tarval *get_tarval_one(ir_mode *mode);
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);
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);
368 /** Return quite nan for float_number modes. */
369 tarval *get_tarval_nan(ir_mode *mode);
371 /** Return +inf for float_number modes. */
372 tarval *get_tarval_plus_inf(ir_mode *mode);
374 /** Return -inf for float_number modes. */
375 tarval *get_tarval_minus_inf(ir_mode *mode);
377 /* ******************** Arithmetic operations on tarvals ******************** */
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;
386 * Sets the overflow mode for integer operations.
388 * @param ov_mode one of teh overflow modes
390 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
393 * Get the overflow mode for integer operations.
395 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
398 * Compares two tarvals
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.
404 * @param a the first tarval to be compared
405 * @param b the second tarval to be compared
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'
414 * irnode.h for the definition of pn_Cmp
416 pn_Cmp tarval_cmp(tarval *a, tarval *b);
419 * Converts a tarval to another mode.
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.
425 * @param src The tarval to convert
426 * @param mode Tho mode to convert to
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
434 * Illegal convertions will trigger a panic
437 * FIRM documentation for conversion rules
438 * mode_is_smaller defined in irmode.h
440 tarval *tarval_convert_to(tarval *src, ir_mode *mode);
443 * These function implement basic computations representable as opcodes
449 * a - the tarval to operate on
452 * a - the first operand tarval
453 * b - the second operand tarval
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.
461 * The order the arguments are given in is important, imagine postfix
463 * Illegal operations will trigger an assertion.
464 * The sort member of the struct mode defines which operations are valid
468 * Bitwise Negation of a tarval.
470 * @param a the first tarval
472 * @return ~a or tarval_bad
474 tarval *tarval_not(tarval *a);
477 * Arithmetic Negation of a tarval.
479 * @param a the first tarval
481 * @return -a or tarval_bad
483 tarval *tarval_neg(tarval *a);
486 * Addition of two tarvals.
488 * @param a the first tarval
489 * @param b the second tarval
491 * @return a + b or tarval_bad
493 tarval *tarval_add(tarval *a, tarval *b);
496 * Subtraction from a tarval.
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
502 * @return a - b or tarval_bad
504 tarval *tarval_sub(tarval *a, tarval *b, ir_mode *dst_mode);
507 * Multiplication of tarvals.
509 * @param a the first tarval
510 * @param b the second tarval
512 * @return a * b or tarval_bad
514 tarval *tarval_mul(tarval *a, tarval *b);
517 * Division of two floating point tarvals.
519 * @param a the first tarval
520 * @param b the second tarval
522 * @return a / b or tarval_bad
524 tarval *tarval_quo(tarval *a, tarval *b);
527 * Integer division of two tarvals.
529 * @param a the first tarval
530 * @param b the second tarval
532 * @return a / b or tarval_bad
534 tarval *tarval_div(tarval *a, tarval *b);
537 * Remainder of integer division.
539 * @param a the first tarval
540 * @param b the second tarval
542 * @return a % b or tarval_bad
544 tarval *tarval_mod(tarval *a, tarval *b);
547 * Integer division AND remainder.
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
553 * @return a / b or tarval_bad
555 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod_res);
558 * Absolute value of a tarval.
560 * @param a the first tarval
562 * @return |a| or tarval_bad
564 tarval *tarval_abs(tarval *a);
567 * Bitwise and of two integer tarvals.
569 * @param a the first tarval
570 * @param b the second tarval
572 * @return a & b or tarval_bad
574 tarval *tarval_and(tarval *a, tarval *b);
577 * Bitwise and not of two integer tarvals.
579 * @param a the first tarval
580 * @param b the second tarval
582 * @return a & ~b or tarval_bad
584 tarval *tarval_andnot(tarval *a, tarval *b);
587 * Bitwise or of two integer tarvals.
589 * @param a the first tarval
590 * @param b the second tarval
592 * @return a | b or tarval_bad
594 tarval *tarval_or(tarval *a, tarval *b);
597 * Bitwise exclusive or of two integer tarvals.
599 * @param a the first tarval
600 * @param b the second tarval
602 * @return a ^ b or tarval_bad
604 tarval *tarval_eor(tarval *a, tarval *b);
607 * Logical Left shift.
609 * @param a the first tarval
610 * @param b the second tarval
612 * @return a << b or tarval_bad
614 tarval *tarval_shl(tarval *a, tarval *b);
617 * Unsigned (logical) right shift.
619 * @param a the first tarval
620 * @param b the second tarval
622 * @return a >>u b or tarval_bad
624 tarval *tarval_shr(tarval *a, tarval *b);
627 * Signed (arithmetic) right shift.
629 * @param a the first tarval
630 * @param b the second tarval
632 * @return a >>s b or tarval_bad
634 tarval *tarval_shrs(tarval *a, tarval *b);
639 * @param a the first tarval
640 * @param b the second tarval
642 * @return a \<\<L\>\> b or tarval_bad
644 tarval *tarval_rotl(tarval *a, tarval *b);
647 * Returns the carry flag of the last operation.
649 int tarval_carry(void);
651 /* *********** Output of tarvals *********** */
654 * The output mode for tarval values.
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...
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)*/
672 * This structure contains helper information to format the output
673 * of a tarval of a mode.
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 */
684 * Specify the output options of one mode.
686 * This functions stores the mode info, so DO NOT DESTROY it.
688 * @param mode a ir_mode that should be associated
689 * @param modeinfo the output format info
691 * @return zero on success.
693 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
696 * Returns the output options of one mode.
698 * This functions returns the mode info of a given mode.
700 * @param mode a ir_mode that should be associated
702 * @return the output option
704 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
707 * Returns Bit representation of a tarval value, as string of '0' and '1'
709 * @param tv The tarval
711 * This function returns a printable bit representation of any value
712 * stored as tarval. This representation is a null terminated C string.
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
720 * The string is allocated using malloc() and is free()ed on the next call
722 * The string consists of the ASCII characters '0' and '1' and is
726 * irmode.h for the definition of the ir_mode struct
727 * the size member of aforementioned struct
729 char *get_tarval_bitpattern(tarval *tv);
732 * Returns the bitpattern of the bytes_ofs byte.
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.
737 * To query a 32bit value the following code can be used:
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
744 * Because this is the bit representation of the target machine, only the following
745 * operations are legal on the result:
747 * - concatenation (endian dependence MUST be handled by the CALLER)
748 * - bitwise logical operations to select/mask bits
750 * @param tv the tarval
751 * @param byte_ofs the byte offset from lower to higher
754 * The result of this function is undefined if the mode is neither integer nor float.
756 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs);
759 * Returns non-zero if a given (integer) tarval has only one single bit
762 * @param tv the tarval
764 int tarval_is_single_bit(tarval *tv);
767 * Return the number of set bits in a given (integer) tarval.
769 * @param tv the tarval
771 * @return number of set bits or -1 on error
773 int get_tarval_popcnt(tarval *tv);
776 * Return the number of the lowest set bit in a given (integer) tarval.
778 * @param tv the tarval
780 * @return number of lowest set bit or -1 on error
782 int get_tarval_lowest_bit(tarval *tv);
785 * Output a tarval to a string buffer.
787 * @param buf the output buffer
788 * @param buflen the length of the buffer
789 * @param tv the tarval
791 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
794 * Output a tarval to stdio.
796 * @param tv the tarval
798 int tarval_printf(tarval *tv);
801 * Returns non-zero if the mantissa of a floating point IEEE-754
802 * tarval is zero (i.e. 1.0Exxx)
804 * @param tv the tarval
806 int tarval_ieee754_zero_mantissa(tarval *tv);
809 * Returns the exponent of a floating point IEEE-754
812 * @param tv the tarval
814 int tarval_ieee754_get_exponent(tarval *tv);
817 * Check if the tarval can be converted to the given mode without
820 * @param tv the tarval
821 * @param mode the mode to convert to
823 int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode);
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.
830 * @return the old setting
832 unsigned tarval_ieee754_set_immediate_precision(unsigned bits);
835 * Returns non-zero if the result of the last IEEE-754 operation was exact.
837 unsigned tarval_ieee754_get_exact(void);
840 * Return the size of the mantissa in bits (including possible
841 * implicit bits) for the given mode.
843 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode);
846 * Enable/Disable floating point constant folding.
848 void tarval_enable_fp_ops(int enable);
850 /** returns 0/1 if floating point folding is enable/disabled */
851 int tarval_fp_ops_enabled(void);
854 * Check if its the a floating point NaN.
856 * @param tv the tarval
858 int tarval_is_NaN(tarval *tv);
861 * Check if its the a floating point +inf.
863 * @param tv the tarval
865 int tarval_is_plus_inf(tarval *tv);
868 * Check if its the a floating point -inf.
870 * @param tv the tarval
872 int tarval_is_minus_inf(tarval *tv);
875 * Check if the tarval represents a finite value, ie neither NaN nor inf.
877 * @param tv the tarval
879 int tarval_is_finite(tarval *tv);
882 * Checks whether a pointer points to a tarval.
884 * @param thing an arbitrary pointer
887 * true if the thing is a tarval, else false
889 int is_tarval(const void *thing);
891 #endif /* FIRM_TV_TV_H */