4 * Declarations for Target Values.
10 Discussion of new interface, proposals by Prof. Waite:
12 > 1. You say that you plan to replace the tv module. That replacement is
13 > absolutely essential for an ANSI C translator: Section 6.1.3.2 of the
14 > standard says that the representation of an integer_constant depends
15 > upon its value as well as any suffixes that are attached to it. The
16 > possible Firm modes for such a constant are i, I, l, and L. The
17 > current tv module provides only one integer conversion routine, and
18 > that requires conversion by the client. Since the type of the value
19 > argument is long, this may preclude the representation of an unsigned
22 > There is a similar problem with floating constants. Floating
23 > constants can be suffixed in C, and the mode depends upon the suffix.
24 > It can indicate that the constant is of type long double, which your
25 > current tv module is incapable of representing.
27 > Your tv module interface accepts two kinds of information: modes and
28 > values. Values obtained from the program text might be uninterpreted
29 > strings, strings interpreted as integers, and strings interpreted as
30 > reals. Values provided by the compiler are usually integers. Modes are
31 > always Firm modes. It seems to me that the tv module should provide
32 > tarval* constructors for three of the four kinds of values. Each of these
33 > constructors should have an ir_mode parameter and one or more parameters
34 > appropriate for the kind of value. As is currently the case, one
35 > constructor should be provided for both compiler-generated integers and
36 > source strings interpreted as integers. (This avoids problems of
37 > different conversion radices -- the client does the conversion.) For
38 > symmetry, the constructor for source strings interpreted as reals should
39 > accept a long double parameter and require the client to do the
49 # include "irnode.h" /* for pnc_number enum */
54 * tv -- TargetValue, short tarval.
55 * Internal representation for machine values.
58 * Christian von Roques
62 * Tarvals represent target machine values. They are typed by modes.
63 * Tarvals only represent values of mode_sort:
70 * In case of references the module accepts an entity to represent the
72 * Furthermore, computations and conversions of these values can
76 * This module is closely related to the irmode module, as the modes
77 * defined there are thoroughly used throughout the whole module.
78 * Also, the comparison functions rely on the definition of comparison
79 * values in the irnode module.
82 * The original tv module originated in the fiasco compiler written ...
83 * This is the new version, described in the tech report 1999-14 by ...
87 * irmode.h for the modes definitions
88 * irnode.h for the pnc_numbers table
90 * tarval_init1 and tarval_init2 for initialization of the
95 #ifndef _TARVAL_TYPEDEF_
96 #define _TARVAL_TYPEDEF_
97 typedef struct tarval tarval;
100 /* ************************ Constructors for tarvals ************************ */
103 * Constructor function for new tarvals.
105 * @param str The string representing the target value
106 * @param len The length of the string
107 * @param mode The mode requested for the result tarval
109 * This function creates a new tarval representing the value represented
110 * by a CString, aka char array. If a tarval representing this value already
111 * exists, this tarval is returned instead of a new one. So tarvals are
112 * directly comparable since their representation is unique.
114 * This function accepts the following strings:
116 * if mode is int_number:
117 * - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
118 * - 0[0-7]* (octal representation)
119 * - (+|-)?[1-9][0-9]* (decimal representation)
121 * if mode if float_number:
122 * - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
124 * if mode is boolean: true, True, TRUE ... False... 0, 1,
126 * if mode is reference: hexadecimal of decimal number as int
128 * if mode is character: hex or dec
130 * Leading and/or trailing spaces are ignored
133 * A tarval of proper type representing the requested value is returned.
134 * Tarvals are unique, so for any value/mode pair at most one tarval will
135 * exist, which will be returned upon further requests with an identical
139 * If the string is not representable in the given mode an assertion is
140 * thrown in assert build.
143 * irmode.h for predefined modes
144 * new_tarval_from_long()
145 * new_tarval_from_double()
147 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
150 * Constructor function for new tarvals
152 * @param l The long representing the value
153 * @param mode The mode requested for the result tarval
155 * This function creates a new tarval representing the value represented
156 * by a long integer. If a tarval representing this value already exists,
157 * this tarval is returned instead of a new one. So tarvals are directly
158 * comparable since their representation is unique.
161 * A tarval of proper type representing the requested value is returned.
162 * Tarvals are unique, so for any value/mode pair at most one tarval will
163 * exist, which will be returned upon further requests with an identical
167 * If the long is not representable in the given mode an assertion is
168 * thrown in assert build.
171 * irmode.h for predefined modes
172 * new_tarval_from_str()
173 * new_tarval_from_double()
176 tarval *new_tarval_from_long(long l, ir_mode *mode);
179 * This returns a long int with the value represented value, or
180 * gibberish, depending on the size of long int and the size of the
181 * stored value. It works for e.g. 1 as mode_Ls, but might not work for
182 * get_mode_max(mode_Ls).
183 * This will overflow silently, so use only if you know what
184 * you are doing! (better check with tarval_is_long()...)
186 long tarval_to_long(tarval *tv);
189 * This validates if tarval_to_long() will return a satisfying
190 * result. I.e. if tv is an int_number and between min, max
191 * of long int (signed!)
193 int tarval_is_long(tarval *tv);
196 * Constructor function for new tarvals.
198 * @param d The long double representing the value
199 * @param mode The mode requested for the result tarval
201 * This function creates a new tarval representing the value represented
202 * by a long double. If a tarval representing this value already exists,
203 * this tarval is returned instead of a new one. So tarvals are directly
204 * comparable since their representation is unique.
205 * Only modes of sort float_number can be constructed this way.
208 * A tarval of proper type representing the requested value is returned.
209 * Tarvals are unique, so for any value/mode pair at most one tarval will
210 * exist, which will be returned upon further requests with an identical
214 * If the long double is not representable in the given mode an assertion
215 * is thrown. This will happen for any mode not of sort float_number.
218 * irmode.h for predefined values
219 * new_tarval_from_str()
220 * new_tarval_from_long()
222 tarval *new_tarval_from_double(long double d, ir_mode *mode);
225 * This returns a double with the value represented value, or
226 * gibberish, depending on the size of double and the size of the
228 * This will overflow silently, so use only if you know what
229 * you are doing! (better check with tarval_is_long...)
231 long double tarval_to_double(tarval *tv);
234 * This validates if tarval_to_double() will return a satisfying
235 * result. I.e. if tv is an float_number and between min, max
238 int tarval_is_double(tarval *tv);
241 * Construct a tarval that represents the address of the entity.
243 * The address must be constant, the entity must have as owner the global type.
245 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode);
248 * Returns the associated entity of a tarval.
250 entity *tarval_to_entity(tarval *tv);
253 * Returns non-zero if a the given tarval represents an entity.
255 int tarval_is_entity(tarval *tv);
257 /** ********** Access routines for tarval fields ********** **/
265 * ir_mode *get_tarval_mode(tarval *tv)
269 * These are access function for tarval struct members. It is encouraged
270 * to use them instead of direct access to the struct fields.
273 * tv - The tarval to access fields of
276 * get_tv_mode: The mode of the tarval
282 /** Returns the mode of the tarval. */
283 ir_mode *get_tarval_mode (tarval *tv);
285 /* Testing properties of the represented values */
288 * Returns 1 if tv is negative
290 * @param a the tarval
292 int tarval_is_negative(tarval *a);
295 * Returns 1 if tv is null
297 * @param a the tarval
299 int tarval_is_null(tarval *a);
301 /** The 'bad' tarval. */
302 extern tarval *tarval_bad;
303 /** Returns the 'bad tarval. */
304 tarval *get_tarval_bad(void);
306 /** The 'undefined' tarval. */
307 extern tarval *tarval_undefined;
308 /** Returns the 'undefined' tarval. */
309 tarval *get_tarval_undefined(void);
311 /** The mode_b tarval 'false'. */
312 extern tarval *tarval_b_false;
313 /** Returns the mode_b tarval 'false'. */
314 tarval *get_tarval_b_false(void);
316 /** The mode_b tarval 'true'. */
317 extern tarval *tarval_b_true;
318 /** Returns the mode_b tarval 'true'. */
319 tarval *get_tarval_b_true(void);
321 /** The 'void' pointer tarval. */
322 extern tarval *tarval_P_void;
323 /** Returns the 'void' pointer tarval. */
324 tarval *get_tarval_P_void(void);
326 /* These functions calculate and return a tarval representing the requested
328 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
329 * functions, but these are stored on initialization of the irmode module and
330 * therefore the irmode functions should be prefered to the functions below. */
332 /** Returns the maximum value of a given mode. */
333 tarval *get_tarval_max(ir_mode *mode);
335 /** Returns the minimum value of a given mode. */
336 tarval *get_tarval_min(ir_mode *mode);
338 /** Returns the 0 value (additive neutral) of a given mode. */
339 tarval *get_tarval_null(ir_mode *mode);
341 /** Returns the 1 value (multiplicative neutral) of a given mode. */
342 tarval *get_tarval_one(ir_mode *mode);
344 /** Return quite nan for float_number modes. */
345 tarval *get_tarval_nan(ir_mode *mode);
347 /** Return +inf for float_number modes. */
348 tarval *get_tarval_inf(ir_mode *mode);
350 /* ******************** Arithmethic operations on tarvals ******************** */
353 * Compares two tarvals
355 * Compare a with b and return a pnc_number describing the relation
356 * between a and b. This is either Uo, Lt, Eq, Gt, or False if a or b
357 * are symbolic pointers which can not be compared at all.
359 * @param a A tarval to be compared
360 * @param b A tarval to be compared
363 * The pnc_number best describing the relation between a and b is returned.
364 * This means the mode with the least bits set is returned, e.g. if the
365 * tarvals are equal the pnc_number 'Eq' is returned, not 'Ge' which
366 * indicates 'greater or equal'
369 * irnode.h for the definition of pnc_numbers
371 pnc_number tarval_cmp(tarval *a, tarval *b);
374 * Converts a tarval to another mode.
376 * Convert tarval 'src' to mode 'mode', this will suceed if and only if mode
377 * 'mode' is wider than the mode of src, as defined in the firm documentation
378 * and as returned by the function mode_is_smaller defined in irmode.h.
380 * @param src The tarval to convert
381 * @param mode Tho mode to convert to
384 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
385 * tarvals value already exists, it will be returned, else a new tarval is
386 * constructed and returned
389 * Illegal conversations will trigger an assertion
392 * FIRM documentation for conversion rules
393 * mode_is_smaller defined in irmode.h
395 tarval *tarval_convert_to(tarval *src, ir_mode *m);
398 * These function implement basic computations representable as opcodes
404 * a - the tarval to operate on
407 * a - the first operand tarval
408 * b - the second operand tarval
411 * If neccessary a new tarval is constructed for the resulting value,
412 * or the one already carrying the computation result is retrieved and
413 * returned as result.
416 * The order the arguments are given in is important, imagine postfix
418 * Illegal operations will trigger an assertion.
419 * The sort member of the struct mode defines which operations are valid
422 /** Negation of a tarval. */
423 tarval *tarval_neg(tarval *a);
425 /** Addition of two tarvals. */
426 tarval *tarval_add(tarval *a, tarval *b);
428 /** Subtraction from a tarval. */
429 tarval *tarval_sub(tarval *a, tarval *b);
431 /** Multiplication of tarvals. */
432 tarval *tarval_mul(tarval *a, tarval *b);
434 /** 'Exact' division. */
435 tarval *tarval_quo(tarval *a, tarval *b);
437 /** Integer division. */
438 tarval *tarval_div(tarval *a, tarval *b);
440 /** Remainder of integer division. */
441 tarval *tarval_mod(tarval *a, tarval *b);
443 /** Absolute value. */
444 tarval *tarval_abs(tarval *a);
447 tarval *tarval_and(tarval *a, tarval *b);
450 tarval *tarval_or(tarval *a, tarval *b);
452 /** Bitwise exclusive or. */
453 tarval *tarval_eor(tarval *a, tarval *b);
456 tarval *tarval_shl(tarval *a, tarval *b);
458 /** Unsigned (logical) right shift. */
459 tarval *tarval_shr(tarval *a, tarval *b);
461 /** Signed (arithmetic) right shift. */
462 tarval *tarval_shrs(tarval *a, tarval *b);
465 tarval *tarval_rot(tarval *a, tarval *b);
467 /* *********** Output of tarvals *********** */
470 * The output mode for tarval values.
472 * Some modes allow more that one representation, for instance integers
473 * can be represented hex or decimal. Of course it would be enough to have
474 * one and let every backend convert it into the 'right' one.
475 * However, we can do this in the tarval much simplier...
478 TVO_NATIVE, /**< the default output mode, depends on the mode */
479 TVO_HEX, /**< use hex representation, always possible */
480 TVO_DECIMAL, /**< use decimal representation */
481 TVO_OCTAL, /**< use octal representation */
482 TVO_BINARY, /**< use binary representation */
483 TVO_FLOAT /**< use floating point representation */
487 * This structure contains helper information to format the output
488 * of a tarval of an mode.
490 typedef struct tarval_mode_info {
491 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
492 const char *mode_prefix; /**< if set, this prefix will be printed
493 before a value of this mode */
494 const char *mode_suffix; /**< if set, this suffix will be printed
495 after a value of this mode */
499 * Specify the output options of one mode.
501 * This functions stores the modinfo, so DO NOT DESTROY it.
503 * @param mode a ir_mode that should be associated
504 * @param modeinfo the output format info
506 * @return zero on success.
508 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
511 * Returns the output options of one mode.
513 * This functions returns the modinfo of a given mode.
515 * @param mode a ir_mode that should be associated
517 * @return the output option
519 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode);
522 * Returns Bit representation of a tarval value, as string of '0' and '1'
524 * @param tv The tarval
526 * This function returns a printable bit representation of any value
527 * stored as tarval. This representation is a null terminated C string.
530 * As usual in C a pointer to a char is returned. The length of the
531 * returned string if fixed, just read as many chars as the mode defines
535 * The string is allocated using malloc() and is free()ed on the next call
537 * The string consists of the ascii characters '0' and '1' and is
541 * irmode.h for the definition of the ir_mode struct
542 * the size member of aforementioned struct
544 char *tarval_bitpattern(tarval *tv);
547 * Returns the bitpattern of the bytes_ofs byte.
549 * This function succeeds even if the mode of the tarval uses lesser bits
550 * than requested, in that case the bitpattern is filled with zero bits.
552 * To query a 32bit value the following code can be used:
554 * val0 = tarval_sub_bits(tv, 0);
555 * val1 = tarval_sub_bits(tv, 1);
556 * val2 = tarval_sub_bits(tv, 2);
557 * val3 = tarval_sub_bits(tv, 3);
559 * Because this is the bit representation of the target machine, only the following
560 * operations are legal on the result:
562 * - concatenation (endian dependance MUST be handled by the CALLER)
563 * - bitwise logical operations to select/mask bits
565 * @param tv the tarval
566 * @param byte_ofs the byte offset
569 * The result of this funcion is undefined if the mode is neither integer nor float.
571 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs);
574 * Identifying some tarvals ???
577 * - 0 for additive neutral,
578 * - +1 for multiplicative neutral,
579 * - -1 for bitwise-and neutral
583 * This function is deprecated and its use strongly discouraged.
584 * Implemented for completeness.
586 long tarval_classify(tarval *tv);
589 * Initialization of the tarval module.
591 * Call before init_mode().
593 void init_tarval_1(void);
596 * Initialization of the tarval module.
598 * Call after init_mode().
600 void init_tarval_2(void);
603 * Output of tarvals to a buffer.
605 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
608 * Output of tarvals to stdio.
610 int tarval_printf(tarval *tv);