2 * Copyright (C) 1995-2007 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 int tarval_is_long(tarval *tv);
154 * Constructor function for new tarvals.
156 * @param d The (long) double representing the value
157 * @param mode The mode requested for the result tarval
159 * This function creates a new tarval representing the value represented
160 * by a (long) double. If a tarval representing this value already exists,
161 * this tarval is returned instead of a new one. So tarvals are directly
162 * comparable since their representation is unique.
163 * Only modes of sort float_number can be constructed this way.
166 * A tarval of proper type representing the requested value is returned.
167 * Tarvals are unique, so for any value/mode pair at most one tarval will
168 * exist, which will be returned upon further requests with an identical
172 * If the (long) double is not representable in the given mode an assertion
173 * is thrown. This will happen for any mode not of sort float_number.
176 * irmode.h for predefined values
177 * new_tarval_from_str()
178 * new_tarval_from_long()
180 tarval *new_tarval_from_double(long double d, ir_mode *mode);
183 * This returns a double with the value represented value, or
184 * gibberish, depending on the size of double and the size of the
186 * This will overflow silently, so use only if you know what
187 * you are doing! (better check with tarval_is_long...)
189 long double get_tarval_double(tarval *tv);
192 * This validates if tarval_to_double() will return a satisfying
193 * result. I.e. if tv is an float_number and between min, max
196 int tarval_is_double(tarval *tv);
199 /** ********** Access routines for tarval fields ********** **/
207 * ir_mode *get_tarval_mode(tarval *tv)
211 * These are access function for tarval struct members. It is encouraged
212 * to use them instead of direct access to the struct fields.
215 * tv - The tarval to access fields of
218 * get_tv_mode: The mode of the tarval
224 /** Returns the mode of the tarval. */
225 ir_mode *get_tarval_mode (const tarval *tv);
227 /** Returns the contents of the 'link' field of the tarval */
228 /* void *get_tarval_link (tarval*); */
230 /* Testing properties of the represented values */
233 * Returns 1 if tv is negative
235 * @param a the tarval
237 int tarval_is_negative(tarval *a);
240 * Returns 1 if tv is null
242 * @param a the tarval
244 int tarval_is_null(tarval *a);
247 * Returns 1 if tv is the "one"
249 * @param a the tarval
251 int tarval_is_one(tarval *a);
254 * Returns 1 if tv is the "minus one"
256 * @param a the tarval
258 int tarval_is_minus_one(tarval *a);
261 * returns non-zero if all bits in the tarval are set
263 int tarval_is_all_one(tarval *tv);
265 /** The 'bad' tarval. */
266 extern tarval *tarval_bad;
267 /** Returns the 'bad tarval. */
268 tarval *get_tarval_bad(void);
270 /** The 'undefined' tarval. */
271 extern tarval *tarval_undefined;
272 /** Returns the 'undefined' tarval. */
273 tarval *get_tarval_undefined(void);
275 /** The mode_b tarval 'false'. */
276 extern tarval *tarval_b_false;
278 /** Returns the mode_b tarval 'false'. */
279 tarval *get_tarval_b_false(void);
281 /** The mode_b tarval 'true'. */
282 extern tarval *tarval_b_true;
283 /** Returns the mode_b tarval 'true'. */
284 tarval *get_tarval_b_true(void);
286 /* These functions calculate and return a tarval representing the requested
288 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
289 * functions, but these are stored on initialization of the irmode module and
290 * therefore the irmode functions should be preferred to the functions below. */
292 /** Returns the maximum value of a given mode. */
293 tarval *get_tarval_max(ir_mode *mode);
295 /** Returns the minimum value of a given mode. */
296 tarval *get_tarval_min(ir_mode *mode);
298 /** Returns the 0 value (additive neutral) of a given mode.
299 For reference modes, the NULL value is returned (old tarval_P_void) */
300 tarval *get_tarval_null(ir_mode *mode);
302 /** Returns the 1 value (multiplicative neutral) of a given mode. */
303 tarval *get_tarval_one(ir_mode *mode);
305 /** Returns the -1 value (multiplicative neutral) of a given mode.
306 * Returns tarval bad for unsigned modes */
307 tarval *get_tarval_minus_one(ir_mode *mode);
309 /** returns the value where all bits are 1 of a given mode.
310 * returns tarval_bad for float modes */
311 tarval *get_tarval_all_one(ir_mode *mode);
313 /** Return quite nan for float_number modes. */
314 tarval *get_tarval_nan(ir_mode *mode);
316 /** Return +inf for float_number modes. */
317 tarval *get_tarval_plus_inf(ir_mode *mode);
319 /** Return -inf for float_number modes. */
320 tarval *get_tarval_minus_inf(ir_mode *mode);
322 /* ******************** Arithmetic operations on tarvals ******************** */
324 typedef enum _tarval_int_overflow_mode_t {
325 TV_OVERFLOW_BAD, /**< tarval module will return tarval_bad if a overflow occurs */
326 TV_OVERFLOW_WRAP, /**< tarval module will overflow will be ignored, wrap around occurs */
327 TV_OVERFLOW_SATURATE /**< tarval module will saturate the overflow */
328 } tarval_int_overflow_mode_t;
331 * Sets the overflow mode for integer operations.
333 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
336 * Get the overflow mode for integer operations.
338 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
341 * Compares two tarvals
343 * Compare a with b and return a pn_Cmp describing the relation
344 * between a and b. This is either pn_Cmp_Uo, pn_Cmp_Lt, pn_Cmp_Eq, pn_Cmp_Gt,
345 * or pn_Cmp_False if a or b are symbolic pointers which can not be compared at all.
347 * @param a A tarval to be compared
348 * @param b A tarval to be compared
351 * The pn_Cmp best describing the relation between a and b is returned.
352 * This means the mode with the least bits set is returned, e.g. if the
353 * tarvals are equal the pn_Cmp 'pn_Cmp_Eq' is returned, not 'pn_Cmp_Ge' which
354 * indicates 'greater or equal'
357 * irnode.h for the definition of pn_Cmp
359 pn_Cmp tarval_cmp(tarval *a, tarval *b);
362 * Converts a tarval to another mode.
364 * Convert tarval 'src' to mode 'mode', this will succeed if and only if mode
365 * 'mode' is wider than the mode of src, as defined in the firm documentation
366 * and as returned by the function mode_is_smaller defined in irmode.h.
368 * @param src The tarval to convert
369 * @param mode Tho mode to convert to
372 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
373 * tarvals value already exists, it will be returned, else a new tarval is
374 * constructed and returned
377 * Illegal conversations will trigger an assertion
380 * FIRM documentation for conversion rules
381 * mode_is_smaller defined in irmode.h
383 tarval *tarval_convert_to(tarval *src, ir_mode *mode);
386 * These function implement basic computations representable as opcodes
392 * a - the tarval to operate on
395 * a - the first operand tarval
396 * b - the second operand tarval
399 * If necessary a new tarval is constructed for the resulting value,
400 * or the one already carrying the computation result is retrieved and
401 * returned as result.
404 * The order the arguments are given in is important, imagine postfix
406 * Illegal operations will trigger an assertion.
407 * The sort member of the struct mode defines which operations are valid
410 /** bitwise Negation of a tarval. */
411 tarval *tarval_not(tarval *a);
413 /** arithmetic Negation of a tarval. */
414 tarval *tarval_neg(tarval *a);
416 /** Addition of two tarvals. */
417 tarval *tarval_add(tarval *a, tarval *b);
419 /** Subtraction from a tarval. */
420 tarval *tarval_sub(tarval *a, tarval *b);
422 /** Multiplication of tarvals. */
423 tarval *tarval_mul(tarval *a, tarval *b);
425 /** 'Exact' division. */
426 tarval *tarval_quo(tarval *a, tarval *b);
428 /** Integer division. */
429 tarval *tarval_div(tarval *a, tarval *b);
431 /** Remainder of integer division. */
432 tarval *tarval_mod(tarval *a, tarval *b);
434 /** Integer division AND remainder. */
435 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod_res);
437 /** Absolute value. */
438 tarval *tarval_abs(tarval *a);
441 tarval *tarval_and(tarval *a, tarval *b);
444 tarval *tarval_or(tarval *a, tarval *b);
446 /** Bitwise exclusive or. */
447 tarval *tarval_eor(tarval *a, tarval *b);
450 tarval *tarval_shl(tarval *a, tarval *b);
452 /** Unsigned (logical) right shift. */
453 tarval *tarval_shr(tarval *a, tarval *b);
455 /** Signed (arithmetic) right shift. */
456 tarval *tarval_shrs(tarval *a, tarval *b);
459 tarval *tarval_rot(tarval *a, tarval *b);
461 /** Carry flag of the last operation */
462 int tarval_carry(void);
464 /* *********** Output of tarvals *********** */
467 * The output mode for tarval values.
469 * Some modes allow more that one representation, for instance integers
470 * can be represented hex or decimal. Of course it would be enough to have
471 * one and let every backend convert it into the 'right' one.
472 * However, we can do this in the tarval much simpler...
475 TVO_NATIVE, /**< the default output mode, depends on the mode */
476 TVO_HEX, /**< use hex representation, always possible */
477 TVO_DECIMAL, /**< use decimal representation */
478 TVO_OCTAL, /**< use octal representation */
479 TVO_BINARY, /**< use binary representation */
480 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
481 TVO_HEXFLOAT /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
485 * This structure contains helper information to format the output
486 * of a tarval of a mode.
488 typedef struct tarval_mode_info {
489 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
490 const char *mode_prefix; /**< if set, this prefix will be printed
491 before a value of this mode */
492 const char *mode_suffix; /**< if set, this suffix will be printed
493 after a value of this mode */
497 * Specify the output options of one mode.
499 * This functions stores the mode info, so DO NOT DESTROY it.
501 * @param mode a ir_mode that should be associated
502 * @param modeinfo the output format info
504 * @return zero on success.
506 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
509 * Returns the output options of one mode.
511 * This functions returns the mode info of a given mode.
513 * @param mode a ir_mode that should be associated
515 * @return the output option
517 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
520 * Returns Bit representation of a tarval value, as string of '0' and '1'
522 * @param tv The tarval
524 * This function returns a printable bit representation of any value
525 * stored as tarval. This representation is a null terminated C string.
528 * As usual in C a pointer to a char is returned. The length of the
529 * returned string if fixed, just read as many chars as the mode defines
533 * The string is allocated using malloc() and is free()ed on the next call
535 * The string consists of the ASCII characters '0' and '1' and is
539 * irmode.h for the definition of the ir_mode struct
540 * the size member of aforementioned struct
542 char *get_tarval_bitpattern(tarval *tv);
545 * Returns the bitpattern of the bytes_ofs byte.
547 * This function succeeds even if the mode of the tarval uses lesser bits
548 * than requested, in that case the bitpattern is filled with zero bits.
550 * To query a 32bit value the following code can be used:
552 * val0 = tarval_sub_bits(tv, 0);
553 * val1 = tarval_sub_bits(tv, 1);
554 * val2 = tarval_sub_bits(tv, 2);
555 * val3 = tarval_sub_bits(tv, 3);
557 * Because this is the bit representation of the target machine, only the following
558 * operations are legal on the result:
560 * - concatenation (endian dependence MUST be handled by the CALLER)
561 * - bitwise logical operations to select/mask bits
563 * @param tv the tarval
564 * @param byte_ofs the byte offset
567 * The result of this function is undefined if the mode is neither integer nor float.
569 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs);
572 * Returns non-zero if a given (integer) tarval has only one single bit
575 int tarval_is_single_bit(tarval *tv);
578 * Output of tarvals to a buffer.
580 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
583 * Output of tarvals to stdio.
585 int tarval_printf(tarval *tv);
588 * Returns non-zero if the mantissa of a floating point IEEE-754
589 * tarval is zero (i.e. 1.0Exxx)
591 int tarval_ieee754_zero_mantissa(tarval *tv);
594 * Returns the exponent of a floating point IEEE-754
597 int tarval_ieee754_get_exponent(tarval *tv);
600 * Set the immediate precision for IEEE-754 results. Set this to
601 * 0 to get the same precision as the operands.
602 * For x87 compatibility, set this to 80.
604 * @return the old setting
606 unsigned tarval_ieee754_set_immediate_precision(unsigned bits);
609 * Returns non-zero if the result of the last IEEE-754 operation was exact.
611 unsigned tarval_ieee754_get_exact(void);
614 * Enable/Disable floating point constant folding.
616 int tarval_enable_fp_ops(int enable);
618 #endif /* FIRM_TV_TV_H */