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
26 * @brief target machine values.
32 #include "firm_types.h"
36 /** @defgroup ir_tarval Target Machine Values
38 * Tarvals only represent values of mode_sort:
45 * In case of references the module accepts an entity to represent the
46 * value. Furthermore, computations and conversions of these values can
51 * irmode.h for the modes definitions
57 * Constructor function for new tarvals.
59 * @param str The string representing the target value
60 * @param len The length of the string
61 * @param mode The mode requested for the result tarval
63 * This function creates a new tarval representing the value represented
64 * by a CString, aka char array. If a tarval representing this value already
65 * exists, this tarval is returned instead of a new one. So tarvals are
66 * directly comparable since their representation is unique.
68 * This function accepts the following strings:
70 * if mode is int_number:
71 * - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
72 * - 0[0-7]* (octal representation)
73 * - (+|-)?[1-9][0-9]* (decimal representation)
75 * if mode is float_number:
76 * - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
78 * if mode is boolean: true, True, TRUE ... False... 0, 1,
80 * if mode is reference: hexadecimal of decimal number as int
82 * if mode is character: hex or dec
84 * Leading and/or trailing spaces are ignored
87 * A tarval of proper type representing the requested value is returned.
88 * Tarvals are unique, so for any value/mode pair at most one tarval will
89 * exist, which will be returned upon further requests with an identical
93 * If the string is not representable in the given mode an assertion is
94 * thrown in assert build.
97 * irmode.h for predefined modes
98 * new_tarval_from_long()
99 * new_tarval_from_double()
101 FIRM_API ir_tarval *new_tarval_from_str(const char *str, size_t len,
105 * Construct a new tarval from a given string.
107 * @param str The string representing the target value
108 * @param len The length of the string
109 * @param sign is -1 or 1 depending on the numbers sign
110 * @param base number system base.
111 * binary(2), octal(8), decimal(10) and hexadecimal(16) numbers
113 * @param mode The mode requested for the result tarval
116 * A tarval with the given mode. If overflow settings are set to
117 * TV_OVERFLOW_BAD then a tarval_bad is returned if the number can't be
118 * represented in the given mode.
119 * Return bad if the number couldn't successfully be parsed.
121 FIRM_API ir_tarval *new_integer_tarval_from_str(const char *str, size_t len,
122 char sign, unsigned char base,
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 FIRM_API ir_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 FIRM_API long get_tarval_long(ir_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 FIRM_API int tarval_is_long(ir_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 FIRM_API ir_tarval *new_tarval_from_double(double d, ir_mode *mode);
205 * same as new_tarval_from_double(), but with a long double argument
207 FIRM_API ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode);
210 * This returns a double with the value represented value, or
211 * gibberish, depending on the size of double and the size of the
213 * This will overflow silently, so use only if you know what
214 * you are doing! (better check with tarval_is_long...)
216 * @param tv the tarval
218 FIRM_API double get_tarval_double(ir_tarval *tv);
221 * same as get_tarval_double but returns a long double value
223 FIRM_API long double get_tarval_long_double(ir_tarval *tv);
226 * This validates if tarval_to_double() will return a satisfying
227 * result. I.e. if tv is an float_number and between min, max
230 * @param tv the tarval
232 FIRM_API int tarval_is_double(ir_tarval *tv);
235 * Returns the mode of the tarval.
237 * @param tv the tarval
239 FIRM_API ir_mode *get_tarval_mode(const ir_tarval *tv);
242 * Returns 1 if tv is negative
244 * @param tv the tarval
246 FIRM_API int tarval_is_negative(ir_tarval *tv);
249 * Returns 1 if tv is null
251 * @param tv the tarval
253 FIRM_API int tarval_is_null(ir_tarval *tv);
256 * Returns 1 if tv is the "one"
258 * @param tv the tarval
260 FIRM_API int tarval_is_one(ir_tarval *tv);
263 * Returns 1 if tv is the "minus one"
265 * @param tv the tarval
267 FIRM_API int tarval_is_minus_one(ir_tarval *tv);
270 * returns non-zero if all bits in the tarval are set
272 FIRM_API int tarval_is_all_one(ir_tarval *tv);
275 * Return non-zero if the tarval is a constant (ie. NOT
276 * a reserved tarval like bad, undef, reachable etc.)
278 FIRM_API int tarval_is_constant(ir_tarval *tv);
280 /** The 'bad' tarval. */
281 FIRM_API ir_tarval *tarval_bad;
282 /** Returns the 'bad' tarval. */
283 FIRM_API ir_tarval *get_tarval_bad(void);
285 /** The 'undefined' tarval. */
286 FIRM_API ir_tarval *tarval_undefined;
287 /** Returns the 'undefined' tarval. */
288 FIRM_API ir_tarval *get_tarval_undefined(void);
290 /** The mode_b tarval 'false'. */
291 FIRM_API ir_tarval *tarval_b_false;
292 /** Returns the mode_b tarval 'false'. */
293 FIRM_API ir_tarval *get_tarval_b_false(void);
295 /** The mode_b tarval 'true'. */
296 FIRM_API ir_tarval *tarval_b_true;
297 /** Returns the mode_b tarval 'true'. */
298 FIRM_API ir_tarval *get_tarval_b_true(void);
300 /** The mode_X tarval 'unreachable'. */
301 FIRM_API ir_tarval *tarval_unreachable;
302 /** Returns the mode_X tarval 'unreachable'. */
303 FIRM_API ir_tarval *get_tarval_unreachable(void);
305 /** The mode_X tarval 'reachable'. */
306 FIRM_API ir_tarval *tarval_reachable;
307 /** Returns the mode_X tarval 'reachable'. */
308 FIRM_API ir_tarval *get_tarval_reachable(void);
310 /** The 'top' tarval. This is just another name for the 'undefined' tarval. */
311 #define tarval_top tarval_undefined
312 /** Returns the 'top' tarval. */
313 #define get_tarval_top() get_tarval_undefined()
315 /** The 'bottom' tarval. This is just another name for the 'bad' tarval. */
316 #define tarval_bottom tarval_bad
317 /** Returns the 'bottom' tarval. */
318 #define get_tarval_bottom() get_tarval_bad()
320 /** Returns the maximum value of a given mode. */
321 FIRM_API ir_tarval *get_tarval_max(ir_mode *mode);
323 /** Returns the minimum value of a given mode. */
324 FIRM_API ir_tarval *get_tarval_min(ir_mode *mode);
326 /** Returns the 0 value (additive neutral) of a given mode.
327 For reference modes, the NULL value is returned (old tarval_P_void) */
328 FIRM_API ir_tarval *get_tarval_null(ir_mode *mode);
330 /** Returns the 1 value (multiplicative neutral) of a given mode. */
331 FIRM_API ir_tarval *get_tarval_one(ir_mode *mode);
333 /** Returns the -1 value (multiplicative neutral) of a given mode.
334 * Returns tarval bad for unsigned modes */
335 FIRM_API ir_tarval *get_tarval_minus_one(ir_mode *mode);
337 /** returns the value where all bits are 1 of a given mode.
338 * returns tarval_bad for float modes */
339 FIRM_API ir_tarval *get_tarval_all_one(ir_mode *mode);
341 /** Return quite nan for float_number modes. */
342 FIRM_API ir_tarval *get_tarval_nan(ir_mode *mode);
344 /** Return +inf for float_number modes. */
345 FIRM_API ir_tarval *get_tarval_plus_inf(ir_mode *mode);
347 /** Return -inf for float_number modes. */
348 FIRM_API ir_tarval *get_tarval_minus_inf(ir_mode *mode);
350 typedef enum tarval_int_overflow_mode_t {
351 TV_OVERFLOW_BAD, /**< tarval module will return tarval_bad if a overflow occurs */
352 TV_OVERFLOW_WRAP, /**< tarval module will overflow will be ignored, wrap around occurs */
353 TV_OVERFLOW_SATURATE /**< tarval module will saturate the overflow */
354 } tarval_int_overflow_mode_t;
357 * Sets the overflow mode for integer operations.
359 * @param ov_mode one of the overflow modes
361 FIRM_API void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
364 * Get the overflow mode for integer operations.
366 FIRM_API tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
369 * Compares two tarvals
371 * Compare a with b and return their relation.
372 * This is either ir_rel_unordered, ir_rel_less, ir_rel_greater, ir_rel_equal
373 * or ir_rel_false if a or b are symbolic pointers which can not be compared at
376 * @param a the first tarval to be compared
377 * @param b the second tarval to be compared
379 FIRM_API ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b);
382 * Converts a tarval to another mode.
384 * Convert tarval 'src' to mode 'mode', this will succeed if and only if mode
385 * 'mode' is wider than the mode of src, as defined in the firm documentation
386 * and as returned by the function mode_is_smaller defined in irmode.h.
388 * @param src The tarval to convert
389 * @param mode Tho mode to convert to
392 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
393 * tarvals value already exists, it will be returned, else a new tarval is
394 * constructed and returned
397 * Illegal convertions will trigger a panic
400 * FIRM documentation for conversion rules
401 * mode_is_smaller defined in irmode.h
403 FIRM_API ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *mode);
406 * These function implement basic computations representable as opcodes
412 * a - the tarval to operate on
415 * a - the first operand tarval
416 * b - the second operand tarval
419 * If necessary a new tarval is constructed for the resulting value,
420 * or the one already carrying the computation result is retrieved and
421 * returned as result.
424 * The order the arguments are given in is important, imagine postfix
426 * Illegal operations will trigger an assertion.
427 * The sort member of the struct mode defines which operations are valid
431 * Bitwise Negation of a tarval.
433 * @param a the first tarval
435 * @return ~a or tarval_bad
437 FIRM_API ir_tarval *tarval_not(ir_tarval *a);
440 * Arithmetic Negation of a tarval.
442 * @param a the first tarval
444 * @return -a or tarval_bad
446 FIRM_API ir_tarval *tarval_neg(ir_tarval *a);
449 * Addition of two tarvals.
451 * @param a the first tarval
452 * @param b the second tarval
454 * @return a + b or tarval_bad
456 FIRM_API ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b);
459 * Subtraction from a tarval.
461 * @param a the first tarval
462 * @param b the second tarval
463 * @param dst_mode the mode of the result, needed for mode_P - mode_P, else NULL
465 * @return a - b or tarval_bad
467 FIRM_API ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode);
470 * Multiplication of tarvals.
472 * @param a the first tarval
473 * @param b the second tarval
475 * @return a * b or tarval_bad
477 FIRM_API ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b);
480 * Integer division of two tarvals.
482 * @param a the first tarval
483 * @param b the second tarval
485 * @return a / b or tarval_bad
487 FIRM_API ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b);
490 * Remainder of integer division.
492 * @param a the first tarval
493 * @param b the second tarval
495 * @return a % b or tarval_bad
497 FIRM_API ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b);
500 * Integer division AND remainder.
502 * @param a the first tarval
503 * @param b the second tarval
504 * @param mod_res after return, contains the remainder result, a % b or tarval_bad
506 * @return a / b or tarval_bad
508 FIRM_API ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod_res);
511 * Absolute value of a tarval.
513 * @param a the first tarval
515 * @return |a| or tarval_bad
517 FIRM_API ir_tarval *tarval_abs(ir_tarval *a);
520 * Bitwise and of two integer tarvals.
522 * @param a the first tarval
523 * @param b the second tarval
525 * @return a & b or tarval_bad
527 FIRM_API ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b);
530 * Bitwise and not of two integer tarvals.
532 * @param a the first tarval
533 * @param b the second tarval
535 * @return a & ~b or tarval_bad
537 FIRM_API ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b);
540 * Bitwise or of two integer tarvals.
542 * @param a the first tarval
543 * @param b the second tarval
545 * @return a | b or tarval_bad
547 FIRM_API ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b);
550 * Bitwise exclusive or of two integer tarvals.
552 * @param a the first tarval
553 * @param b the second tarval
555 * @return a ^ b or tarval_bad
557 FIRM_API ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b);
560 * Logical Left shift.
562 * @param a the first tarval
563 * @param b the second tarval
565 * @return a << b or tarval_bad
567 FIRM_API ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b);
570 * Unsigned (logical) right shift.
572 * @param a the first tarval
573 * @param b the second tarval
575 * @return a >>u b or tarval_bad
577 FIRM_API ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b);
580 * Signed (arithmetic) right shift.
582 * @param a the first tarval
583 * @param b the second tarval
585 * @return a >>s b or tarval_bad
587 FIRM_API ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b);
592 * @param a the first tarval
593 * @param b the second tarval
595 * @return a \<\<L\>\> b or tarval_bad
597 FIRM_API ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b);
600 * Returns the carry flag of the last operation.
602 FIRM_API int tarval_carry(void);
605 * The output mode for tarval values.
607 * Some modes allow more that one representation, for instance integers
608 * can be represented hex or decimal. Of course it would be enough to have
609 * one and let every backend convert it into the 'right' one.
610 * However, we can do this in the tarval much simpler...
613 TVO_NATIVE, /**< the default output mode, depends on the mode */
614 TVO_HEX, /**< use hex representation, always possible */
615 TVO_DECIMAL, /**< use decimal representation */
616 TVO_OCTAL, /**< use octal representation */
617 TVO_BINARY, /**< use binary representation */
618 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
619 TVO_HEXFLOAT /**< use hexadecimal floating point representation
624 * This structure contains helper information to format the output
625 * of a tarval of a mode.
627 typedef struct tarval_mode_info {
628 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
629 const char *mode_prefix; /**< if set, this prefix will be printed
630 before a value of this mode */
631 const char *mode_suffix; /**< if set, this suffix will be printed
632 after a value of this mode */
636 * Specify the output options of one mode.
638 * This functions stores the mode info, so DO NOT DESTROY it.
640 * @param mode a ir_mode that should be associated
641 * @param modeinfo the output format info
643 * @return zero on success.
645 FIRM_API int set_tarval_mode_output_option(ir_mode *mode,
646 const tarval_mode_info *modeinfo);
649 * Returns the output options of one mode.
651 * This functions returns the mode info of a given mode.
653 * @param mode a ir_mode that should be associated
655 * @return the output option
657 FIRM_API const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
660 * Returns Bit representation of a tarval value, as string of '0' and '1'
662 * @param tv The tarval
664 * This function returns a printable bit representation of any value
665 * stored as tarval. This representation is a null terminated C string.
668 * As usual in C a pointer to a char is returned. The length of the
669 * returned string if fixed, just read as many chars as the mode defines
673 * The string is allocated using malloc() and is free()ed on the next call
675 * The string consists of the ASCII characters '0' and '1' and is
679 * irmode.h for the definition of the ir_mode struct
680 * the size member of aforementioned struct
682 FIRM_API char *get_tarval_bitpattern(ir_tarval *tv);
685 * Returns the bitpattern of the bytes_ofs byte.
687 * This function succeeds even if the mode of the tarval uses lesser bits
688 * than requested, in that case the bitpattern is filled with zero bits.
690 * To query a 32bit value the following code can be used:
692 * val0 = tarval_sub_bits(tv, 0); <- lowest bits
693 * val1 = tarval_sub_bits(tv, 1);
694 * val2 = tarval_sub_bits(tv, 2);
695 * val3 = tarval_sub_bits(tv, 3); <- highest bits
697 * Because this is the bit representation of the target machine, only the
698 * following operations are legal on the result:
700 * - concatenation (endian dependence MUST be handled by the CALLER)
701 * - bitwise logical operations to select/mask bits
703 * @param tv the tarval
704 * @param byte_ofs the byte offset from lower to higher
707 * The result of this function is undefined if the mode is neither integer
710 FIRM_API unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs);
713 * Returns non-zero if a given (integer) tarval has only one single bit
716 * @param tv the tarval
718 FIRM_API int tarval_is_single_bit(ir_tarval *tv);
721 * Return the number of set bits in a given (integer) tarval.
723 * @param tv the tarval
725 * @return number of set bits or -1 on error
727 FIRM_API int get_tarval_popcount(ir_tarval *tv);
730 * Return the number of the lowest set bit in a given (integer) tarval.
732 * @param tv the tarval
734 * @return number of lowest set bit or -1 on error
736 FIRM_API int get_tarval_lowest_bit(ir_tarval *tv);
739 * Output a tarval to a string buffer.
741 * @param buf the output buffer
742 * @param buflen the length of the buffer
743 * @param tv the tarval
745 FIRM_API int tarval_snprintf(char *buf, size_t buflen, ir_tarval *tv);
748 * Output a tarval to stdio.
750 * @param tv the tarval
752 FIRM_API int tarval_printf(ir_tarval *tv);
755 * Returns non-zero if the mantissa of a floating point tarval is zero
758 * @param tv the tarval
760 FIRM_API int tarval_zero_mantissa(ir_tarval *tv);
763 * Returns the exponent of a floating point IEEE-754
766 * @param tv the tarval
768 FIRM_API int tarval_get_exponent(ir_tarval *tv);
771 * Check if the tarval can be converted to the given mode without
774 * @param tv the tarval
775 * @param mode the mode to convert to
777 FIRM_API int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode);
780 * Returns non-zero if the result of the last IEEE-754 operation was exact.
782 FIRM_API unsigned tarval_ieee754_get_exact(void);
785 * Enable/Disable floating point constant folding.
787 FIRM_API void tarval_enable_fp_ops(int enable);
789 /** returns 0/1 if floating point folding is enable/disabled */
790 FIRM_API int tarval_fp_ops_enabled(void);
793 * Check if its the a floating point NaN.
795 * @param tv the tarval
797 FIRM_API int tarval_is_NaN(ir_tarval *tv);
800 * Check if its the a floating point +inf.
802 * @param tv the tarval
804 FIRM_API int tarval_is_plus_inf(ir_tarval *tv);
807 * Check if its the a floating point -inf.
809 * @param tv the tarval
811 FIRM_API int tarval_is_minus_inf(ir_tarval *tv);
814 * Check if the tarval represents a finite value, ie neither NaN nor inf.
816 * @param tv the tarval
818 FIRM_API int tarval_is_finite(ir_tarval *tv);
821 * Checks whether a pointer points to a tarval.
823 * @param thing an arbitrary pointer
826 * true if the thing is a tarval, else false
828 FIRM_API int is_tarval(const void *thing);