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"
58 * Constructor function for new tarvals.
60 * @param str The string representing the target value
61 * @param len The length of the string
62 * @param mode The mode requested for the result tarval
64 * This function creates a new tarval representing the value represented
65 * by a CString, aka char array. If a tarval representing this value already
66 * exists, this tarval is returned instead of a new one. So tarvals are
67 * directly comparable since their representation is unique.
69 * This function accepts the following strings:
71 * if mode is int_number:
72 * - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
73 * - 0[0-7]* (octal representation)
74 * - (+|-)?[1-9][0-9]* (decimal representation)
76 * if mode is float_number:
77 * - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
79 * if mode is boolean: true, True, TRUE ... False... 0, 1,
81 * if mode is reference: hexadecimal of decimal number as int
83 * if mode is character: hex or dec
85 * Leading and/or trailing spaces are ignored
88 * A tarval of proper type representing the requested value is returned.
89 * Tarvals are unique, so for any value/mode pair at most one tarval will
90 * exist, which will be returned upon further requests with an identical
94 * If the string is not representable in the given mode an assertion is
95 * thrown in assert build.
98 * irmode.h for predefined modes
99 * new_tarval_from_long()
100 * new_tarval_from_double()
102 FIRM_API ir_tarval *new_tarval_from_str(const char *str, size_t len,
106 * Construct a new tarval from a given string.
108 * @param str The string representing the target value
109 * @param len The length of the string
110 * @param sign is -1 or 1 depending on the numbers sign
111 * @param base number system base.
112 * binary(2), octal(8), decimal(10) and hexadecimal(16) numbers
114 * @param mode The mode requested for the result tarval
117 * A tarval with the given mode. If overflow settings are set to
118 * TV_OVERFLOW_BAD then a tarval_bad is returned if the number can't be
119 * represented in the given mode.
120 * Return bad if the number couldn't successfully be parsed.
122 FIRM_API ir_tarval *new_integer_tarval_from_str(const char *str, size_t len,
123 char sign, unsigned char base,
127 * Constructor function for new tarvals
129 * @param l The long representing the value
130 * @param mode The mode requested for the result tarval
132 * This function creates a new tarval representing the value represented
133 * by a long integer. If a tarval representing this value already exists,
134 * this tarval is returned instead of a new one. So tarvals are directly
135 * comparable since their representation is unique.
138 * A tarval of proper type representing the requested value is returned.
139 * Tarvals are unique, so for any value/mode pair at most one tarval will
140 * exist, which will be returned upon further requests with an identical
144 * If the long is not representable in the given mode an assertion is
145 * thrown in assert build.
148 * irmode.h for predefined modes
149 * new_tarval_from_str()
150 * new_tarval_from_double()
153 FIRM_API ir_tarval *new_tarval_from_long(long l, ir_mode *mode);
155 /** Return value as long if possible.
157 * This returns a long int with the value represented value, or
158 * gibberish, depending on the size of long int and the size of the
159 * stored value. It works for e.g. 1 as mode_Ls, but might not work for
160 * get_mode_max(mode_Ls).
161 * This will overflow silently, so use only if you know what
162 * you are doing! (better check with tarval_is_long()...)
163 * Works only for int modes, even not for character modes!
165 FIRM_API long get_tarval_long(ir_tarval *tv);
168 * This validates if get_tarval_long() will return a satisfying
169 * result. I.e. if tv is an int_number and between min, max
170 * of long int (signed!)
172 * @param tv the tarval
174 FIRM_API int tarval_is_long(ir_tarval *tv);
177 * Constructor function for new tarvals.
179 * @param d The (long) double representing the value
180 * @param mode The mode requested for the result tarval
182 * This function creates a new tarval representing the value represented
183 * by a (long) double. If a tarval representing this value already exists,
184 * this tarval is returned instead of a new one. So tarvals are directly
185 * comparable since their representation is unique.
186 * Only modes of sort float_number can be constructed this way.
189 * A tarval of proper type representing the requested value is returned.
190 * Tarvals are unique, so for any value/mode pair at most one tarval will
191 * exist, which will be returned upon further requests with an identical
195 * If the (long) double is not representable in the given mode an assertion
196 * is thrown. This will happen for any mode not of sort float_number.
199 * irmode.h for predefined values
200 * new_tarval_from_str()
201 * new_tarval_from_long()
203 FIRM_API ir_tarval *new_tarval_from_double(double d, ir_mode *mode);
206 * same as new_tarval_from_double(), but with a long double argument
208 FIRM_API ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode);
211 * This returns a double with the value represented value, or
212 * gibberish, depending on the size of double and the size of the
214 * This will overflow silently, so use only if you know what
215 * you are doing! (better check with tarval_is_long...)
217 * @param tv the tarval
219 FIRM_API double get_tarval_double(ir_tarval *tv);
222 * same as get_tarval_double but returns a long double value
224 FIRM_API long double get_tarval_long_double(ir_tarval *tv);
227 * This validates if tarval_to_double() will return a satisfying
228 * result. I.e. if tv is an float_number and between min, max
231 * @param tv the tarval
233 FIRM_API int tarval_is_double(ir_tarval *tv);
236 * Returns the mode of the tarval.
238 * @param tv the tarval
240 FIRM_API ir_mode *get_tarval_mode(const ir_tarval *tv);
243 * Returns 1 if tv is negative
245 * @param tv the tarval
247 FIRM_API int tarval_is_negative(ir_tarval *tv);
250 * Returns 1 if tv is null
252 * @param tv the tarval
254 FIRM_API int tarval_is_null(ir_tarval *tv);
257 * Returns 1 if tv is the "one"
259 * @param tv the tarval
261 FIRM_API int tarval_is_one(ir_tarval *tv);
264 * Returns 1 if tv is the "minus one"
266 * @param tv the tarval
268 FIRM_API int tarval_is_minus_one(ir_tarval *tv);
271 * returns non-zero if all bits in the tarval are set
273 FIRM_API int tarval_is_all_one(ir_tarval *tv);
276 * Return non-zero if the tarval is a constant (ie. NOT
277 * a reserved tarval like bad, undef, reachable etc.)
279 FIRM_API int tarval_is_constant(ir_tarval *tv);
281 /** The 'bad' tarval. */
282 FIRM_API ir_tarval *tarval_bad;
283 /** Returns the 'bad' tarval. */
284 FIRM_API ir_tarval *get_tarval_bad(void);
286 /** The 'undefined' tarval. */
287 FIRM_API ir_tarval *tarval_undefined;
288 /** Returns the 'undefined' tarval. */
289 FIRM_API ir_tarval *get_tarval_undefined(void);
291 /** The mode_b tarval 'false'. */
292 FIRM_API ir_tarval *tarval_b_false;
293 /** Returns the mode_b tarval 'false'. */
294 FIRM_API ir_tarval *get_tarval_b_false(void);
296 /** The mode_b tarval 'true'. */
297 FIRM_API ir_tarval *tarval_b_true;
298 /** Returns the mode_b tarval 'true'. */
299 FIRM_API ir_tarval *get_tarval_b_true(void);
301 /** The mode_X tarval 'unreachable'. */
302 FIRM_API ir_tarval *tarval_unreachable;
303 /** Returns the mode_X tarval 'unreachable'. */
304 FIRM_API ir_tarval *get_tarval_unreachable(void);
306 /** The mode_X tarval 'reachable'. */
307 FIRM_API ir_tarval *tarval_reachable;
308 /** Returns the mode_X tarval 'reachable'. */
309 FIRM_API ir_tarval *get_tarval_reachable(void);
311 /** The 'top' tarval. This is just another name for the 'undefined' tarval. */
312 #define tarval_top tarval_undefined
313 /** Returns the 'top' tarval. */
314 #define get_tarval_top() get_tarval_undefined()
316 /** The 'bottom' tarval. This is just another name for the 'bad' tarval. */
317 #define tarval_bottom tarval_bad
318 /** Returns the 'bottom' tarval. */
319 #define get_tarval_bottom() get_tarval_bad()
321 /** Returns the maximum value of a given mode. */
322 FIRM_API ir_tarval *get_tarval_max(ir_mode *mode);
324 /** Returns the minimum value of a given mode. */
325 FIRM_API ir_tarval *get_tarval_min(ir_mode *mode);
327 /** Returns the 0 value (additive neutral) of a given mode.
328 For reference modes, the NULL value is returned (old tarval_P_void) */
329 FIRM_API ir_tarval *get_tarval_null(ir_mode *mode);
331 /** Returns the 1 value (multiplicative neutral) of a given mode. */
332 FIRM_API ir_tarval *get_tarval_one(ir_mode *mode);
334 /** Returns the -1 value (multiplicative neutral) of a given mode.
335 * Returns tarval bad for unsigned modes */
336 FIRM_API ir_tarval *get_tarval_minus_one(ir_mode *mode);
338 /** returns the value where all bits are 1 of a given mode.
339 * returns tarval_bad for float modes */
340 FIRM_API ir_tarval *get_tarval_all_one(ir_mode *mode);
342 /** Return quite nan for float_number modes. */
343 FIRM_API ir_tarval *get_tarval_nan(ir_mode *mode);
345 /** Return +inf for float_number modes. */
346 FIRM_API ir_tarval *get_tarval_plus_inf(ir_mode *mode);
348 /** Return -inf for float_number modes. */
349 FIRM_API ir_tarval *get_tarval_minus_inf(ir_mode *mode);
351 typedef enum tarval_int_overflow_mode_t {
352 TV_OVERFLOW_BAD, /**< tarval module will return tarval_bad if a overflow occurs */
353 TV_OVERFLOW_WRAP, /**< tarval module will overflow will be ignored, wrap around occurs */
354 TV_OVERFLOW_SATURATE /**< tarval module will saturate the overflow */
355 } tarval_int_overflow_mode_t;
358 * Sets the overflow mode for integer operations.
360 * @param ov_mode one of the overflow modes
362 FIRM_API void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
365 * Get the overflow mode for integer operations.
367 FIRM_API tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
370 * Compares two tarvals
372 * Compare a with b and return their relation.
373 * This is either ir_rel_unordered, ir_rel_less, ir_rel_greater, ir_rel_equal
374 * or ir_rel_false if a or b are symbolic pointers which can not be compared at
377 * @param a the first tarval to be compared
378 * @param b the second tarval to be compared
380 FIRM_API ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b);
383 * Converts a tarval to another mode.
385 * Convert tarval 'src' to mode 'mode', this will succeed if and only if mode
386 * 'mode' is wider than the mode of src, as defined in the firm documentation
387 * and as returned by the function mode_is_smaller defined in irmode.h.
389 * @param src The tarval to convert
390 * @param mode Tho mode to convert to
393 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
394 * tarvals value already exists, it will be returned, else a new tarval is
395 * constructed and returned
398 * Illegal convertions will trigger a panic
401 * FIRM documentation for conversion rules
402 * mode_is_smaller defined in irmode.h
404 FIRM_API ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *mode);
407 * These function implement basic computations representable as opcodes
413 * a - the tarval to operate on
416 * a - the first operand tarval
417 * b - the second operand tarval
420 * If necessary a new tarval is constructed for the resulting value,
421 * or the one already carrying the computation result is retrieved and
422 * returned as result.
425 * The order the arguments are given in is important, imagine postfix
427 * Illegal operations will trigger an assertion.
428 * The sort member of the struct mode defines which operations are valid
432 * Bitwise Negation of a tarval.
434 * @param a the first tarval
436 * @return ~a or tarval_bad
438 FIRM_API ir_tarval *tarval_not(ir_tarval *a);
441 * Arithmetic Negation of a tarval.
443 * @param a the first tarval
445 * @return -a or tarval_bad
447 FIRM_API ir_tarval *tarval_neg(ir_tarval *a);
450 * Addition of two tarvals.
452 * @param a the first tarval
453 * @param b the second tarval
455 * @return a + b or tarval_bad
457 FIRM_API ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b);
460 * Subtraction from a tarval.
462 * @param a the first tarval
463 * @param b the second tarval
464 * @param dst_mode the mode of the result, needed for mode_P - mode_P, else NULL
466 * @return a - b or tarval_bad
468 FIRM_API ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode);
471 * Multiplication of tarvals.
473 * @param a the first tarval
474 * @param b the second tarval
476 * @return a * b or tarval_bad
478 FIRM_API ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b);
481 * Integer division of two tarvals.
483 * @param a the first tarval
484 * @param b the second tarval
486 * @return a / b or tarval_bad
488 FIRM_API ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b);
491 * Remainder of integer division.
493 * @param a the first tarval
494 * @param b the second tarval
496 * @return a % b or tarval_bad
498 FIRM_API ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b);
501 * Integer division AND remainder.
503 * @param a the first tarval
504 * @param b the second tarval
505 * @param mod_res after return, contains the remainder result, a % b or tarval_bad
507 * @return a / b or tarval_bad
509 FIRM_API ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod_res);
512 * Absolute value of a tarval.
514 * @param a the first tarval
516 * @return |a| or tarval_bad
518 FIRM_API ir_tarval *tarval_abs(ir_tarval *a);
521 * Bitwise and of two integer tarvals.
523 * @param a the first tarval
524 * @param b the second tarval
526 * @return a & b or tarval_bad
528 FIRM_API ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b);
531 * Bitwise and not of two integer tarvals.
533 * @param a the first tarval
534 * @param b the second tarval
536 * @return a & ~b or tarval_bad
538 FIRM_API ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b);
541 * Bitwise or of two integer tarvals.
543 * @param a the first tarval
544 * @param b the second tarval
546 * @return a | b or tarval_bad
548 FIRM_API ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b);
551 * Bitwise exclusive or of two integer tarvals.
553 * @param a the first tarval
554 * @param b the second tarval
556 * @return a ^ b or tarval_bad
558 FIRM_API ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b);
561 * Logical Left shift.
563 * @param a the first tarval
564 * @param b the second tarval
566 * @return a << b or tarval_bad
568 FIRM_API ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b);
571 * Unsigned (logical) right shift.
573 * @param a the first tarval
574 * @param b the second tarval
576 * @return a >>u b or tarval_bad
578 FIRM_API ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b);
581 * Signed (arithmetic) right shift.
583 * @param a the first tarval
584 * @param b the second tarval
586 * @return a >>s b or tarval_bad
588 FIRM_API ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b);
593 * @param a the first tarval
594 * @param b the second tarval
596 * @return a \<\<L\>\> b or tarval_bad
598 FIRM_API ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b);
601 * Returns the carry flag of the last operation.
603 FIRM_API int tarval_carry(void);
606 * The output mode for tarval values.
608 * Some modes allow more that one representation, for instance integers
609 * can be represented hex or decimal. Of course it would be enough to have
610 * one and let every backend convert it into the 'right' one.
611 * However, we can do this in the tarval much simpler...
614 TVO_NATIVE, /**< the default output mode, depends on the mode */
615 TVO_HEX, /**< use hex representation, always possible */
616 TVO_DECIMAL, /**< use decimal representation */
617 TVO_OCTAL, /**< use octal representation */
618 TVO_BINARY, /**< use binary representation */
619 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
620 TVO_HEXFLOAT /**< use hexadecimal floating point representation
625 * This structure contains helper information to format the output
626 * of a tarval of a mode.
628 typedef struct tarval_mode_info {
629 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
630 const char *mode_prefix; /**< if set, this prefix will be printed
631 before a value of this mode */
632 const char *mode_suffix; /**< if set, this suffix will be printed
633 after a value of this mode */
637 * Specify the output options of one mode.
639 * This functions stores the mode info, so DO NOT DESTROY it.
641 * @param mode a ir_mode that should be associated
642 * @param modeinfo the output format info
644 * @return zero on success.
646 FIRM_API int set_tarval_mode_output_option(ir_mode *mode,
647 const tarval_mode_info *modeinfo);
650 * Returns the output options of one mode.
652 * This functions returns the mode info of a given mode.
654 * @param mode a ir_mode that should be associated
656 * @return the output option
658 FIRM_API const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
661 * Returns Bit representation of a tarval value, as string of '0' and '1'
663 * @param tv The tarval
665 * This function returns a printable bit representation of any value
666 * stored as tarval. This representation is a null terminated C string.
669 * As usual in C a pointer to a char is returned. The length of the
670 * returned string if fixed, just read as many chars as the mode defines
674 * The string is allocated using malloc() and is free()ed on the next call
676 * The string consists of the ASCII characters '0' and '1' and is
680 * irmode.h for the definition of the ir_mode struct
681 * the size member of aforementioned struct
683 FIRM_API char *get_tarval_bitpattern(ir_tarval *tv);
686 * Returns the bitpattern of the bytes_ofs byte.
688 * This function succeeds even if the mode of the tarval uses lesser bits
689 * than requested, in that case the bitpattern is filled with zero bits.
691 * To query a 32bit value the following code can be used:
693 * val0 = tarval_sub_bits(tv, 0); <- lowest bits
694 * val1 = tarval_sub_bits(tv, 1);
695 * val2 = tarval_sub_bits(tv, 2);
696 * val3 = tarval_sub_bits(tv, 3); <- highest bits
698 * Because this is the bit representation of the target machine, only the
699 * following operations are legal on the result:
701 * - concatenation (endian dependence MUST be handled by the CALLER)
702 * - bitwise logical operations to select/mask bits
704 * @param tv the tarval
705 * @param byte_ofs the byte offset from lower to higher
708 * The result of this function is undefined if the mode is neither integer
711 FIRM_API unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs);
714 * Returns non-zero if a given (integer) tarval has only one single bit
717 * @param tv the tarval
719 FIRM_API int tarval_is_single_bit(ir_tarval *tv);
722 * Return the number of set bits in a given (integer) tarval.
724 * @param tv the tarval
726 * @return number of set bits or -1 on error
728 FIRM_API int get_tarval_popcount(ir_tarval *tv);
731 * Return the number of the lowest set bit in a given (integer) tarval.
733 * @param tv the tarval
735 * @return number of lowest set bit or -1 on error
737 FIRM_API int get_tarval_lowest_bit(ir_tarval *tv);
740 * Output a tarval to a string buffer.
742 * @param buf the output buffer
743 * @param buflen the length of the buffer
744 * @param tv the tarval
746 FIRM_API int tarval_snprintf(char *buf, size_t buflen, ir_tarval *tv);
749 * Output a tarval to stdio.
751 * @param tv the tarval
753 FIRM_API int tarval_printf(ir_tarval *tv);
756 * Returns non-zero if the mantissa of a floating point IEEE-754
757 * tarval is zero (i.e. 1.0Exxx)
759 * @param tv the tarval
761 FIRM_API int tarval_ieee754_zero_mantissa(ir_tarval *tv);
764 * Returns the exponent of a floating point IEEE-754
767 * @param tv the tarval
769 FIRM_API int tarval_ieee754_get_exponent(ir_tarval *tv);
772 * Check if the tarval can be converted to the given mode without
775 * @param tv the tarval
776 * @param mode the mode to convert to
778 FIRM_API int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode);
781 * Returns non-zero if the result of the last IEEE-754 operation was exact.
783 FIRM_API unsigned tarval_ieee754_get_exact(void);
786 * Return the size of the mantissa in bits (including possible
787 * implicit bits) for the given mode.
789 FIRM_API unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode);
792 * Enable/Disable floating point constant folding.
794 FIRM_API void tarval_enable_fp_ops(int enable);
796 /** returns 0/1 if floating point folding is enable/disabled */
797 FIRM_API int tarval_fp_ops_enabled(void);
800 * Check if its the a floating point NaN.
802 * @param tv the tarval
804 FIRM_API int tarval_is_NaN(ir_tarval *tv);
807 * Check if its the a floating point +inf.
809 * @param tv the tarval
811 FIRM_API int tarval_is_plus_inf(ir_tarval *tv);
814 * Check if its the a floating point -inf.
816 * @param tv the tarval
818 FIRM_API int tarval_is_minus_inf(ir_tarval *tv);
821 * Check if the tarval represents a finite value, ie neither NaN nor inf.
823 * @param tv the tarval
825 FIRM_API int tarval_is_finite(ir_tarval *tv);
828 * Checks whether a pointer points to a tarval.
830 * @param thing an arbitrary pointer
833 * true if the thing is a tarval, else false
835 FIRM_API int is_tarval(const void *thing);