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
53 #include "firm_types.h"
57 /* ************************ Constructors for tarvals ************************ */
60 * Constructor function for new tarvals.
62 * @param str The string representing the target value
63 * @param len The length of the string
64 * @param mode The mode requested for the result tarval
66 * This function creates a new tarval representing the value represented
67 * by a CString, aka char array. If a tarval representing this value already
68 * exists, this tarval is returned instead of a new one. So tarvals are
69 * directly comparable since their representation is unique.
71 * This function accepts the following strings:
73 * if mode is int_number:
74 * - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
75 * - 0[0-7]* (octal representation)
76 * - (+|-)?[1-9][0-9]* (decimal representation)
78 * if mode is float_number:
79 * - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
81 * if mode is boolean: true, True, TRUE ... False... 0, 1,
83 * if mode is reference: hexadecimal of decimal number as int
85 * if mode is character: hex or dec
87 * Leading and/or trailing spaces are ignored
90 * A tarval of proper type representing the requested value is returned.
91 * Tarvals are unique, so for any value/mode pair at most one tarval will
92 * exist, which will be returned upon further requests with an identical
96 * If the string is not representable in the given mode an assertion is
97 * thrown in assert build.
100 * irmode.h for predefined modes
101 * new_tarval_from_long()
102 * new_tarval_from_double()
104 FIRM_API ir_tarval *new_tarval_from_str(const char *str, size_t len,
108 * Construct a new tarval from a given string.
110 * @param str The string representing the target value
111 * @param len The length of the string
112 * @param sign is -1 or 1 depending on the numbers sign
113 * @param base number system base.
114 * binary(2), octal(8), decimal(10) and hexadecimal(16) numbers
116 * @param mode The mode requested for the result tarval
119 * A tarval with the given mode. If overflow settings are set to
120 * TV_OVERFLOW_BAD then a tarval_bad is returned if the number can't be
121 * represented in the given mode.
122 * Return bad if the number couldn't successfully be parsed.
124 FIRM_API ir_tarval *new_integer_tarval_from_str(const char *str, size_t len,
125 char sign, unsigned char base,
129 * Constructor function for new tarvals
131 * @param l The long representing the value
132 * @param mode The mode requested for the result tarval
134 * This function creates a new tarval representing the value represented
135 * by a long integer. If a tarval representing this value already exists,
136 * this tarval is returned instead of a new one. So tarvals are directly
137 * comparable since their representation is unique.
140 * A tarval of proper type representing the requested value is returned.
141 * Tarvals are unique, so for any value/mode pair at most one tarval will
142 * exist, which will be returned upon further requests with an identical
146 * If the long is not representable in the given mode an assertion is
147 * thrown in assert build.
150 * irmode.h for predefined modes
151 * new_tarval_from_str()
152 * new_tarval_from_double()
155 FIRM_API ir_tarval *new_tarval_from_long(long l, ir_mode *mode);
157 /** Return value as long if possible.
159 * This returns a long int with the value represented value, or
160 * gibberish, depending on the size of long int and the size of the
161 * stored value. It works for e.g. 1 as mode_Ls, but might not work for
162 * get_mode_max(mode_Ls).
163 * This will overflow silently, so use only if you know what
164 * you are doing! (better check with tarval_is_long()...)
165 * Works only for int modes, even not for character modes!
167 FIRM_API long get_tarval_long(ir_tarval *tv);
170 * This validates if get_tarval_long() will return a satisfying
171 * result. I.e. if tv is an int_number and between min, max
172 * of long int (signed!)
174 * @param tv the tarval
176 FIRM_API int tarval_is_long(ir_tarval *tv);
179 * Constructor function for new tarvals.
181 * @param d The (long) double representing the value
182 * @param mode The mode requested for the result tarval
184 * This function creates a new tarval representing the value represented
185 * by a (long) double. If a tarval representing this value already exists,
186 * this tarval is returned instead of a new one. So tarvals are directly
187 * comparable since their representation is unique.
188 * Only modes of sort float_number can be constructed this way.
191 * A tarval of proper type representing the requested value is returned.
192 * Tarvals are unique, so for any value/mode pair at most one tarval will
193 * exist, which will be returned upon further requests with an identical
197 * If the (long) double is not representable in the given mode an assertion
198 * is thrown. This will happen for any mode not of sort float_number.
201 * irmode.h for predefined values
202 * new_tarval_from_str()
203 * new_tarval_from_long()
205 FIRM_API ir_tarval *new_tarval_from_double(double d, ir_mode *mode);
208 * same as new_tarval_from_double(), but with a long double argument
210 FIRM_API ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode);
213 * This returns a double with the value represented value, or
214 * gibberish, depending on the size of double and the size of the
216 * This will overflow silently, so use only if you know what
217 * you are doing! (better check with tarval_is_long...)
219 * @param tv the tarval
221 FIRM_API double get_tarval_double(ir_tarval *tv);
224 * same as get_tarval_double but returns a long double value
226 FIRM_API long double get_tarval_long_double(ir_tarval *tv);
229 * This validates if tarval_to_double() will return a satisfying
230 * result. I.e. if tv is an float_number and between min, max
233 * @param tv the tarval
235 FIRM_API int tarval_is_double(ir_tarval *tv);
238 /** ********** Access routines for tarval fields ********** **/
246 * ir_mode *get_tarval_mode(tarval *tv)
250 * These are access function for tarval struct members. It is encouraged
251 * to use them instead of direct access to the struct fields.
254 * tv - The tarval to access fields of
257 * get_tv_mode: The mode of the tarval
264 * Returns the mode of the tarval.
266 * @param tv the tarval
268 FIRM_API ir_mode *get_tarval_mode(const ir_tarval *tv);
271 * Returns 1 if tv is negative
273 * @param tv the tarval
275 FIRM_API int tarval_is_negative(ir_tarval *tv);
278 * Returns 1 if tv is null
280 * @param tv the tarval
282 FIRM_API int tarval_is_null(ir_tarval *tv);
285 * Returns 1 if tv is the "one"
287 * @param tv the tarval
289 FIRM_API int tarval_is_one(ir_tarval *tv);
292 * Returns 1 if tv is the "minus one"
294 * @param tv the tarval
296 FIRM_API int tarval_is_minus_one(ir_tarval *tv);
299 * returns non-zero if all bits in the tarval are set
301 FIRM_API int tarval_is_all_one(ir_tarval *tv);
304 * Return non-zero if the tarval is a constant (ie. NOT
305 * a reserved tarval like bad, undef, reachable etc.)
307 FIRM_API int tarval_is_constant(ir_tarval *tv);
309 /** The 'bad' tarval. */
310 FIRM_API ir_tarval *tarval_bad;
311 /** Returns the 'bad' tarval. */
312 FIRM_API ir_tarval *get_tarval_bad(void);
314 /** The 'undefined' tarval. */
315 FIRM_API ir_tarval *tarval_undefined;
316 /** Returns the 'undefined' tarval. */
317 FIRM_API ir_tarval *get_tarval_undefined(void);
319 /** The mode_b tarval 'false'. */
320 FIRM_API ir_tarval *tarval_b_false;
321 /** Returns the mode_b tarval 'false'. */
322 FIRM_API ir_tarval *get_tarval_b_false(void);
324 /** The mode_b tarval 'true'. */
325 FIRM_API ir_tarval *tarval_b_true;
326 /** Returns the mode_b tarval 'true'. */
327 FIRM_API ir_tarval *get_tarval_b_true(void);
329 /** The mode_X tarval 'unreachable'. */
330 FIRM_API ir_tarval *tarval_unreachable;
331 /** Returns the mode_X tarval 'unreachable'. */
332 FIRM_API ir_tarval *get_tarval_unreachable(void);
334 /** The mode_X tarval 'reachable'. */
335 FIRM_API ir_tarval *tarval_reachable;
336 /** Returns the mode_X tarval 'reachable'. */
337 FIRM_API ir_tarval *get_tarval_reachable(void);
339 /** The 'top' tarval. This is just another name for the 'undefined' tarval. */
340 #define tarval_top tarval_undefined
341 /** Returns the 'top' tarval. */
342 #define get_tarval_top() get_tarval_undefined()
344 /** The 'bottom' tarval. This is just another name for the 'bad' tarval. */
345 #define tarval_bottom tarval_bad
346 /** Returns the 'bottom' tarval. */
347 #define get_tarval_bottom() get_tarval_bad()
349 /* These functions calculate and return a tarval representing the requested
351 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
352 * functions, but these are stored on initialization of the irmode module and
353 * therefore the irmode functions should be preferred to the functions below. */
355 /** Returns the maximum value of a given mode. */
356 FIRM_API ir_tarval *get_tarval_max(ir_mode *mode);
358 /** Returns the minimum value of a given mode. */
359 FIRM_API ir_tarval *get_tarval_min(ir_mode *mode);
361 /** Returns the 0 value (additive neutral) of a given mode.
362 For reference modes, the NULL value is returned (old tarval_P_void) */
363 FIRM_API ir_tarval *get_tarval_null(ir_mode *mode);
365 /** Returns the 1 value (multiplicative neutral) of a given mode. */
366 FIRM_API ir_tarval *get_tarval_one(ir_mode *mode);
368 /** Returns the -1 value (multiplicative neutral) of a given mode.
369 * Returns tarval bad for unsigned modes */
370 FIRM_API ir_tarval *get_tarval_minus_one(ir_mode *mode);
372 /** returns the value where all bits are 1 of a given mode.
373 * returns tarval_bad for float modes */
374 FIRM_API ir_tarval *get_tarval_all_one(ir_mode *mode);
376 /** Return quite nan for float_number modes. */
377 FIRM_API ir_tarval *get_tarval_nan(ir_mode *mode);
379 /** Return +inf for float_number modes. */
380 FIRM_API ir_tarval *get_tarval_plus_inf(ir_mode *mode);
382 /** Return -inf for float_number modes. */
383 FIRM_API ir_tarval *get_tarval_minus_inf(ir_mode *mode);
385 /* ******************** Arithmetic operations on tarvals ******************** */
387 typedef enum tarval_int_overflow_mode_t {
388 TV_OVERFLOW_BAD, /**< tarval module will return tarval_bad if a overflow occurs */
389 TV_OVERFLOW_WRAP, /**< tarval module will overflow will be ignored, wrap around occurs */
390 TV_OVERFLOW_SATURATE /**< tarval module will saturate the overflow */
391 } tarval_int_overflow_mode_t;
394 * Sets the overflow mode for integer operations.
396 * @param ov_mode one of teh overflow modes
398 FIRM_API void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
401 * Get the overflow mode for integer operations.
403 FIRM_API tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
406 * Compares two tarvals
408 * Compare a with b and return their relation.
409 * This is either ir_rel_unordered, ir_rel_less, ir_rel_greater, ir_rel_equal
410 * or ir_rel_false if a or b are symbolic pointers which can not be compared at
413 * @param a the first tarval to be compared
414 * @param b the second tarval to be compared
416 FIRM_API ir_relation tarval_cmp(ir_tarval *a, ir_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 FIRM_API ir_tarval *tarval_convert_to(ir_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 FIRM_API ir_tarval *tarval_not(ir_tarval *a);
477 * Arithmetic Negation of a tarval.
479 * @param a the first tarval
481 * @return -a or tarval_bad
483 FIRM_API ir_tarval *tarval_neg(ir_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 FIRM_API ir_tarval *tarval_add(ir_tarval *a, ir_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 FIRM_API ir_tarval *tarval_sub(ir_tarval *a, ir_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 FIRM_API ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b);
517 * Integer division of two tarvals.
519 * @param a the first tarval
520 * @param b the second tarval
522 * @return a / b or tarval_bad
524 FIRM_API ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b);
527 * Remainder of integer division.
529 * @param a the first tarval
530 * @param b the second tarval
532 * @return a % b or tarval_bad
534 FIRM_API ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b);
537 * Integer division AND remainder.
539 * @param a the first tarval
540 * @param b the second tarval
541 * @param mod_res after return, contains the remainder result, a % b or tarval_bad
543 * @return a / b or tarval_bad
545 FIRM_API ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod_res);
548 * Absolute value of a tarval.
550 * @param a the first tarval
552 * @return |a| or tarval_bad
554 FIRM_API ir_tarval *tarval_abs(ir_tarval *a);
557 * Bitwise and of two integer tarvals.
559 * @param a the first tarval
560 * @param b the second tarval
562 * @return a & b or tarval_bad
564 FIRM_API ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b);
567 * Bitwise and not of two integer tarvals.
569 * @param a the first tarval
570 * @param b the second tarval
572 * @return a & ~b or tarval_bad
574 FIRM_API ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b);
577 * Bitwise or of two integer tarvals.
579 * @param a the first tarval
580 * @param b the second tarval
582 * @return a | b or tarval_bad
584 FIRM_API ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b);
587 * Bitwise exclusive 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 FIRM_API ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b);
597 * Logical Left shift.
599 * @param a the first tarval
600 * @param b the second tarval
602 * @return a << b or tarval_bad
604 FIRM_API ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b);
607 * Unsigned (logical) right shift.
609 * @param a the first tarval
610 * @param b the second tarval
612 * @return a >>u b or tarval_bad
614 FIRM_API ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b);
617 * Signed (arithmetic) right shift.
619 * @param a the first tarval
620 * @param b the second tarval
622 * @return a >>s b or tarval_bad
624 FIRM_API ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b);
629 * @param a the first tarval
630 * @param b the second tarval
632 * @return a \<\<L\>\> b or tarval_bad
634 FIRM_API ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b);
637 * Returns the carry flag of the last operation.
639 FIRM_API int tarval_carry(void);
641 /* *********** Output of tarvals *********** */
644 * The output mode for tarval values.
646 * Some modes allow more that one representation, for instance integers
647 * can be represented hex or decimal. Of course it would be enough to have
648 * one and let every backend convert it into the 'right' one.
649 * However, we can do this in the tarval much simpler...
652 TVO_NATIVE, /**< the default output mode, depends on the mode */
653 TVO_HEX, /**< use hex representation, always possible */
654 TVO_DECIMAL, /**< use decimal representation */
655 TVO_OCTAL, /**< use octal representation */
656 TVO_BINARY, /**< use binary representation */
657 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
658 TVO_HEXFLOAT /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
662 * This structure contains helper information to format the output
663 * of a tarval of a mode.
665 typedef struct tarval_mode_info {
666 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
667 const char *mode_prefix; /**< if set, this prefix will be printed
668 before a value of this mode */
669 const char *mode_suffix; /**< if set, this suffix will be printed
670 after a value of this mode */
674 * Specify the output options of one mode.
676 * This functions stores the mode info, so DO NOT DESTROY it.
678 * @param mode a ir_mode that should be associated
679 * @param modeinfo the output format info
681 * @return zero on success.
683 FIRM_API int set_tarval_mode_output_option(ir_mode *mode,
684 const tarval_mode_info *modeinfo);
687 * Returns the output options of one mode.
689 * This functions returns the mode info of a given mode.
691 * @param mode a ir_mode that should be associated
693 * @return the output option
695 FIRM_API const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
698 * Returns Bit representation of a tarval value, as string of '0' and '1'
700 * @param tv The tarval
702 * This function returns a printable bit representation of any value
703 * stored as tarval. This representation is a null terminated C string.
706 * As usual in C a pointer to a char is returned. The length of the
707 * returned string if fixed, just read as many chars as the mode defines
711 * The string is allocated using malloc() and is free()ed on the next call
713 * The string consists of the ASCII characters '0' and '1' and is
717 * irmode.h for the definition of the ir_mode struct
718 * the size member of aforementioned struct
720 FIRM_API char *get_tarval_bitpattern(ir_tarval *tv);
723 * Returns the bitpattern of the bytes_ofs byte.
725 * This function succeeds even if the mode of the tarval uses lesser bits
726 * than requested, in that case the bitpattern is filled with zero bits.
728 * To query a 32bit value the following code can be used:
730 * val0 = tarval_sub_bits(tv, 0); <- lowest bits
731 * val1 = tarval_sub_bits(tv, 1);
732 * val2 = tarval_sub_bits(tv, 2);
733 * val3 = tarval_sub_bits(tv, 3); <- highest bits
735 * Because this is the bit representation of the target machine, only the following
736 * operations are legal on the result:
738 * - concatenation (endian dependence MUST be handled by the CALLER)
739 * - bitwise logical operations to select/mask bits
741 * @param tv the tarval
742 * @param byte_ofs the byte offset from lower to higher
745 * The result of this function is undefined if the mode is neither integer nor float.
747 FIRM_API unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs);
750 * Returns non-zero if a given (integer) tarval has only one single bit
753 * @param tv the tarval
755 FIRM_API int tarval_is_single_bit(ir_tarval *tv);
758 * Return the number of set bits in a given (integer) tarval.
760 * @param tv the tarval
762 * @return number of set bits or -1 on error
764 FIRM_API int get_tarval_popcount(ir_tarval *tv);
767 * Return the number of the lowest set bit in a given (integer) tarval.
769 * @param tv the tarval
771 * @return number of lowest set bit or -1 on error
773 FIRM_API int get_tarval_lowest_bit(ir_tarval *tv);
776 * Output a tarval to a string buffer.
778 * @param buf the output buffer
779 * @param buflen the length of the buffer
780 * @param tv the tarval
782 FIRM_API int tarval_snprintf(char *buf, size_t buflen, ir_tarval *tv);
785 * Output a tarval to stdio.
787 * @param tv the tarval
789 FIRM_API int tarval_printf(ir_tarval *tv);
792 * Returns non-zero if the mantissa of a floating point IEEE-754
793 * tarval is zero (i.e. 1.0Exxx)
795 * @param tv the tarval
797 FIRM_API int tarval_ieee754_zero_mantissa(ir_tarval *tv);
800 * Returns the exponent of a floating point IEEE-754
803 * @param tv the tarval
805 FIRM_API int tarval_ieee754_get_exponent(ir_tarval *tv);
808 * Check if the tarval can be converted to the given mode without
811 * @param tv the tarval
812 * @param mode the mode to convert to
814 FIRM_API int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode);
817 * Set the immediate precision for IEEE-754 results. Set this to
818 * 0 to get the same precision as the operands.
819 * For x87 compatibility, set this to 80.
821 * @return the old setting
823 FIRM_API unsigned tarval_ieee754_set_immediate_precision(unsigned bits);
826 * Returns non-zero if the result of the last IEEE-754 operation was exact.
828 FIRM_API unsigned tarval_ieee754_get_exact(void);
831 * Return the size of the mantissa in bits (including possible
832 * implicit bits) for the given mode.
834 FIRM_API unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode);
837 * Enable/Disable floating point constant folding.
839 FIRM_API void tarval_enable_fp_ops(int enable);
841 /** returns 0/1 if floating point folding is enable/disabled */
842 FIRM_API int tarval_fp_ops_enabled(void);
845 * Check if its the a floating point NaN.
847 * @param tv the tarval
849 FIRM_API int tarval_is_NaN(ir_tarval *tv);
852 * Check if its the a floating point +inf.
854 * @param tv the tarval
856 FIRM_API int tarval_is_plus_inf(ir_tarval *tv);
859 * Check if its the a floating point -inf.
861 * @param tv the tarval
863 FIRM_API int tarval_is_minus_inf(ir_tarval *tv);
866 * Check if the tarval represents a finite value, ie neither NaN nor inf.
868 * @param tv the tarval
870 FIRM_API int tarval_is_finite(ir_tarval *tv);
873 * Checks whether a pointer points to a tarval.
875 * @param thing an arbitrary pointer
878 * true if the thing is a tarval, else false
880 FIRM_API int is_tarval(const void *thing);