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"
58 /* ************************ Constructors for tarvals ************************ */
61 * Constructor function for new tarvals.
63 * @param str The string representing the target value
64 * @param len The length of the string
65 * @param mode The mode requested for the result tarval
67 * This function creates a new tarval representing the value represented
68 * by a CString, aka char array. If a tarval representing this value already
69 * exists, this tarval is returned instead of a new one. So tarvals are
70 * directly comparable since their representation is unique.
72 * This function accepts the following strings:
74 * if mode is int_number:
75 * - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
76 * - 0[0-7]* (octal representation)
77 * - (+|-)?[1-9][0-9]* (decimal representation)
79 * if mode is float_number:
80 * - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
82 * if mode is boolean: true, True, TRUE ... False... 0, 1,
84 * if mode is reference: hexadecimal of decimal number as int
86 * if mode is character: hex or dec
88 * Leading and/or trailing spaces are ignored
91 * A tarval of proper type representing the requested value is returned.
92 * Tarvals are unique, so for any value/mode pair at most one tarval will
93 * exist, which will be returned upon further requests with an identical
97 * If the string is not representable in the given mode an assertion is
98 * thrown in assert build.
101 * irmode.h for predefined modes
102 * new_tarval_from_long()
103 * new_tarval_from_double()
105 FIRM_API tarval *new_tarval_from_str(const char *str, size_t len,
109 * Construct a new tarval from a given string.
111 * @param str The string representing the target value
112 * @param len The length of the string
113 * @param sign is -1 or 1 depending on the numbers sign
114 * @param base number system base.
115 * binary(2), octal(8), decimal(10) and hexadecimal(16) numbers
117 * @param mode The mode requested for the result tarval
120 * A tarval with the given mode. If overflow settings are set to
121 * TV_OVERFLOW_BAD then a tarval_bad is returned if the number can't be
122 * represented in the given mode.
123 * Return bad if the number couldn't successfully be parsed.
125 FIRM_API tarval *new_integer_tarval_from_str(const char *str, size_t len,
126 char sign, unsigned char base,
130 * Constructor function for new tarvals
132 * @param l The long representing the value
133 * @param mode The mode requested for the result tarval
135 * This function creates a new tarval representing the value represented
136 * by a long integer. If a tarval representing this value already exists,
137 * this tarval is returned instead of a new one. So tarvals are directly
138 * comparable since their representation is unique.
141 * A tarval of proper type representing the requested value is returned.
142 * Tarvals are unique, so for any value/mode pair at most one tarval will
143 * exist, which will be returned upon further requests with an identical
147 * If the long is not representable in the given mode an assertion is
148 * thrown in assert build.
151 * irmode.h for predefined modes
152 * new_tarval_from_str()
153 * new_tarval_from_double()
156 FIRM_API tarval *new_tarval_from_long(long l, ir_mode *mode);
158 /** Return value as long if possible.
160 * This returns a long int with the value represented value, or
161 * gibberish, depending on the size of long int and the size of the
162 * stored value. It works for e.g. 1 as mode_Ls, but might not work for
163 * get_mode_max(mode_Ls).
164 * This will overflow silently, so use only if you know what
165 * you are doing! (better check with tarval_is_long()...)
166 * Works only for int modes, even not for character modes!
168 FIRM_API long get_tarval_long(tarval *tv);
171 * This validates if get_tarval_long() will return a satisfying
172 * result. I.e. if tv is an int_number and between min, max
173 * of long int (signed!)
175 * @param tv the tarval
177 FIRM_API int tarval_is_long(tarval *tv);
180 * Constructor function for new tarvals.
182 * @param d The (long) double representing the value
183 * @param mode The mode requested for the result tarval
185 * This function creates a new tarval representing the value represented
186 * by a (long) double. If a tarval representing this value already exists,
187 * this tarval is returned instead of a new one. So tarvals are directly
188 * comparable since their representation is unique.
189 * Only modes of sort float_number can be constructed this way.
192 * A tarval of proper type representing the requested value is returned.
193 * Tarvals are unique, so for any value/mode pair at most one tarval will
194 * exist, which will be returned upon further requests with an identical
198 * If the (long) double is not representable in the given mode an assertion
199 * is thrown. This will happen for any mode not of sort float_number.
202 * irmode.h for predefined values
203 * new_tarval_from_str()
204 * new_tarval_from_long()
206 FIRM_API tarval *new_tarval_from_double(long double d, ir_mode *mode);
209 * This returns a double with the value represented value, or
210 * gibberish, depending on the size of double and the size of the
212 * This will overflow silently, so use only if you know what
213 * you are doing! (better check with tarval_is_long...)
215 * @param tv the tarval
217 FIRM_API long double get_tarval_double(tarval *tv);
220 * This validates if tarval_to_double() will return a satisfying
221 * result. I.e. if tv is an float_number and between min, max
224 * @param tv the tarval
226 FIRM_API int tarval_is_double(tarval *tv);
229 /** ********** Access routines for tarval fields ********** **/
237 * ir_mode *get_tarval_mode(tarval *tv)
241 * These are access function for tarval struct members. It is encouraged
242 * to use them instead of direct access to the struct fields.
245 * tv - The tarval to access fields of
248 * get_tv_mode: The mode of the tarval
255 * Returns the mode of the tarval.
257 * @param tv the tarval
259 FIRM_API ir_mode *get_tarval_mode(const tarval *tv);
261 /** Returns the contents of the 'link' field of the tarval */
262 /* void *get_tarval_link (tarval*); */
264 /* Testing properties of the represented values */
267 * Returns 1 if tv is negative
269 * @param tv the tarval
271 FIRM_API int tarval_is_negative(tarval *tv);
274 * Returns 1 if tv is null
276 * @param tv the tarval
278 FIRM_API int tarval_is_null(tarval *tv);
281 * Returns 1 if tv is the "one"
283 * @param tv the tarval
285 FIRM_API int tarval_is_one(tarval *tv);
288 * Returns 1 if tv is the "minus one"
290 * @param tv the tarval
292 FIRM_API int tarval_is_minus_one(tarval *tv);
295 * returns non-zero if all bits in the tarval are set
297 FIRM_API int tarval_is_all_one(tarval *tv);
300 * Return non-zero if the tarval is a constant (ie. NOT
301 * a reserved tarval like bad, undef, reachable etc.)
303 FIRM_API int tarval_is_constant(tarval *tv);
305 /** The 'bad' tarval. */
306 FIRM_API tarval *tarval_bad;
307 /** Returns the 'bad' tarval. */
308 FIRM_API tarval *get_tarval_bad(void);
310 /** The 'undefined' tarval. */
311 FIRM_API tarval *tarval_undefined;
312 /** Returns the 'undefined' tarval. */
313 FIRM_API tarval *get_tarval_undefined(void);
315 /** The mode_b tarval 'false'. */
316 FIRM_API tarval *tarval_b_false;
317 /** Returns the mode_b tarval 'false'. */
318 FIRM_API tarval *get_tarval_b_false(void);
320 /** The mode_b tarval 'true'. */
321 FIRM_API tarval *tarval_b_true;
322 /** Returns the mode_b tarval 'true'. */
323 FIRM_API tarval *get_tarval_b_true(void);
325 /** The mode_X tarval 'unreachable'. */
326 FIRM_API tarval *tarval_unreachable;
327 /** Returns the mode_X tarval 'unreachable'. */
328 FIRM_API tarval *get_tarval_unreachable(void);
330 /** The mode_X tarval 'reachable'. */
331 FIRM_API tarval *tarval_reachable;
332 /** Returns the mode_X tarval 'reachable'. */
333 FIRM_API tarval *get_tarval_reachable(void);
335 /** The 'top' tarval. This is just another name for the 'undefined' tarval. */
336 #define tarval_top tarval_undefined
337 /** Returns the 'top' tarval. */
338 #define get_tarval_top() get_tarval_undefined()
340 /** The 'bottom' tarval. This is just another name for the 'bad' tarval. */
341 #define tarval_bottom tarval_bad
342 /** Returns the 'bottom' tarval. */
343 #define get_tarval_bottom() get_tarval_bad()
345 /* These functions calculate and return a tarval representing the requested
347 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
348 * functions, but these are stored on initialization of the irmode module and
349 * therefore the irmode functions should be preferred to the functions below. */
351 /** Returns the maximum value of a given mode. */
352 FIRM_API tarval *get_tarval_max(ir_mode *mode);
354 /** Returns the minimum value of a given mode. */
355 FIRM_API tarval *get_tarval_min(ir_mode *mode);
357 /** Returns the 0 value (additive neutral) of a given mode.
358 For reference modes, the NULL value is returned (old tarval_P_void) */
359 FIRM_API tarval *get_tarval_null(ir_mode *mode);
361 /** Returns the 1 value (multiplicative neutral) of a given mode. */
362 FIRM_API tarval *get_tarval_one(ir_mode *mode);
364 /** Returns the -1 value (multiplicative neutral) of a given mode.
365 * Returns tarval bad for unsigned modes */
366 FIRM_API tarval *get_tarval_minus_one(ir_mode *mode);
368 /** returns the value where all bits are 1 of a given mode.
369 * returns tarval_bad for float modes */
370 FIRM_API tarval *get_tarval_all_one(ir_mode *mode);
372 /** Return quite nan for float_number modes. */
373 FIRM_API tarval *get_tarval_nan(ir_mode *mode);
375 /** Return +inf for float_number modes. */
376 FIRM_API tarval *get_tarval_plus_inf(ir_mode *mode);
378 /** Return -inf for float_number modes. */
379 FIRM_API tarval *get_tarval_minus_inf(ir_mode *mode);
381 /* ******************** Arithmetic operations on tarvals ******************** */
383 typedef enum _tarval_int_overflow_mode_t {
384 TV_OVERFLOW_BAD, /**< tarval module will return tarval_bad if a overflow occurs */
385 TV_OVERFLOW_WRAP, /**< tarval module will overflow will be ignored, wrap around occurs */
386 TV_OVERFLOW_SATURATE /**< tarval module will saturate the overflow */
387 } tarval_int_overflow_mode_t;
390 * Sets the overflow mode for integer operations.
392 * @param ov_mode one of teh overflow modes
394 FIRM_API void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
397 * Get the overflow mode for integer operations.
399 FIRM_API tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
402 * Compares two tarvals
404 * Compare a with b and return a pn_Cmp describing the relation
405 * between a and b. This is either pn_Cmp_Uo, pn_Cmp_Lt, pn_Cmp_Eq, pn_Cmp_Gt,
406 * or pn_Cmp_False if a or b are symbolic pointers which can not be compared at all.
408 * @param a the first tarval to be compared
409 * @param b the second tarval to be compared
412 * The pn_Cmp best describing the relation between a and b is returned.
413 * This means the mode with the least bits set is returned, e.g. if the
414 * tarvals are equal the pn_Cmp 'pn_Cmp_Eq' is returned, not 'pn_Cmp_Ge' which
415 * indicates 'greater or equal'
418 * irnode.h for the definition of pn_Cmp
420 FIRM_API pn_Cmp tarval_cmp(tarval *a, tarval *b);
423 * Converts a tarval to another mode.
425 * Convert tarval 'src' to mode 'mode', this will succeed if and only if mode
426 * 'mode' is wider than the mode of src, as defined in the firm documentation
427 * and as returned by the function mode_is_smaller defined in irmode.h.
429 * @param src The tarval to convert
430 * @param mode Tho mode to convert to
433 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
434 * tarvals value already exists, it will be returned, else a new tarval is
435 * constructed and returned
438 * Illegal convertions will trigger a panic
441 * FIRM documentation for conversion rules
442 * mode_is_smaller defined in irmode.h
444 FIRM_API tarval *tarval_convert_to(tarval *src, ir_mode *mode);
447 * These function implement basic computations representable as opcodes
453 * a - the tarval to operate on
456 * a - the first operand tarval
457 * b - the second operand tarval
460 * If necessary a new tarval is constructed for the resulting value,
461 * or the one already carrying the computation result is retrieved and
462 * returned as result.
465 * The order the arguments are given in is important, imagine postfix
467 * Illegal operations will trigger an assertion.
468 * The sort member of the struct mode defines which operations are valid
472 * Bitwise Negation of a tarval.
474 * @param a the first tarval
476 * @return ~a or tarval_bad
478 FIRM_API tarval *tarval_not(tarval *a);
481 * Arithmetic Negation of a tarval.
483 * @param a the first tarval
485 * @return -a or tarval_bad
487 FIRM_API tarval *tarval_neg(tarval *a);
490 * Addition of two tarvals.
492 * @param a the first tarval
493 * @param b the second tarval
495 * @return a + b or tarval_bad
497 FIRM_API tarval *tarval_add(tarval *a, tarval *b);
500 * Subtraction from a tarval.
502 * @param a the first tarval
503 * @param b the second tarval
504 * @param dst_mode the mode of the result, needed for mode_P - mode_P, else NULL
506 * @return a - b or tarval_bad
508 FIRM_API tarval *tarval_sub(tarval *a, tarval *b, ir_mode *dst_mode);
511 * Multiplication of tarvals.
513 * @param a the first tarval
514 * @param b the second tarval
516 * @return a * b or tarval_bad
518 FIRM_API tarval *tarval_mul(tarval *a, tarval *b);
521 * Division of two floating point tarvals.
523 * @param a the first tarval
524 * @param b the second tarval
526 * @return a / b or tarval_bad
528 FIRM_API tarval *tarval_quo(tarval *a, tarval *b);
531 * Integer division of two tarvals.
533 * @param a the first tarval
534 * @param b the second tarval
536 * @return a / b or tarval_bad
538 FIRM_API tarval *tarval_div(tarval *a, tarval *b);
541 * Remainder of integer division.
543 * @param a the first tarval
544 * @param b the second tarval
546 * @return a % b or tarval_bad
548 FIRM_API tarval *tarval_mod(tarval *a, tarval *b);
551 * Integer division AND remainder.
553 * @param a the first tarval
554 * @param b the second tarval
555 * @param mod_res after return, contains the remainder result, a % b or tarval_bad
557 * @return a / b or tarval_bad
559 FIRM_API tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod_res);
562 * Absolute value of a tarval.
564 * @param a the first tarval
566 * @return |a| or tarval_bad
568 FIRM_API tarval *tarval_abs(tarval *a);
571 * Bitwise and of two integer tarvals.
573 * @param a the first tarval
574 * @param b the second tarval
576 * @return a & b or tarval_bad
578 FIRM_API tarval *tarval_and(tarval *a, tarval *b);
581 * Bitwise and not of two integer tarvals.
583 * @param a the first tarval
584 * @param b the second tarval
586 * @return a & ~b or tarval_bad
588 FIRM_API tarval *tarval_andnot(tarval *a, tarval *b);
591 * Bitwise or of two integer tarvals.
593 * @param a the first tarval
594 * @param b the second tarval
596 * @return a | b or tarval_bad
598 FIRM_API tarval *tarval_or(tarval *a, tarval *b);
601 * Bitwise exclusive or of two integer tarvals.
603 * @param a the first tarval
604 * @param b the second tarval
606 * @return a ^ b or tarval_bad
608 FIRM_API tarval *tarval_eor(tarval *a, tarval *b);
611 * Logical Left shift.
613 * @param a the first tarval
614 * @param b the second tarval
616 * @return a << b or tarval_bad
618 FIRM_API tarval *tarval_shl(tarval *a, tarval *b);
621 * Unsigned (logical) right shift.
623 * @param a the first tarval
624 * @param b the second tarval
626 * @return a >>u b or tarval_bad
628 FIRM_API tarval *tarval_shr(tarval *a, tarval *b);
631 * Signed (arithmetic) right shift.
633 * @param a the first tarval
634 * @param b the second tarval
636 * @return a >>s b or tarval_bad
638 FIRM_API tarval *tarval_shrs(tarval *a, tarval *b);
643 * @param a the first tarval
644 * @param b the second tarval
646 * @return a \<\<L\>\> b or tarval_bad
648 FIRM_API tarval *tarval_rotl(tarval *a, tarval *b);
651 * Returns the carry flag of the last operation.
653 FIRM_API int tarval_carry(void);
655 /* *********** Output of tarvals *********** */
658 * The output mode for tarval values.
660 * Some modes allow more that one representation, for instance integers
661 * can be represented hex or decimal. Of course it would be enough to have
662 * one and let every backend convert it into the 'right' one.
663 * However, we can do this in the tarval much simpler...
666 TVO_NATIVE, /**< the default output mode, depends on the mode */
667 TVO_HEX, /**< use hex representation, always possible */
668 TVO_DECIMAL, /**< use decimal representation */
669 TVO_OCTAL, /**< use octal representation */
670 TVO_BINARY, /**< use binary representation */
671 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
672 TVO_HEXFLOAT /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
676 * This structure contains helper information to format the output
677 * of a tarval of a mode.
679 typedef struct tarval_mode_info {
680 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
681 const char *mode_prefix; /**< if set, this prefix will be printed
682 before a value of this mode */
683 const char *mode_suffix; /**< if set, this suffix will be printed
684 after a value of this mode */
688 * Specify the output options of one mode.
690 * This functions stores the mode info, so DO NOT DESTROY it.
692 * @param mode a ir_mode that should be associated
693 * @param modeinfo the output format info
695 * @return zero on success.
697 FIRM_API int set_tarval_mode_output_option(ir_mode *mode,
698 const tarval_mode_info *modeinfo);
701 * Returns the output options of one mode.
703 * This functions returns the mode info of a given mode.
705 * @param mode a ir_mode that should be associated
707 * @return the output option
709 FIRM_API const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
712 * Returns Bit representation of a tarval value, as string of '0' and '1'
714 * @param tv The tarval
716 * This function returns a printable bit representation of any value
717 * stored as tarval. This representation is a null terminated C string.
720 * As usual in C a pointer to a char is returned. The length of the
721 * returned string if fixed, just read as many chars as the mode defines
725 * The string is allocated using malloc() and is free()ed on the next call
727 * The string consists of the ASCII characters '0' and '1' and is
731 * irmode.h for the definition of the ir_mode struct
732 * the size member of aforementioned struct
734 FIRM_API char *get_tarval_bitpattern(tarval *tv);
737 * Returns the bitpattern of the bytes_ofs byte.
739 * This function succeeds even if the mode of the tarval uses lesser bits
740 * than requested, in that case the bitpattern is filled with zero bits.
742 * To query a 32bit value the following code can be used:
744 * val0 = tarval_sub_bits(tv, 0); <- lowest bits
745 * val1 = tarval_sub_bits(tv, 1);
746 * val2 = tarval_sub_bits(tv, 2);
747 * val3 = tarval_sub_bits(tv, 3); <- highest bits
749 * Because this is the bit representation of the target machine, only the following
750 * operations are legal on the result:
752 * - concatenation (endian dependence MUST be handled by the CALLER)
753 * - bitwise logical operations to select/mask bits
755 * @param tv the tarval
756 * @param byte_ofs the byte offset from lower to higher
759 * The result of this function is undefined if the mode is neither integer nor float.
761 FIRM_API unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs);
764 * Returns non-zero if a given (integer) tarval has only one single bit
767 * @param tv the tarval
769 FIRM_API int tarval_is_single_bit(tarval *tv);
772 * Return the number of set bits in a given (integer) tarval.
774 * @param tv the tarval
776 * @return number of set bits or -1 on error
778 FIRM_API int get_tarval_popcount(tarval *tv);
781 * Return the number of the lowest set bit in a given (integer) tarval.
783 * @param tv the tarval
785 * @return number of lowest set bit or -1 on error
787 FIRM_API int get_tarval_lowest_bit(tarval *tv);
790 * Output a tarval to a string buffer.
792 * @param buf the output buffer
793 * @param buflen the length of the buffer
794 * @param tv the tarval
796 FIRM_API int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
799 * Output a tarval to stdio.
801 * @param tv the tarval
803 FIRM_API int tarval_printf(tarval *tv);
806 * Returns non-zero if the mantissa of a floating point IEEE-754
807 * tarval is zero (i.e. 1.0Exxx)
809 * @param tv the tarval
811 FIRM_API int tarval_ieee754_zero_mantissa(tarval *tv);
814 * Returns the exponent of a floating point IEEE-754
817 * @param tv the tarval
819 FIRM_API int tarval_ieee754_get_exponent(tarval *tv);
822 * Check if the tarval can be converted to the given mode without
825 * @param tv the tarval
826 * @param mode the mode to convert to
828 FIRM_API int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode);
831 * Set the immediate precision for IEEE-754 results. Set this to
832 * 0 to get the same precision as the operands.
833 * For x87 compatibility, set this to 80.
835 * @return the old setting
837 FIRM_API unsigned tarval_ieee754_set_immediate_precision(unsigned bits);
840 * Returns non-zero if the result of the last IEEE-754 operation was exact.
842 FIRM_API unsigned tarval_ieee754_get_exact(void);
845 * Return the size of the mantissa in bits (including possible
846 * implicit bits) for the given mode.
848 FIRM_API unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode);
851 * Enable/Disable floating point constant folding.
853 FIRM_API void tarval_enable_fp_ops(int enable);
855 /** returns 0/1 if floating point folding is enable/disabled */
856 FIRM_API int tarval_fp_ops_enabled(void);
859 * Check if its the a floating point NaN.
861 * @param tv the tarval
863 FIRM_API int tarval_is_NaN(tarval *tv);
866 * Check if its the a floating point +inf.
868 * @param tv the tarval
870 FIRM_API int tarval_is_plus_inf(tarval *tv);
873 * Check if its the a floating point -inf.
875 * @param tv the tarval
877 FIRM_API int tarval_is_minus_inf(tarval *tv);
880 * Check if the tarval represents a finite value, ie neither NaN nor inf.
882 * @param tv the tarval
884 FIRM_API int tarval_is_finite(tarval *tv);
887 * Checks whether a pointer points to a tarval.
889 * @param thing an arbitrary pointer
892 * true if the thing is a tarval, else false
894 FIRM_API int is_tarval(const void *thing);