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 ir_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 ir_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 ir_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(ir_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(ir_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 ir_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(ir_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(ir_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 ir_tarval *tv);
262 * Returns 1 if tv is negative
264 * @param tv the tarval
266 FIRM_API int tarval_is_negative(ir_tarval *tv);
269 * Returns 1 if tv is null
271 * @param tv the tarval
273 FIRM_API int tarval_is_null(ir_tarval *tv);
276 * Returns 1 if tv is the "one"
278 * @param tv the tarval
280 FIRM_API int tarval_is_one(ir_tarval *tv);
283 * Returns 1 if tv is the "minus one"
285 * @param tv the tarval
287 FIRM_API int tarval_is_minus_one(ir_tarval *tv);
290 * returns non-zero if all bits in the tarval are set
292 FIRM_API int tarval_is_all_one(ir_tarval *tv);
295 * Return non-zero if the tarval is a constant (ie. NOT
296 * a reserved tarval like bad, undef, reachable etc.)
298 FIRM_API int tarval_is_constant(ir_tarval *tv);
300 /** The 'bad' tarval. */
301 FIRM_API ir_tarval *tarval_bad;
302 /** Returns the 'bad' tarval. */
303 FIRM_API ir_tarval *get_tarval_bad(void);
305 /** The 'undefined' tarval. */
306 FIRM_API ir_tarval *tarval_undefined;
307 /** Returns the 'undefined' tarval. */
308 FIRM_API ir_tarval *get_tarval_undefined(void);
310 /** The mode_b tarval 'false'. */
311 FIRM_API ir_tarval *tarval_b_false;
312 /** Returns the mode_b tarval 'false'. */
313 FIRM_API ir_tarval *get_tarval_b_false(void);
315 /** The mode_b tarval 'true'. */
316 FIRM_API ir_tarval *tarval_b_true;
317 /** Returns the mode_b tarval 'true'. */
318 FIRM_API ir_tarval *get_tarval_b_true(void);
320 /** The mode_X tarval 'unreachable'. */
321 FIRM_API ir_tarval *tarval_unreachable;
322 /** Returns the mode_X tarval 'unreachable'. */
323 FIRM_API ir_tarval *get_tarval_unreachable(void);
325 /** The mode_X tarval 'reachable'. */
326 FIRM_API ir_tarval *tarval_reachable;
327 /** Returns the mode_X tarval 'reachable'. */
328 FIRM_API ir_tarval *get_tarval_reachable(void);
330 /** The 'top' tarval. This is just another name for the 'undefined' tarval. */
331 #define tarval_top tarval_undefined
332 /** Returns the 'top' tarval. */
333 #define get_tarval_top() get_tarval_undefined()
335 /** The 'bottom' tarval. This is just another name for the 'bad' tarval. */
336 #define tarval_bottom tarval_bad
337 /** Returns the 'bottom' tarval. */
338 #define get_tarval_bottom() get_tarval_bad()
340 /* These functions calculate and return a tarval representing the requested
342 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
343 * functions, but these are stored on initialization of the irmode module and
344 * therefore the irmode functions should be preferred to the functions below. */
346 /** Returns the maximum value of a given mode. */
347 FIRM_API ir_tarval *get_tarval_max(ir_mode *mode);
349 /** Returns the minimum value of a given mode. */
350 FIRM_API ir_tarval *get_tarval_min(ir_mode *mode);
352 /** Returns the 0 value (additive neutral) of a given mode.
353 For reference modes, the NULL value is returned (old tarval_P_void) */
354 FIRM_API ir_tarval *get_tarval_null(ir_mode *mode);
356 /** Returns the 1 value (multiplicative neutral) of a given mode. */
357 FIRM_API ir_tarval *get_tarval_one(ir_mode *mode);
359 /** Returns the -1 value (multiplicative neutral) of a given mode.
360 * Returns tarval bad for unsigned modes */
361 FIRM_API ir_tarval *get_tarval_minus_one(ir_mode *mode);
363 /** returns the value where all bits are 1 of a given mode.
364 * returns tarval_bad for float modes */
365 FIRM_API ir_tarval *get_tarval_all_one(ir_mode *mode);
367 /** Return quite nan for float_number modes. */
368 FIRM_API ir_tarval *get_tarval_nan(ir_mode *mode);
370 /** Return +inf for float_number modes. */
371 FIRM_API ir_tarval *get_tarval_plus_inf(ir_mode *mode);
373 /** Return -inf for float_number modes. */
374 FIRM_API ir_tarval *get_tarval_minus_inf(ir_mode *mode);
376 /* ******************** Arithmetic operations on tarvals ******************** */
378 typedef enum tarval_int_overflow_mode_t {
379 TV_OVERFLOW_BAD, /**< tarval module will return tarval_bad if a overflow occurs */
380 TV_OVERFLOW_WRAP, /**< tarval module will overflow will be ignored, wrap around occurs */
381 TV_OVERFLOW_SATURATE /**< tarval module will saturate the overflow */
382 } tarval_int_overflow_mode_t;
385 * Sets the overflow mode for integer operations.
387 * @param ov_mode one of teh overflow modes
389 FIRM_API void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
392 * Get the overflow mode for integer operations.
394 FIRM_API tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
397 * Compares two tarvals
399 * Compare a with b and return a pn_Cmp describing the relation
400 * between a and b. This is either pn_Cmp_Uo, pn_Cmp_Lt, pn_Cmp_Eq, pn_Cmp_Gt,
401 * or pn_Cmp_False if a or b are symbolic pointers which can not be compared at all.
403 * @param a the first tarval to be compared
404 * @param b the second tarval to be compared
407 * The pn_Cmp best describing the relation between a and b is returned.
408 * This means the mode with the least bits set is returned, e.g. if the
409 * tarvals are equal the pn_Cmp 'pn_Cmp_Eq' is returned, not 'pn_Cmp_Ge' which
410 * indicates 'greater or equal'
413 * irnode.h for the definition of pn_Cmp
415 FIRM_API pn_Cmp tarval_cmp(ir_tarval *a, ir_tarval *b);
418 * Converts a tarval to another mode.
420 * Convert tarval 'src' to mode 'mode', this will succeed if and only if mode
421 * 'mode' is wider than the mode of src, as defined in the firm documentation
422 * and as returned by the function mode_is_smaller defined in irmode.h.
424 * @param src The tarval to convert
425 * @param mode Tho mode to convert to
428 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
429 * tarvals value already exists, it will be returned, else a new tarval is
430 * constructed and returned
433 * Illegal convertions will trigger a panic
436 * FIRM documentation for conversion rules
437 * mode_is_smaller defined in irmode.h
439 FIRM_API ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *mode);
442 * These function implement basic computations representable as opcodes
448 * a - the tarval to operate on
451 * a - the first operand tarval
452 * b - the second operand tarval
455 * If necessary a new tarval is constructed for the resulting value,
456 * or the one already carrying the computation result is retrieved and
457 * returned as result.
460 * The order the arguments are given in is important, imagine postfix
462 * Illegal operations will trigger an assertion.
463 * The sort member of the struct mode defines which operations are valid
467 * Bitwise Negation of a tarval.
469 * @param a the first tarval
471 * @return ~a or tarval_bad
473 FIRM_API ir_tarval *tarval_not(ir_tarval *a);
476 * Arithmetic Negation of a tarval.
478 * @param a the first tarval
480 * @return -a or tarval_bad
482 FIRM_API ir_tarval *tarval_neg(ir_tarval *a);
485 * Addition of two tarvals.
487 * @param a the first tarval
488 * @param b the second tarval
490 * @return a + b or tarval_bad
492 FIRM_API ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b);
495 * Subtraction from a tarval.
497 * @param a the first tarval
498 * @param b the second tarval
499 * @param dst_mode the mode of the result, needed for mode_P - mode_P, else NULL
501 * @return a - b or tarval_bad
503 FIRM_API ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode);
506 * Multiplication of tarvals.
508 * @param a the first tarval
509 * @param b the second tarval
511 * @return a * b or tarval_bad
513 FIRM_API ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b);
516 * Division of two floating point tarvals.
518 * @param a the first tarval
519 * @param b the second tarval
521 * @return a / b or tarval_bad
523 FIRM_API ir_tarval *tarval_quo(ir_tarval *a, ir_tarval *b);
526 * Integer division of two tarvals.
528 * @param a the first tarval
529 * @param b the second tarval
531 * @return a / b or tarval_bad
533 FIRM_API ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b);
536 * Remainder of integer division.
538 * @param a the first tarval
539 * @param b the second tarval
541 * @return a % b or tarval_bad
543 FIRM_API ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b);
546 * Integer division AND remainder.
548 * @param a the first tarval
549 * @param b the second tarval
550 * @param mod_res after return, contains the remainder result, a % b or tarval_bad
552 * @return a / b or tarval_bad
554 FIRM_API ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod_res);
557 * Absolute value of a tarval.
559 * @param a the first tarval
561 * @return |a| or tarval_bad
563 FIRM_API ir_tarval *tarval_abs(ir_tarval *a);
566 * Bitwise and of two integer tarvals.
568 * @param a the first tarval
569 * @param b the second tarval
571 * @return a & b or tarval_bad
573 FIRM_API ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b);
576 * Bitwise and not of two integer tarvals.
578 * @param a the first tarval
579 * @param b the second tarval
581 * @return a & ~b or tarval_bad
583 FIRM_API ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b);
586 * Bitwise or of two integer tarvals.
588 * @param a the first tarval
589 * @param b the second tarval
591 * @return a | b or tarval_bad
593 FIRM_API ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b);
596 * Bitwise exclusive or of two integer tarvals.
598 * @param a the first tarval
599 * @param b the second tarval
601 * @return a ^ b or tarval_bad
603 FIRM_API ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b);
606 * Logical Left shift.
608 * @param a the first tarval
609 * @param b the second tarval
611 * @return a << b or tarval_bad
613 FIRM_API ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b);
616 * Unsigned (logical) right shift.
618 * @param a the first tarval
619 * @param b the second tarval
621 * @return a >>u b or tarval_bad
623 FIRM_API ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b);
626 * Signed (arithmetic) right shift.
628 * @param a the first tarval
629 * @param b the second tarval
631 * @return a >>s b or tarval_bad
633 FIRM_API ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b);
638 * @param a the first tarval
639 * @param b the second tarval
641 * @return a \<\<L\>\> b or tarval_bad
643 FIRM_API ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b);
646 * Returns the carry flag of the last operation.
648 FIRM_API int tarval_carry(void);
650 /* *********** Output of tarvals *********** */
653 * The output mode for tarval values.
655 * Some modes allow more that one representation, for instance integers
656 * can be represented hex or decimal. Of course it would be enough to have
657 * one and let every backend convert it into the 'right' one.
658 * However, we can do this in the tarval much simpler...
661 TVO_NATIVE, /**< the default output mode, depends on the mode */
662 TVO_HEX, /**< use hex representation, always possible */
663 TVO_DECIMAL, /**< use decimal representation */
664 TVO_OCTAL, /**< use octal representation */
665 TVO_BINARY, /**< use binary representation */
666 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
667 TVO_HEXFLOAT /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
671 * This structure contains helper information to format the output
672 * of a tarval of a mode.
674 typedef struct tarval_mode_info {
675 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
676 const char *mode_prefix; /**< if set, this prefix will be printed
677 before a value of this mode */
678 const char *mode_suffix; /**< if set, this suffix will be printed
679 after a value of this mode */
683 * Specify the output options of one mode.
685 * This functions stores the mode info, so DO NOT DESTROY it.
687 * @param mode a ir_mode that should be associated
688 * @param modeinfo the output format info
690 * @return zero on success.
692 FIRM_API int set_tarval_mode_output_option(ir_mode *mode,
693 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 FIRM_API 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 FIRM_API char *get_tarval_bitpattern(ir_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 FIRM_API unsigned char get_tarval_sub_bits(ir_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 FIRM_API int tarval_is_single_bit(ir_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 FIRM_API int get_tarval_popcount(ir_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 FIRM_API int get_tarval_lowest_bit(ir_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 FIRM_API int tarval_snprintf(char *buf, size_t buflen, ir_tarval *tv);
794 * Output a tarval to stdio.
796 * @param tv the tarval
798 FIRM_API int tarval_printf(ir_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 FIRM_API int tarval_ieee754_zero_mantissa(ir_tarval *tv);
809 * Returns the exponent of a floating point IEEE-754
812 * @param tv the tarval
814 FIRM_API int tarval_ieee754_get_exponent(ir_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 FIRM_API int tarval_ieee754_can_conv_lossless(ir_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 FIRM_API unsigned tarval_ieee754_set_immediate_precision(unsigned bits);
835 * Returns non-zero if the result of the last IEEE-754 operation was exact.
837 FIRM_API 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 FIRM_API unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode);
846 * Enable/Disable floating point constant folding.
848 FIRM_API void tarval_enable_fp_ops(int enable);
850 /** returns 0/1 if floating point folding is enable/disabled */
851 FIRM_API int tarval_fp_ops_enabled(void);
854 * Check if its the a floating point NaN.
856 * @param tv the tarval
858 FIRM_API int tarval_is_NaN(ir_tarval *tv);
861 * Check if its the a floating point +inf.
863 * @param tv the tarval
865 FIRM_API int tarval_is_plus_inf(ir_tarval *tv);
868 * Check if its the a floating point -inf.
870 * @param tv the tarval
872 FIRM_API int tarval_is_minus_inf(ir_tarval *tv);
875 * Check if the tarval represents a finite value, ie neither NaN nor inf.
877 * @param tv the tarval
879 FIRM_API int tarval_is_finite(ir_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 FIRM_API int is_tarval(const void *thing);