3 * File name: ir/tv/tv.h
4 * Purpose: Representation of and static computations on target machine
10 * Copyright: (c) 2003 Universität Karlsruhe
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
17 * Declarations for Target Values.
24 /* # include "entity.h" */
25 # include "irnode.h" /* for pnc_number enum */
31 * tv -- TargetValue, short tarval.
32 * Internal representation for machine values.
38 * Tarvals represent target machine values. They are typed by modes.
39 * Tarvals only represent values of mode_sort:
46 * In case of references the module accepts an entity to represent the
48 * Furthermore, computations and conversions of these values can
52 * This module is closely related to the irmode module, as the modes
53 * defined there are thoroughly used throughout the whole module.
54 * Also, the comparison functions rely on the definition of comparison
55 * values in the irnode module.
58 * The original tv module originated in the fiasco compiler written ...
59 * This is the new version, described in the tech report 1999-14 by ...
63 * irmode.h for the modes definitions
64 * irnode.h for the pnc_numbers table
66 * tarval_init1 and tarval_init2 for initialization of the
71 #ifndef _TARVAL_TYPEDEF_
72 #define _TARVAL_TYPEDEF_
73 typedef struct tarval tarval;
76 /* ************************ Constructors for tarvals ************************ */
79 * Constructor function for new tarvals.
81 * @param str The string representing the target value
82 * @param len The length of the string
83 * @param mode The mode requested for the result tarval
85 * This function creates a new tarval representing the value represented
86 * by a CString, aka char array. If a tarval representing this value already
87 * exists, this tarval is returned instead of a new one. So tarvals are
88 * directly comparable since their representation is unique.
90 * This function accepts the following strings:
92 * if mode is int_number:
93 * - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
94 * - 0[0-7]* (octal representation)
95 * - (+|-)?[1-9][0-9]* (decimal representation)
97 * if mode if float_number:
98 * - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
100 * if mode is boolean: true, True, TRUE ... False... 0, 1,
102 * if mode is reference: hexadecimal of decimal number as int
104 * if mode is character: hex or dec
106 * Leading and/or trailing spaces are ignored
109 * A tarval of proper type representing the requested value is returned.
110 * Tarvals are unique, so for any value/mode pair at most one tarval will
111 * exist, which will be returned upon further requests with an identical
115 * If the string is not representable in the given mode an assertion is
116 * thrown in assert build.
119 * irmode.h for predefined modes
120 * new_tarval_from_long()
121 * new_tarval_from_double()
123 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
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 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 long get_tarval_long(tarval *tv);
167 * This validates if tarval_to_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 int tarval_is_long(tarval *tv);
174 * Constructor function for new tarvals.
176 * @param d The (long) double representing the value
177 * @param mode The mode requested for the result tarval
179 * This function creates a new tarval representing the value represented
180 * by a (long) double. If a tarval representing this value already exists,
181 * this tarval is returned instead of a new one. So tarvals are directly
182 * comparable since their representation is unique.
183 * Only modes of sort float_number can be constructed this way.
186 * A tarval of proper type representing the requested value is returned.
187 * Tarvals are unique, so for any value/mode pair at most one tarval will
188 * exist, which will be returned upon further requests with an identical
192 * If the (long) double is not representable in the given mode an assertion
193 * is thrown. This will happen for any mode not of sort float_number.
196 * irmode.h for predefined values
197 * new_tarval_from_str()
198 * new_tarval_from_long()
200 tarval *new_tarval_from_double(long double d, ir_mode *mode);
203 * This returns a double with the value represented value, or
204 * gibberish, depending on the size of double and the size of the
206 * This will overflow silently, so use only if you know what
207 * you are doing! (better check with tarval_is_long...)
209 long double get_tarval_double(tarval *tv);
212 * This validates if tarval_to_double() will return a satisfying
213 * result. I.e. if tv is an float_number and between min, max
216 int tarval_is_double(tarval *tv);
219 /** ********** Access routines for tarval fields ********** **/
227 * ir_mode *get_tarval_mode(tarval *tv)
231 * These are access function for tarval struct members. It is encouraged
232 * to use them instead of direct access to the struct fields.
235 * tv - The tarval to access fields of
238 * get_tv_mode: The mode of the tarval
244 /** Returns the mode of the tarval. */
245 ir_mode *get_tarval_mode (tarval *tv);
247 /** Returns the contents of the 'link' field of the tarval */
248 /* void *get_tarval_link (tarval*); */
250 /* Testing properties of the represented values */
253 * Returns 1 if tv is negative
255 * @param a the tarval
257 int tarval_is_negative(tarval *a);
260 * Returns 1 if tv is null
262 * @param a the tarval
264 int tarval_is_null(tarval *a);
267 * Returns 1 if tv is the "one"
269 * @param a the tarval
271 int tarval_is_one(tarval *a);
273 /** The 'bad' tarval. */
274 extern tarval *tarval_bad;
275 /** Returns the 'bad tarval. */
276 tarval *get_tarval_bad(void);
278 /** The 'undefined' tarval. */
279 extern tarval *tarval_undefined;
280 /** Returns the 'undefined' tarval. */
281 tarval *get_tarval_undefined(void);
283 /** The mode_b tarval 'false'. */
284 extern tarval *tarval_b_false;
285 /** Returns the mode_b tarval 'false'. */
286 tarval *get_tarval_b_false(void);
288 /** The mode_b tarval 'true'. */
289 extern tarval *tarval_b_true;
290 /** Returns the mode_b tarval 'true'. */
291 tarval *get_tarval_b_true(void);
293 /** The 'void' pointer tarval. */
294 extern tarval *tarval_P_void;
295 /** Returns the 'void' pointer tarval. */
296 tarval *get_tarval_P_void(void);
298 /* These functions calculate and return a tarval representing the requested
300 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
301 * functions, but these are stored on initialization of the irmode module and
302 * therefore the irmode functions should be prefered to the functions below. */
304 /** Returns the maximum value of a given mode. */
305 tarval *get_tarval_max(ir_mode *mode);
307 /** Returns the minimum value of a given mode. */
308 tarval *get_tarval_min(ir_mode *mode);
310 /** Returns the 0 value (additive neutral) of a given mode. */
311 tarval *get_tarval_null(ir_mode *mode);
313 /** Returns the 1 value (multiplicative neutral) of a given mode. */
314 tarval *get_tarval_one(ir_mode *mode);
316 /** Return quite nan for float_number modes. */
317 tarval *get_tarval_nan(ir_mode *mode);
319 /** Return +inf for float_number modes. */
320 tarval *get_tarval_inf(ir_mode *mode);
322 /* ******************** Arithmethic 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 pnc_number describing the relation
344 * between a and b. This is either Uo, Lt, Eq, Gt, or False if a or b
345 * 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 pnc_number 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 pnc_number 'Eq' is returned, not 'Ge' which
354 * indicates 'greater or equal'
357 * irnode.h for the definition of pnc_numbers
359 pnc_number tarval_cmp(tarval *a, tarval *b);
362 * Converts a tarval to another mode.
364 * Convert tarval 'src' to mode 'mode', this will suceed 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 *m);
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 neccessary 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 /** Absolute value. */
435 tarval *tarval_abs(tarval *a);
438 tarval *tarval_and(tarval *a, tarval *b);
441 tarval *tarval_or(tarval *a, tarval *b);
443 /** Bitwise exclusive or. */
444 tarval *tarval_eor(tarval *a, tarval *b);
447 tarval *tarval_shl(tarval *a, tarval *b);
449 /** Unsigned (logical) right shift. */
450 tarval *tarval_shr(tarval *a, tarval *b);
452 /** Signed (arithmetic) right shift. */
453 tarval *tarval_shrs(tarval *a, tarval *b);
456 tarval *tarval_rot(tarval *a, tarval *b);
458 /** Carry flag of the last operation */
459 int tarval_carry(void);
461 /* *********** Output of tarvals *********** */
464 * The output mode for tarval values.
466 * Some modes allow more that one representation, for instance integers
467 * can be represented hex or decimal. Of course it would be enough to have
468 * one and let every backend convert it into the 'right' one.
469 * However, we can do this in the tarval much simplier...
472 TVO_NATIVE, /**< the default output mode, depends on the mode */
473 TVO_HEX, /**< use hex representation, always possible */
474 TVO_DECIMAL, /**< use decimal representation */
475 TVO_OCTAL, /**< use octal representation */
476 TVO_BINARY, /**< use binary representation */
477 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
478 TVO_HEXFLOAT /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
482 * This structure contains helper information to format the output
483 * of a tarval of a mode.
485 typedef struct tarval_mode_info {
486 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
487 const char *mode_prefix; /**< if set, this prefix will be printed
488 before a value of this mode */
489 const char *mode_suffix; /**< if set, this suffix will be printed
490 after a value of this mode */
494 * Specify the output options of one mode.
496 * This functions stores the modinfo, so DO NOT DESTROY it.
498 * @param mode a ir_mode that should be associated
499 * @param modeinfo the output format info
501 * @return zero on success.
503 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
506 * Returns the output options of one mode.
508 * This functions returns the modinfo of a given mode.
510 * @param mode a ir_mode that should be associated
512 * @return the output option
514 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
517 * Returns Bit representation of a tarval value, as string of '0' and '1'
519 * @param tv The tarval
521 * This function returns a printable bit representation of any value
522 * stored as tarval. This representation is a null terminated C string.
525 * As usual in C a pointer to a char is returned. The length of the
526 * returned string if fixed, just read as many chars as the mode defines
530 * The string is allocated using malloc() and is free()ed on the next call
532 * The string consists of the ascii characters '0' and '1' and is
536 * irmode.h for the definition of the ir_mode struct
537 * the size member of aforementioned struct
539 char *get_tarval_bitpattern(tarval *tv);
542 * Returns the bitpattern of the bytes_ofs byte.
544 * This function succeeds even if the mode of the tarval uses lesser bits
545 * than requested, in that case the bitpattern is filled with zero bits.
547 * To query a 32bit value the following code can be used:
549 * val0 = tarval_sub_bits(tv, 0);
550 * val1 = tarval_sub_bits(tv, 1);
551 * val2 = tarval_sub_bits(tv, 2);
552 * val3 = tarval_sub_bits(tv, 3);
554 * Because this is the bit representation of the target machine, only the following
555 * operations are legal on the result:
557 * - concatenation (endian dependance MUST be handled by the CALLER)
558 * - bitwise logical operations to select/mask bits
560 * @param tv the tarval
561 * @param byte_ofs the byte offset
564 * The result of this funcion is undefined if the mode is neither integer nor float.
566 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs);
569 * Return values of tarval classify
571 typedef enum _tarval_classification_t {
572 TV_CLASSIFY_NULL = 0, /**< the tarval represents the additive neutral element */
573 TV_CLASSIFY_ONE = +1, /**< the tarval represents the multiplicative neutral element */
574 TV_CLASSIFY_ALL_ONE = -1, /**< the tarval represents the bitwise-and neutral element */
575 TV_CLASSIFY_OTHER = 2 /**< all other tarvals */
576 } tarval_classification_t;
579 * Identifying tarvals values for algebraic simplifications.
582 * - TV_CLASSIFY_NULL for additive neutral,
583 * - TV_CLASSIFY_ONE for multiplicative neutral,
584 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
585 * - TV_CLASSIFY_OTHER else
587 tarval_classification_t classify_tarval(tarval *tv);
592 * Output of tarvals to a buffer.
594 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
597 * Output of tarvals to stdio.
599 int tarval_printf(tarval *tv);