4 * Declarations for Target Values.
14 # include "irnode.h" /* for pnc_number enum */
20 * tv -- TargetValue, short tarval.
21 * Internal representation for machine values.
27 * Tarvals represent target machine values. They are typed by modes.
28 * Tarvals only represent values of mode_sort:
35 * In case of references the module accepts an entity to represent the
37 * Furthermore, computations and conversions of these values can
41 * This module is closely related to the irmode module, as the modes
42 * defined there are thoroughly used throughout the whole module.
43 * Also, the comparison functions rely on the definition of comparison
44 * values in the irnode module.
47 * The original tv module originated in the fiasco compiler written ...
48 * This is the new version, described in the tech report 1999-14 by ...
52 * irmode.h for the modes definitions
53 * irnode.h for the pnc_numbers table
55 * tarval_init1 and tarval_init2 for initialization of the
60 #ifndef _TARVAL_TYPEDEF_
61 #define _TARVAL_TYPEDEF_
62 typedef struct tarval tarval;
65 /* ************************ Constructors for tarvals ************************ */
68 * Constructor function for new tarvals.
70 * @param str The string representing the target value
71 * @param len The length of the string
72 * @param mode The mode requested for the result tarval
74 * This function creates a new tarval representing the value represented
75 * by a CString, aka char array. If a tarval representing this value already
76 * exists, this tarval is returned instead of a new one. So tarvals are
77 * directly comparable since their representation is unique.
79 * This function accepts the following strings:
81 * if mode is int_number:
82 * - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
83 * - 0[0-7]* (octal representation)
84 * - (+|-)?[1-9][0-9]* (decimal representation)
86 * if mode if float_number:
87 * - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
89 * if mode is boolean: true, True, TRUE ... False... 0, 1,
91 * if mode is reference: hexadecimal of decimal number as int
93 * if mode is character: hex or dec
95 * Leading and/or trailing spaces are ignored
98 * A tarval of proper type representing the requested value is returned.
99 * Tarvals are unique, so for any value/mode pair at most one tarval will
100 * exist, which will be returned upon further requests with an identical
104 * If the string is not representable in the given mode an assertion is
105 * thrown in assert build.
108 * irmode.h for predefined modes
109 * new_tarval_from_long()
110 * new_tarval_from_double()
112 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
115 * Constructor function for new tarvals
117 * @param l The long representing the value
118 * @param mode The mode requested for the result tarval
120 * This function creates a new tarval representing the value represented
121 * by a long integer. If a tarval representing this value already exists,
122 * this tarval is returned instead of a new one. So tarvals are directly
123 * comparable since their representation is unique.
126 * A tarval of proper type representing the requested value is returned.
127 * Tarvals are unique, so for any value/mode pair at most one tarval will
128 * exist, which will be returned upon further requests with an identical
132 * If the long is not representable in the given mode an assertion is
133 * thrown in assert build.
136 * irmode.h for predefined modes
137 * new_tarval_from_str()
138 * new_tarval_from_double()
141 tarval *new_tarval_from_long(long l, ir_mode *mode);
144 * This returns a long int with the value represented value, or
145 * gibberish, depending on the size of long int and the size of the
146 * stored value. It works for e.g. 1 as mode_Ls, but might not work for
147 * get_mode_max(mode_Ls).
148 * This will overflow silently, so use only if you know what
149 * you are doing! (better check with tarval_is_long()...)
151 long tarval_to_long(tarval *tv);
154 * This validates if tarval_to_long() will return a satisfying
155 * result. I.e. if tv is an int_number and between min, max
156 * of long int (signed!)
158 int tarval_is_long(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 tarval *new_tarval_from_double(long double d, ir_mode *mode);
190 * This returns a double with the value represented value, or
191 * gibberish, depending on the size of double and the size of the
193 * This will overflow silently, so use only if you know what
194 * you are doing! (better check with tarval_is_long...)
196 long double tarval_to_double(tarval *tv);
199 * This validates if tarval_to_double() will return a satisfying
200 * result. I.e. if tv is an float_number and between min, max
203 int tarval_is_double(tarval *tv);
206 * Construct a tarval that represents the address of the entity.
208 * The address must be constant, the entity must have as owner the global type.
210 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode);
213 * Returns the associated entity of a tarval.
215 entity *tarval_to_entity(tarval *tv);
218 * Returns non-zero if a the given tarval represents an entity.
220 int tarval_is_entity(tarval *tv);
222 /** ********** Access routines for tarval fields ********** **/
230 * ir_mode *get_tarval_mode(tarval *tv)
234 * These are access function for tarval struct members. It is encouraged
235 * to use them instead of direct access to the struct fields.
238 * tv - The tarval to access fields of
241 * get_tv_mode: The mode of the tarval
247 /** Returns the mode of the tarval. */
248 ir_mode *get_tarval_mode (tarval *tv);
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);
266 /** The 'bad' tarval. */
267 extern tarval *tarval_bad;
268 /** Returns the 'bad tarval. */
269 tarval *get_tarval_bad(void);
271 /** The 'undefined' tarval. */
272 extern tarval *tarval_undefined;
273 /** Returns the 'undefined' tarval. */
274 tarval *get_tarval_undefined(void);
276 /** The mode_b tarval 'false'. */
277 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 /** The 'void' pointer tarval. */
287 extern tarval *tarval_P_void;
288 /** Returns the 'void' pointer tarval. */
289 tarval *get_tarval_P_void(void);
291 /* These functions calculate and return a tarval representing the requested
293 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
294 * functions, but these are stored on initialization of the irmode module and
295 * therefore the irmode functions should be prefered to the functions below. */
297 /** Returns the maximum value of a given mode. */
298 tarval *get_tarval_max(ir_mode *mode);
300 /** Returns the minimum value of a given mode. */
301 tarval *get_tarval_min(ir_mode *mode);
303 /** Returns the 0 value (additive neutral) of a given mode. */
304 tarval *get_tarval_null(ir_mode *mode);
306 /** Returns the 1 value (multiplicative neutral) of a given mode. */
307 tarval *get_tarval_one(ir_mode *mode);
309 /** Return quite nan for float_number modes. */
310 tarval *get_tarval_nan(ir_mode *mode);
312 /** Return +inf for float_number modes. */
313 tarval *get_tarval_inf(ir_mode *mode);
315 /* ******************** Arithmethic operations on tarvals ******************** */
318 * Compares two tarvals
320 * Compare a with b and return a pnc_number describing the relation
321 * between a and b. This is either Uo, Lt, Eq, Gt, or False if a or b
322 * are symbolic pointers which can not be compared at all.
324 * @param a A tarval to be compared
325 * @param b A tarval to be compared
328 * The pnc_number best describing the relation between a and b is returned.
329 * This means the mode with the least bits set is returned, e.g. if the
330 * tarvals are equal the pnc_number 'Eq' is returned, not 'Ge' which
331 * indicates 'greater or equal'
334 * irnode.h for the definition of pnc_numbers
336 pnc_number tarval_cmp(tarval *a, tarval *b);
339 * Converts a tarval to another mode.
341 * Convert tarval 'src' to mode 'mode', this will suceed if and only if mode
342 * 'mode' is wider than the mode of src, as defined in the firm documentation
343 * and as returned by the function mode_is_smaller defined in irmode.h.
345 * @param src The tarval to convert
346 * @param mode Tho mode to convert to
349 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
350 * tarvals value already exists, it will be returned, else a new tarval is
351 * constructed and returned
354 * Illegal conversations will trigger an assertion
357 * FIRM documentation for conversion rules
358 * mode_is_smaller defined in irmode.h
360 tarval *tarval_convert_to(tarval *src, ir_mode *m);
363 * These function implement basic computations representable as opcodes
369 * a - the tarval to operate on
372 * a - the first operand tarval
373 * b - the second operand tarval
376 * If neccessary a new tarval is constructed for the resulting value,
377 * or the one already carrying the computation result is retrieved and
378 * returned as result.
381 * The order the arguments are given in is important, imagine postfix
383 * Illegal operations will trigger an assertion.
384 * The sort member of the struct mode defines which operations are valid
387 /** Negation of a tarval. */
388 tarval *tarval_neg(tarval *a);
390 /** Addition of two tarvals. */
391 tarval *tarval_add(tarval *a, tarval *b);
393 /** Subtraction from a tarval. */
394 tarval *tarval_sub(tarval *a, tarval *b);
396 /** Multiplication of tarvals. */
397 tarval *tarval_mul(tarval *a, tarval *b);
399 /** 'Exact' division. */
400 tarval *tarval_quo(tarval *a, tarval *b);
402 /** Integer division. */
403 tarval *tarval_div(tarval *a, tarval *b);
405 /** Remainder of integer division. */
406 tarval *tarval_mod(tarval *a, tarval *b);
408 /** Absolute value. */
409 tarval *tarval_abs(tarval *a);
412 tarval *tarval_and(tarval *a, tarval *b);
415 tarval *tarval_or(tarval *a, tarval *b);
417 /** Bitwise exclusive or. */
418 tarval *tarval_eor(tarval *a, tarval *b);
421 tarval *tarval_shl(tarval *a, tarval *b);
423 /** Unsigned (logical) right shift. */
424 tarval *tarval_shr(tarval *a, tarval *b);
426 /** Signed (arithmetic) right shift. */
427 tarval *tarval_shrs(tarval *a, tarval *b);
430 tarval *tarval_rot(tarval *a, tarval *b);
432 /* *********** Output of tarvals *********** */
435 * The output mode for tarval values.
437 * Some modes allow more that one representation, for instance integers
438 * can be represented hex or decimal. Of course it would be enough to have
439 * one and let every backend convert it into the 'right' one.
440 * However, we can do this in the tarval much simplier...
443 TVO_NATIVE, /**< the default output mode, depends on the mode */
444 TVO_HEX, /**< use hex representation, always possible */
445 TVO_DECIMAL, /**< use decimal representation */
446 TVO_OCTAL, /**< use octal representation */
447 TVO_BINARY, /**< use binary representation */
448 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
449 TVO_HEXFLOAT /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
453 * This structure contains helper information to format the output
454 * of a tarval of an mode.
456 typedef struct tarval_mode_info {
457 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
458 const char *mode_prefix; /**< if set, this prefix will be printed
459 before a value of this mode */
460 const char *mode_suffix; /**< if set, this suffix will be printed
461 after a value of this mode */
465 * Specify the output options of one mode.
467 * This functions stores the modinfo, so DO NOT DESTROY it.
469 * @param mode a ir_mode that should be associated
470 * @param modeinfo the output format info
472 * @return zero on success.
474 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
477 * Returns the output options of one mode.
479 * This functions returns the modinfo of a given mode.
481 * @param mode a ir_mode that should be associated
483 * @return the output option
485 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode);
488 * Returns Bit representation of a tarval value, as string of '0' and '1'
490 * @param tv The tarval
492 * This function returns a printable bit representation of any value
493 * stored as tarval. This representation is a null terminated C string.
496 * As usual in C a pointer to a char is returned. The length of the
497 * returned string if fixed, just read as many chars as the mode defines
501 * The string is allocated using malloc() and is free()ed on the next call
503 * The string consists of the ascii characters '0' and '1' and is
507 * irmode.h for the definition of the ir_mode struct
508 * the size member of aforementioned struct
510 char *tarval_bitpattern(tarval *tv);
513 * Returns the bitpattern of the bytes_ofs byte.
515 * This function succeeds even if the mode of the tarval uses lesser bits
516 * than requested, in that case the bitpattern is filled with zero bits.
518 * To query a 32bit value the following code can be used:
520 * val0 = tarval_sub_bits(tv, 0);
521 * val1 = tarval_sub_bits(tv, 1);
522 * val2 = tarval_sub_bits(tv, 2);
523 * val3 = tarval_sub_bits(tv, 3);
525 * Because this is the bit representation of the target machine, only the following
526 * operations are legal on the result:
528 * - concatenation (endian dependance MUST be handled by the CALLER)
529 * - bitwise logical operations to select/mask bits
531 * @param tv the tarval
532 * @param byte_ofs the byte offset
535 * The result of this funcion is undefined if the mode is neither integer nor float.
537 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs);
540 * Identifying some tarvals ???
543 * - 0 for additive neutral,
544 * - +1 for multiplicative neutral,
545 * - -1 for bitwise-and neutral
549 * This function is deprecated and its use strongly discouraged.
550 * Implemented for completeness.
552 long tarval_classify(tarval *tv);
555 * Initialization of the tarval module.
557 * Call before init_mode().
559 void init_tarval_1(void);
562 * Initialization of the tarval module.
564 * Call after init_mode().
566 void init_tarval_2(void);
569 * Output of tarvals to a buffer.
571 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
574 * Output of tarvals to stdio.
576 int tarval_printf(tarval *tv);