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"
56 /* ************************ Constructors for tarvals ************************ */
59 * Constructor function for new tarvals.
61 * @param str The string representing the target value
62 * @param len The length of the string
63 * @param mode The mode requested for the result tarval
65 * This function creates a new tarval representing the value represented
66 * by a CString, aka char array. If a tarval representing this value already
67 * exists, this tarval is returned instead of a new one. So tarvals are
68 * directly comparable since their representation is unique.
70 * This function accepts the following strings:
72 * if mode is int_number:
73 * - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
74 * - 0[0-7]* (octal representation)
75 * - (+|-)?[1-9][0-9]* (decimal representation)
77 * if mode if float_number:
78 * - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
80 * if mode is boolean: true, True, TRUE ... False... 0, 1,
82 * if mode is reference: hexadecimal of decimal number as int
84 * if mode is character: hex or dec
86 * Leading and/or trailing spaces are ignored
89 * A tarval of proper type representing the requested value is returned.
90 * Tarvals are unique, so for any value/mode pair at most one tarval will
91 * exist, which will be returned upon further requests with an identical
95 * If the string is not representable in the given mode an assertion is
96 * thrown in assert build.
99 * irmode.h for predefined modes
100 * new_tarval_from_long()
101 * new_tarval_from_double()
103 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
106 * Constructor function for new tarvals
108 * @param l The long representing the value
109 * @param mode The mode requested for the result tarval
111 * This function creates a new tarval representing the value represented
112 * by a long integer. If a tarval representing this value already exists,
113 * this tarval is returned instead of a new one. So tarvals are directly
114 * comparable since their representation is unique.
117 * A tarval of proper type representing the requested value is returned.
118 * Tarvals are unique, so for any value/mode pair at most one tarval will
119 * exist, which will be returned upon further requests with an identical
123 * If the long is not representable in the given mode an assertion is
124 * thrown in assert build.
127 * irmode.h for predefined modes
128 * new_tarval_from_str()
129 * new_tarval_from_double()
132 tarval *new_tarval_from_long(long l, ir_mode *mode);
134 /** Return value as long if possible.
136 * This returns a long int with the value represented value, or
137 * gibberish, depending on the size of long int and the size of the
138 * stored value. It works for e.g. 1 as mode_Ls, but might not work for
139 * get_mode_max(mode_Ls).
140 * This will overflow silently, so use only if you know what
141 * you are doing! (better check with tarval_is_long()...)
142 * Works only for int modes, even not for character modes!
144 long get_tarval_long(tarval *tv);
147 * This validates if get_tarval_long() will return a satisfying
148 * result. I.e. if tv is an int_number and between min, max
149 * of long int (signed!)
151 * @param tv the tarval
153 int tarval_is_long(tarval *tv);
156 * Constructor function for new tarvals.
158 * @param d The (long) double representing the value
159 * @param mode The mode requested for the result tarval
161 * This function creates a new tarval representing the value represented
162 * by a (long) double. If a tarval representing this value already exists,
163 * this tarval is returned instead of a new one. So tarvals are directly
164 * comparable since their representation is unique.
165 * Only modes of sort float_number can be constructed this way.
168 * A tarval of proper type representing the requested value is returned.
169 * Tarvals are unique, so for any value/mode pair at most one tarval will
170 * exist, which will be returned upon further requests with an identical
174 * If the (long) double is not representable in the given mode an assertion
175 * is thrown. This will happen for any mode not of sort float_number.
178 * irmode.h for predefined values
179 * new_tarval_from_str()
180 * new_tarval_from_long()
182 tarval *new_tarval_from_double(long double d, ir_mode *mode);
185 * This returns a double with the value represented value, or
186 * gibberish, depending on the size of double and the size of the
188 * This will overflow silently, so use only if you know what
189 * you are doing! (better check with tarval_is_long...)
191 * @param tv the tarval
193 long double get_tarval_double(tarval *tv);
196 * This validates if tarval_to_double() will return a satisfying
197 * result. I.e. if tv is an float_number and between min, max
200 * @param tv the tarval
202 int tarval_is_double(tarval *tv);
205 /** ********** Access routines for tarval fields ********** **/
213 * ir_mode *get_tarval_mode(tarval *tv)
217 * These are access function for tarval struct members. It is encouraged
218 * to use them instead of direct access to the struct fields.
221 * tv - The tarval to access fields of
224 * get_tv_mode: The mode of the tarval
231 * Returns the mode of the tarval.
233 * @param tv the tarval
235 ir_mode *get_tarval_mode(const tarval *tv);
237 /** Returns the contents of the 'link' field of the tarval */
238 /* void *get_tarval_link (tarval*); */
240 /* Testing properties of the represented values */
243 * Returns 1 if tv is negative
245 * @param tv the tarval
247 int tarval_is_negative(tarval *tv);
250 * Returns 1 if tv is null
252 * @param tv the tarval
254 int tarval_is_null(tarval *tv);
257 * Returns 1 if tv is the "one"
259 * @param tv the tarval
261 int tarval_is_one(tarval *tv);
264 * Returns 1 if tv is the "minus one"
266 * @param tv the tarval
268 int tarval_is_minus_one(tarval *tv);
271 * returns non-zero if all bits in the tarval are set
273 int tarval_is_all_one(tarval *tv);
275 /** The 'bad' tarval. */
276 extern tarval *tarval_bad;
277 /** Returns the 'bad tarval. */
278 tarval *get_tarval_bad(void);
280 /** The 'undefined' tarval. */
281 extern tarval *tarval_undefined;
282 /** Returns the 'undefined' tarval. */
283 tarval *get_tarval_undefined(void);
285 /** The mode_b tarval 'false'. */
286 extern tarval *tarval_b_false;
288 /** Returns the mode_b tarval 'false'. */
289 tarval *get_tarval_b_false(void);
291 /** The mode_b tarval 'true'. */
292 extern tarval *tarval_b_true;
293 /** Returns the mode_b tarval 'true'. */
294 tarval *get_tarval_b_true(void);
296 /* These functions calculate and return a tarval representing the requested
298 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
299 * functions, but these are stored on initialization of the irmode module and
300 * therefore the irmode functions should be preferred to the functions below. */
302 /** Returns the maximum value of a given mode. */
303 tarval *get_tarval_max(ir_mode *mode);
305 /** Returns the minimum value of a given mode. */
306 tarval *get_tarval_min(ir_mode *mode);
308 /** Returns the 0 value (additive neutral) of a given mode.
309 For reference modes, the NULL value is returned (old tarval_P_void) */
310 tarval *get_tarval_null(ir_mode *mode);
312 /** Returns the 1 value (multiplicative neutral) of a given mode. */
313 tarval *get_tarval_one(ir_mode *mode);
315 /** Returns the -1 value (multiplicative neutral) of a given mode.
316 * Returns tarval bad for unsigned modes */
317 tarval *get_tarval_minus_one(ir_mode *mode);
319 /** returns the value where all bits are 1 of a given mode.
320 * returns tarval_bad for float modes */
321 tarval *get_tarval_all_one(ir_mode *mode);
323 /** Return quite nan for float_number modes. */
324 tarval *get_tarval_nan(ir_mode *mode);
326 /** Return +inf for float_number modes. */
327 tarval *get_tarval_plus_inf(ir_mode *mode);
329 /** Return -inf for float_number modes. */
330 tarval *get_tarval_minus_inf(ir_mode *mode);
332 /* ******************** Arithmetic operations on tarvals ******************** */
334 typedef enum _tarval_int_overflow_mode_t {
335 TV_OVERFLOW_BAD, /**< tarval module will return tarval_bad if a overflow occurs */
336 TV_OVERFLOW_WRAP, /**< tarval module will overflow will be ignored, wrap around occurs */
337 TV_OVERFLOW_SATURATE /**< tarval module will saturate the overflow */
338 } tarval_int_overflow_mode_t;
341 * Sets the overflow mode for integer operations.
343 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
346 * Get the overflow mode for integer operations.
348 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
351 * Compares two tarvals
353 * Compare a with b and return a pn_Cmp describing the relation
354 * between a and b. This is either pn_Cmp_Uo, pn_Cmp_Lt, pn_Cmp_Eq, pn_Cmp_Gt,
355 * or pn_Cmp_False if a or b are symbolic pointers which can not be compared at all.
357 * @param a the first tarval to be compared
358 * @param b the second tarval to be compared
361 * The pn_Cmp best describing the relation between a and b is returned.
362 * This means the mode with the least bits set is returned, e.g. if the
363 * tarvals are equal the pn_Cmp 'pn_Cmp_Eq' is returned, not 'pn_Cmp_Ge' which
364 * indicates 'greater or equal'
367 * irnode.h for the definition of pn_Cmp
369 pn_Cmp tarval_cmp(tarval *a, tarval *b);
372 * Converts a tarval to another mode.
374 * Convert tarval 'src' to mode 'mode', this will succeed if and only if mode
375 * 'mode' is wider than the mode of src, as defined in the firm documentation
376 * and as returned by the function mode_is_smaller defined in irmode.h.
378 * @param src The tarval to convert
379 * @param mode Tho mode to convert to
382 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
383 * tarvals value already exists, it will be returned, else a new tarval is
384 * constructed and returned
387 * Illegal convertions will trigger a panic
390 * FIRM documentation for conversion rules
391 * mode_is_smaller defined in irmode.h
393 tarval *tarval_convert_to(tarval *src, ir_mode *mode);
396 * These function implement basic computations representable as opcodes
402 * a - the tarval to operate on
405 * a - the first operand tarval
406 * b - the second operand tarval
409 * If necessary a new tarval is constructed for the resulting value,
410 * or the one already carrying the computation result is retrieved and
411 * returned as result.
414 * The order the arguments are given in is important, imagine postfix
416 * Illegal operations will trigger an assertion.
417 * The sort member of the struct mode defines which operations are valid
420 /** Bitwise Negation of a tarval. */
421 tarval *tarval_not(tarval *a);
423 /** Arithmetic Negation of a tarval. */
424 tarval *tarval_neg(tarval *a);
426 /** Addition of two tarvals. */
427 tarval *tarval_add(tarval *a, tarval *b);
429 /** Subtraction from a tarval. */
430 tarval *tarval_sub(tarval *a, tarval *b);
432 /** Multiplication of tarvals. */
433 tarval *tarval_mul(tarval *a, tarval *b);
435 /** 'Exact' division of two tarvals. */
436 tarval *tarval_quo(tarval *a, tarval *b);
438 /** Integer division of two tarvals. */
439 tarval *tarval_div(tarval *a, tarval *b);
441 /** Remainder of integer division. */
442 tarval *tarval_mod(tarval *a, tarval *b);
444 /** Integer division AND remainder. */
445 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod_res);
447 /** Absolute value of a tarval. */
448 tarval *tarval_abs(tarval *a);
451 tarval *tarval_and(tarval *a, tarval *b);
454 tarval *tarval_or(tarval *a, tarval *b);
456 /** Bitwise exclusive or. */
457 tarval *tarval_eor(tarval *a, tarval *b);
460 tarval *tarval_shl(tarval *a, tarval *b);
462 /** Unsigned (logical) right shift. */
463 tarval *tarval_shr(tarval *a, tarval *b);
465 /** Signed (arithmetic) right shift. */
466 tarval *tarval_shrs(tarval *a, tarval *b);
468 /** Rotation to left. */
469 tarval *tarval_rotl(tarval *a, tarval *b);
472 * Returns the carry flag of the last operation.
474 int tarval_carry(void);
476 /* *********** Output of tarvals *********** */
479 * The output mode for tarval values.
481 * Some modes allow more that one representation, for instance integers
482 * can be represented hex or decimal. Of course it would be enough to have
483 * one and let every backend convert it into the 'right' one.
484 * However, we can do this in the tarval much simpler...
487 TVO_NATIVE, /**< the default output mode, depends on the mode */
488 TVO_HEX, /**< use hex representation, always possible */
489 TVO_DECIMAL, /**< use decimal representation */
490 TVO_OCTAL, /**< use octal representation */
491 TVO_BINARY, /**< use binary representation */
492 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
493 TVO_HEXFLOAT /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
497 * This structure contains helper information to format the output
498 * of a tarval of a mode.
500 typedef struct tarval_mode_info {
501 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
502 const char *mode_prefix; /**< if set, this prefix will be printed
503 before a value of this mode */
504 const char *mode_suffix; /**< if set, this suffix will be printed
505 after a value of this mode */
509 * Specify the output options of one mode.
511 * This functions stores the mode info, so DO NOT DESTROY it.
513 * @param mode a ir_mode that should be associated
514 * @param modeinfo the output format info
516 * @return zero on success.
518 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
521 * Returns the output options of one mode.
523 * This functions returns the mode info of a given mode.
525 * @param mode a ir_mode that should be associated
527 * @return the output option
529 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
532 * Returns Bit representation of a tarval value, as string of '0' and '1'
534 * @param tv The tarval
536 * This function returns a printable bit representation of any value
537 * stored as tarval. This representation is a null terminated C string.
540 * As usual in C a pointer to a char is returned. The length of the
541 * returned string if fixed, just read as many chars as the mode defines
545 * The string is allocated using malloc() and is free()ed on the next call
547 * The string consists of the ASCII characters '0' and '1' and is
551 * irmode.h for the definition of the ir_mode struct
552 * the size member of aforementioned struct
554 char *get_tarval_bitpattern(tarval *tv);
557 * Returns the bitpattern of the bytes_ofs byte.
559 * This function succeeds even if the mode of the tarval uses lesser bits
560 * than requested, in that case the bitpattern is filled with zero bits.
562 * To query a 32bit value the following code can be used:
564 * val0 = tarval_sub_bits(tv, 0);
565 * val1 = tarval_sub_bits(tv, 1);
566 * val2 = tarval_sub_bits(tv, 2);
567 * val3 = tarval_sub_bits(tv, 3);
569 * Because this is the bit representation of the target machine, only the following
570 * operations are legal on the result:
572 * - concatenation (endian dependence MUST be handled by the CALLER)
573 * - bitwise logical operations to select/mask bits
575 * @param tv the tarval
576 * @param byte_ofs the byte offset
579 * The result of this function is undefined if the mode is neither integer nor float.
581 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs);
584 * Returns non-zero if a given (integer) tarval has only one single bit
587 * @param tv the tarval
589 int tarval_is_single_bit(tarval *tv);
592 * Output a tarval to a string buffer.
594 * @param buf the output buffer
595 * @param buflen the length of the buffer
596 * @param tv the tarval
598 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
601 * Output a tarval to stdio.
603 * @param tv the tarval
605 int tarval_printf(tarval *tv);
608 * Returns non-zero if the mantissa of a floating point IEEE-754
609 * tarval is zero (i.e. 1.0Exxx)
611 * @param tv the tarval
613 int tarval_ieee754_zero_mantissa(tarval *tv);
616 * Returns the exponent of a floating point IEEE-754
619 * @param tv the tarval
621 int tarval_ieee754_get_exponent(tarval *tv);
624 * Check if the tarval can be converted to the given mode without
627 * @param tv the tarval
628 * param mode the mode to convert to
630 int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode);
633 * Set the immediate precision for IEEE-754 results. Set this to
634 * 0 to get the same precision as the operands.
635 * For x87 compatibility, set this to 80.
637 * @return the old setting
639 unsigned tarval_ieee754_set_immediate_precision(unsigned bits);
642 * Returns non-zero if the result of the last IEEE-754 operation was exact.
644 unsigned tarval_ieee754_get_exact(void);
647 * Enable/Disable floating point constant folding.
649 int tarval_enable_fp_ops(int enable);
652 * Check if its the a floating point NaN.
654 * @param tv the tarval
656 int tarval_is_NaN(tarval *tv);
659 * Check if its the a floating point +inf.
661 * @param tv the tarval
663 int tarval_is_plus_inf(tarval *tv);
666 * Check if its the a floating point -inf.
668 * @param tv the tarval
670 int tarval_is_minus_inf(tarval *tv);
673 * Check if the tarval represents a finite value, ie neither NaN nor inf.
675 * @param tv the tarval
677 int tarval_is_finite(tarval *tv);
679 #endif /* FIRM_TV_TV_H */