2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Representation of and static computations on target machine
11 * @author Mathias Heil
12 * @brief target machine values.
18 #include "firm_types.h"
22 /** @defgroup ir_tarval Target Machine Values
24 * Tarvals only represent values of mode_sort:
31 * In case of references the module accepts an entity to represent the
32 * value. Furthermore, computations and conversions of these values can
37 * irmode.h for the modes definitions
43 * Constructor function for new tarvals.
45 * @param str The string representing the target value
46 * @param len The length of the string
47 * @param mode The mode requested for the result tarval
49 * This function creates a new tarval representing the value represented
50 * by a CString, aka char array. If a tarval representing this value already
51 * exists, this tarval is returned instead of a new one. So tarvals are
52 * directly comparable since their representation is unique.
54 * This function accepts the following strings:
56 * if mode is int_number:
57 * - [+-]?0[xX][0-9a-fA-F]+ (hexadecimal representation)
58 * - [+-]?0[0-7]* (octal representation)
59 * - [+-]?0[bB][01]+ (binary representation)
60 * - [+-]?[1-9][0-9]* (decimal representation)
62 * if mode is float_number:
63 * - [+-]?(decimal int) (. (decimal int))? ([eE][+-]?(decimal int))?
65 * if mode is boolean: true, True, TRUE ... False... 0, 1,
67 * if mode is reference: "null" and the same as for int_number
69 * Leading and/or trailing spaces are ignored
72 * A tarval of proper type representing the requested value is returned.
73 * Tarvals are unique, so for any value/mode pair at most one tarval will
74 * exist, which will be returned upon further requests with an identical
78 * If the string is not representable in the given mode an assertion is
79 * thrown in assert build.
82 * irmode.h for predefined modes
83 * new_tarval_from_long()
84 * new_tarval_from_double()
86 FIRM_API ir_tarval *new_tarval_from_str(const char *str, size_t len,
90 * Construct a new tarval from a given string.
92 * @param str The string representing the target value
93 * @param len The length of the string
94 * @param sign is -1 or 1 depending on the numbers sign
95 * @param base number system base.
96 * binary(2), octal(8), decimal(10) and hexadecimal(16) numbers
98 * @param mode The mode requested for the result tarval
101 * A tarval with the given mode. If overflow settings are set to
102 * TV_OVERFLOW_BAD then a tarval_bad is returned if the number can't be
103 * represented in the given mode.
104 * Returns bad if the number couldn't successfully be parsed.
106 FIRM_API ir_tarval *new_integer_tarval_from_str(const char *str, size_t len,
107 char sign, unsigned char base,
111 * Constructor function for new tarvals
113 * @param l The long representing the value
114 * @param mode The mode requested for the result tarval
116 * This function creates a new tarval representing the value represented
117 * by a long integer. If a tarval representing this value already exists,
118 * this tarval is returned instead of a new one. So tarvals are directly
119 * comparable since their representation is unique.
122 * A tarval of proper type representing the requested value is returned.
123 * Tarvals are unique, so for any value/mode pair at most one tarval will
124 * exist, which will be returned upon further requests with an identical
128 * If the long is not representable in the given mode an assertion is
129 * thrown in assert build.
132 * irmode.h for predefined modes
133 * new_tarval_from_str()
134 * new_tarval_from_double()
137 FIRM_API ir_tarval *new_tarval_from_long(long l, ir_mode *mode);
139 /** Returns value as long if possible.
141 * This returns a long int with the value represented value, or
142 * gibberish, depending on the size of long int and the size of the
143 * stored value. It works for e.g. 1 as mode_Ls, but might not work for
144 * get_mode_max(mode_Ls).
145 * This will overflow silently, so use only if you know what
146 * you are doing! (better check with tarval_is_long()...)
147 * Works only for int modes, even not for character modes!
149 FIRM_API long get_tarval_long(ir_tarval *tv);
152 * This validates if get_tarval_long() will return a satisfying
153 * result. I.e. if tv is an int_number and between min, max
154 * of long int (signed!)
156 * @param tv the tarval
158 FIRM_API int tarval_is_long(ir_tarval *tv);
161 * Constructor function for new tarvals.
163 * @param d The (long) double representing the value
164 * @param mode The mode requested for the result tarval
166 * This function creates a new tarval representing the value represented
167 * by a (long) double. If a tarval representing this value already exists,
168 * this tarval is returned instead of a new one. So tarvals are directly
169 * comparable since their representation is unique.
170 * Only modes of sort float_number can be constructed this way.
173 * A tarval of proper type representing the requested value is returned.
174 * Tarvals are unique, so for any value/mode pair at most one tarval will
175 * exist, which will be returned upon further requests with an identical
179 * If the (long) double is not representable in the given mode an assertion
180 * is thrown. This will happen for any mode not of sort float_number.
183 * irmode.h for predefined values
184 * new_tarval_from_str()
185 * new_tarval_from_long()
187 FIRM_API ir_tarval *new_tarval_from_double(double d, ir_mode *mode);
190 * same as new_tarval_from_double(), but with a long double argument
192 FIRM_API ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode);
195 * This returns a double with the value represented value, or
196 * gibberish, depending on the size of double and the size of the
198 * This will overflow silently, so use only if you know what
199 * you are doing! (better check with tarval_is_long...)
201 * @param tv the tarval
203 FIRM_API double get_tarval_double(ir_tarval *tv);
206 * same as get_tarval_double but returns a long double value
208 FIRM_API long double get_tarval_long_double(ir_tarval *tv);
211 * This validates if tarval_to_double() will return a satisfying
212 * result. I.e. if tv is an float_number and between min, max
215 * @param tv the tarval
217 FIRM_API int tarval_is_double(ir_tarval *tv);
220 * Returns the mode of the tarval.
222 * @param tv the tarval
224 FIRM_API ir_mode *get_tarval_mode(const ir_tarval *tv);
227 * Returns 1 if tv is negative
229 * @param tv the tarval
231 FIRM_API int tarval_is_negative(ir_tarval *tv);
234 * Returns 1 if tv is null
236 * @param tv the tarval
238 FIRM_API int tarval_is_null(ir_tarval *tv);
241 * Returns 1 if tv is the "one"
243 * @param tv the tarval
245 FIRM_API int tarval_is_one(ir_tarval *tv);
248 * Returns 1 if tv is the "minus one"
250 * @param tv the tarval
252 FIRM_API int tarval_is_minus_one(ir_tarval *tv);
255 * returns non-zero if all bits in the tarval are set
257 FIRM_API int tarval_is_all_one(ir_tarval *tv);
260 * Returns non-zero if the tarval is a constant (i.e. NOT
261 * a reserved tarval like bad, undef, reachable etc.)
263 FIRM_API int tarval_is_constant(ir_tarval *tv);
265 /** The 'bad' tarval. */
266 FIRM_API ir_tarval *tarval_bad;
267 /** Returns the 'bad' tarval. */
268 FIRM_API ir_tarval *get_tarval_bad(void);
270 /** The 'undefined' tarval. */
271 FIRM_API ir_tarval *tarval_undefined;
272 /** Returns the 'undefined' tarval. */
273 FIRM_API ir_tarval *get_tarval_undefined(void);
275 /** The mode_b tarval 'false'. */
276 FIRM_API ir_tarval *tarval_b_false;
277 /** Returns the mode_b tarval 'false'. */
278 FIRM_API ir_tarval *get_tarval_b_false(void);
280 /** The mode_b tarval 'true'. */
281 FIRM_API ir_tarval *tarval_b_true;
282 /** Returns the mode_b tarval 'true'. */
283 FIRM_API ir_tarval *get_tarval_b_true(void);
285 /** The mode_X tarval 'unreachable'. */
286 FIRM_API ir_tarval *tarval_unreachable;
287 /** Returns the mode_X tarval 'unreachable'. */
288 FIRM_API ir_tarval *get_tarval_unreachable(void);
290 /** The mode_X tarval 'reachable'. */
291 FIRM_API ir_tarval *tarval_reachable;
292 /** Returns the mode_X tarval 'reachable'. */
293 FIRM_API ir_tarval *get_tarval_reachable(void);
295 /** The 'top' tarval. This is just another name for the 'undefined' tarval. */
296 #define tarval_top tarval_undefined
297 /** Returns the 'top' tarval. */
298 #define get_tarval_top() get_tarval_undefined()
300 /** The 'bottom' tarval. This is just another name for the 'bad' tarval. */
301 #define tarval_bottom tarval_bad
302 /** Returns the 'bottom' tarval. */
303 #define get_tarval_bottom() get_tarval_bad()
305 /** Returns the maximum value of a given mode. */
306 FIRM_API ir_tarval *get_tarval_max(ir_mode *mode);
308 /** Returns the minimum value of a given mode. */
309 FIRM_API ir_tarval *get_tarval_min(ir_mode *mode);
311 /** Returns the 0 value (additive neutral) of a given mode.
312 For reference modes, the NULL value is returned (old tarval_P_void) */
313 FIRM_API ir_tarval *get_tarval_null(ir_mode *mode);
315 /** Returns the 1 value (multiplicative neutral) of a given mode. */
316 FIRM_API ir_tarval *get_tarval_one(ir_mode *mode);
318 /** Returns the -1 value (multiplicative neutral) of a given mode.
319 * Returns tarval bad for unsigned modes */
320 FIRM_API ir_tarval *get_tarval_minus_one(ir_mode *mode);
322 /** returns the value where all bits are 1 of a given mode.
323 * returns tarval_bad for float modes */
324 FIRM_API ir_tarval *get_tarval_all_one(ir_mode *mode);
326 /** Returns quite nan for float_number modes. */
327 FIRM_API ir_tarval *get_tarval_nan(ir_mode *mode);
329 /** Returns +inf for float_number modes. */
330 FIRM_API ir_tarval *get_tarval_plus_inf(ir_mode *mode);
332 /** Returns -inf for float_number modes. */
333 FIRM_API ir_tarval *get_tarval_minus_inf(ir_mode *mode);
335 /** Modes for handling integer overflows. */
336 typedef enum tarval_int_overflow_mode_t {
337 TV_OVERFLOW_BAD, /**< tarval module will return tarval_bad if a overflow occurs */
338 TV_OVERFLOW_WRAP, /**< tarval module will overflow will be ignored, wrap around occurs */
339 TV_OVERFLOW_SATURATE /**< tarval module will saturate the overflow */
340 } tarval_int_overflow_mode_t;
343 * Sets the overflow mode for integer operations.
345 * @param ov_mode one of the overflow modes
347 FIRM_API void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
350 * Returns the overflow mode for integer operations.
352 FIRM_API tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
355 * Compares two tarvals
357 * Compare a with b and return their relation.
358 * This is either ir_rel_unordered, ir_rel_less, ir_rel_greater, ir_rel_equal
359 * or ir_rel_false if a or b are symbolic pointers which can not be compared at
362 * @param a the first tarval to be compared
363 * @param b the second tarval to be compared
365 FIRM_API ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b);
368 * Converts a tarval to another mode.
370 * Convert tarval 'src' to mode 'mode', this will succeed if and only if mode
371 * 'mode' is wider than the mode of src, as defined in the firm documentation
372 * and as returned by the function mode_is_smaller defined in irmode.h.
374 * @param src The tarval to convert
375 * @param mode Tho mode to convert to
378 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
379 * tarvals value already exists, it will be returned, else a new tarval is
380 * constructed and returned
383 * Illegal convertions will trigger a panic
386 * FIRM documentation for conversion rules
387 * mode_is_smaller defined in irmode.h
389 FIRM_API ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *mode);
392 * These function implement basic computations representable as opcodes
398 * a - the tarval to operate on
401 * a - the first operand tarval
402 * b - the second operand tarval
405 * If necessary a new tarval is constructed for the resulting value,
406 * or the one already carrying the computation result is retrieved and
407 * returned as result.
410 * The order the arguments are given in is important, imagine postfix
412 * Illegal operations will trigger an assertion.
413 * The sort member of the struct mode defines which operations are valid
417 * Bitwise Negation of a tarval.
419 * @param a the first tarval
421 * @return ~a or tarval_bad
423 FIRM_API ir_tarval *tarval_not(ir_tarval *a);
426 * Arithmetic Negation of a tarval.
428 * @param a the first tarval
430 * @return -a or tarval_bad
432 FIRM_API ir_tarval *tarval_neg(ir_tarval *a);
435 * Addition of two tarvals.
437 * @param a the first tarval
438 * @param b the second tarval
440 * @return a + b or tarval_bad
442 FIRM_API ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b);
445 * Subtraction from a tarval.
447 * @param a the first tarval
448 * @param b the second tarval
449 * @param dst_mode the mode of the result, needed for mode_P - mode_P, else NULL
451 * @return a - b or tarval_bad
453 FIRM_API ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode);
456 * Multiplication of tarvals.
458 * @param a the first tarval
459 * @param b the second tarval
461 * @return a * b or tarval_bad
463 FIRM_API ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b);
466 * Integer division of two tarvals.
468 * @param a the first tarval
469 * @param b the second tarval
471 * @return a / b or tarval_bad
473 FIRM_API ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b);
476 * Remainder of integer division.
478 * @param a the first tarval
479 * @param b the second tarval
481 * @return a % b or tarval_bad
483 FIRM_API ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b);
486 * Integer division AND remainder.
488 * @param a the first tarval
489 * @param b the second tarval
490 * @param mod_res after return, contains the remainder result, a % b or tarval_bad
492 * @return a / b or tarval_bad
494 FIRM_API ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod_res);
497 * Absolute value of a tarval.
499 * @param a the first tarval
501 * @return |a| or tarval_bad
503 FIRM_API ir_tarval *tarval_abs(ir_tarval *a);
506 * Bitwise and of two integer 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_and(ir_tarval *a, ir_tarval *b);
516 * Bitwise and not of two integer 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_andnot(ir_tarval *a, ir_tarval *b);
526 * Bitwise or of two integer 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_or(ir_tarval *a, ir_tarval *b);
536 * Bitwise exclusive or of two integer tarvals.
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_eor(ir_tarval *a, ir_tarval *b);
546 * Logical Left shift.
548 * @param a the first tarval
549 * @param b the second tarval
551 * @return a << b or tarval_bad
553 FIRM_API ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b);
556 * logical left shift (variant with unsigned argument).
559 FIRM_API ir_tarval *tarval_shl_unsigned(ir_tarval *a, unsigned b);
562 * Unsigned (logical) right shift.
564 * @param a the first tarval
565 * @param b the second tarval
567 * @return a >>u b or tarval_bad
569 FIRM_API ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b);
572 * unsigned (logical) right shift (variant with unsigned argument).
575 FIRM_API ir_tarval *tarval_shr_unsigned(ir_tarval *a, unsigned b);
578 * Signed (arithmetic) right shift.
580 * @param a the first tarval
581 * @param b the second tarval
583 * @return a >>s b or tarval_bad
585 FIRM_API ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b);
588 * signed (arithmetic) right shift (variant with unsigned argument).
591 FIRM_API ir_tarval *tarval_shrs_unsigned(ir_tarval *a, unsigned b);
596 * @param a the first tarval
597 * @param b the second tarval
599 * @return a \<\<L\>\> b or tarval_bad
601 FIRM_API ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b);
604 * The output mode for tarval values.
606 * Some modes allow more that one representation, for instance integers
607 * can be represented hex or decimal. Of course it would be enough to have
608 * one and let every backend convert it into the 'right' one.
609 * However, we can do this in the tarval much simpler...
612 TVO_NATIVE, /**< the default output mode, depends on the mode */
613 TVO_HEX, /**< use hex representation, always possible */
614 TVO_DECIMAL, /**< use decimal representation */
615 TVO_OCTAL, /**< use octal representation */
616 TVO_BINARY, /**< use binary representation */
617 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
618 TVO_HEXFLOAT /**< use hexadecimal floating point representation
623 * This structure contains helper information to format the output
624 * of a tarval of a mode.
626 typedef struct tarval_mode_info {
627 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
628 const char *mode_prefix; /**< if set, this prefix will be printed
629 before a value of this mode */
630 const char *mode_suffix; /**< if set, this suffix will be printed
631 after a value of this mode */
635 * Specify the output options of one mode.
637 * This functions stores the mode info, so DO NOT DESTROY it.
639 * @param mode a ir_mode that should be associated
640 * @param modeinfo the output format info
642 * @return zero on success.
644 FIRM_API int set_tarval_mode_output_option(ir_mode *mode,
645 const tarval_mode_info *modeinfo);
648 * Returns the output options of one mode.
650 * This functions returns the mode info of a given mode.
652 * @param mode a ir_mode that should be associated
654 * @return the output option
656 FIRM_API const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
659 * Returns Bit representation of a tarval value, as string of '0' and '1'
661 * @param tv The tarval
663 * This function returns a printable bit representation of any value
664 * stored as tarval. This representation is a null terminated C string.
667 * As usual in C a pointer to a char is returned. The length of the
668 * returned string if fixed, just read as many chars as the mode defines
672 * The string is allocated using malloc() and is free()ed on the next call
674 * The string consists of the ASCII characters '0' and '1' and is
678 * irmode.h for the definition of the ir_mode struct
679 * the size member of aforementioned struct
681 FIRM_API char *get_tarval_bitpattern(ir_tarval *tv);
684 * Returns the bitpattern of the bytes_ofs byte.
686 * This function succeeds even if the mode of the tarval uses lesser bits
687 * than requested, in that case the bitpattern is filled with zero bits.
689 * To query a 32bit value the following code can be used:
691 * val0 = tarval_sub_bits(tv, 0); <- lowest bits
692 * val1 = tarval_sub_bits(tv, 1);
693 * val2 = tarval_sub_bits(tv, 2);
694 * val3 = tarval_sub_bits(tv, 3); <- highest bits
696 * Because this is the bit representation of the target machine, only the
697 * following operations are legal on the result:
699 * - concatenation (endian dependence MUST be handled by the CALLER)
700 * - bitwise logical operations to select/mask bits
702 * @param tv the tarval
703 * @param byte_ofs the byte offset from lower to higher
706 * The result of this function is undefined if the mode is neither integer
709 FIRM_API unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs);
712 * Returns non-zero if a given (integer) tarval has only one single bit
715 * @param tv the tarval
717 FIRM_API int tarval_is_single_bit(ir_tarval *tv);
720 * Returns the number of set bits in a given (integer) tarval.
722 * @param tv the tarval
724 * @return number of set bits or -1 on error
726 FIRM_API int get_tarval_popcount(ir_tarval *tv);
729 * Returns the number of the lowest set bit in a given (integer) tarval.
731 * @param tv the tarval
733 * @return number of lowest set bit or -1 on error
735 FIRM_API int get_tarval_lowest_bit(ir_tarval *tv);
738 * Output a tarval to a string buffer.
740 * @param buf the output buffer
741 * @param buflen the length of the buffer
742 * @param tv the tarval
744 FIRM_API int tarval_snprintf(char *buf, size_t buflen, ir_tarval *tv);
747 * Output a tarval to stdio.
749 * @param tv the tarval
751 FIRM_API int tarval_printf(ir_tarval *tv);
754 * Returns non-zero if the mantissa of a floating point tarval is zero
757 * @param tv the tarval
759 FIRM_API int tarval_zero_mantissa(ir_tarval *tv);
762 * Returns the exponent of a floating point IEEE-754
765 * @param tv the tarval
767 FIRM_API int tarval_get_exponent(ir_tarval *tv);
770 * Check if the tarval can be converted to the given mode without
773 * @param tv the tarval
774 * @param mode the mode to convert to
776 FIRM_API int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode);
779 * Returns non-zero if the result of the last IEEE-754 operation was exact.
781 FIRM_API unsigned tarval_ieee754_get_exact(void);
784 * Check if its the a floating point NaN.
786 * @param tv the tarval
788 FIRM_API int tarval_is_NaN(ir_tarval *tv);
791 * Check if its the a floating point +inf.
793 * @param tv the tarval
795 FIRM_API int tarval_is_plus_inf(ir_tarval *tv);
798 * Check if its the a floating point -inf.
800 * @param tv the tarval
802 FIRM_API int tarval_is_minus_inf(ir_tarval *tv);
805 * Check if the tarval represents a finite value, ie neither NaN nor inf.
807 * @param tv the tarval
809 FIRM_API int tarval_is_finite(ir_tarval *tv);
812 * Checks whether a pointer points to a tarval.
814 * @param thing an arbitrary pointer
817 * true if the thing is a tarval, else false
819 FIRM_API int is_tarval(const void *thing);