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.
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);
155 * This returns a long int with the value represented value, or
156 * gibberish, depending on the size of long int and the size of the
157 * stored value. It works for e.g. 1 as mode_Ls, but might not work for
158 * get_mode_max(mode_Ls).
159 * This will overflow silently, so use only if you know what
160 * you are doing! (better check with tarval_is_long()...)
162 long tarval_to_long(tarval *tv);
165 * This validates if tarval_to_long() will return a satisfying
166 * result. I.e. if tv is an int_number and between min, max
167 * of long int (signed!)
169 int tarval_is_long(tarval *tv);
172 * Constructor function for new tarvals.
174 * @param d The (long) double representing the value
175 * @param mode The mode requested for the result tarval
177 * This function creates a new tarval representing the value represented
178 * by a (long) double. If a tarval representing this value already exists,
179 * this tarval is returned instead of a new one. So tarvals are directly
180 * comparable since their representation is unique.
181 * Only modes of sort float_number can be constructed this way.
184 * A tarval of proper type representing the requested value is returned.
185 * Tarvals are unique, so for any value/mode pair at most one tarval will
186 * exist, which will be returned upon further requests with an identical
190 * If the (long) double is not representable in the given mode an assertion
191 * is thrown. This will happen for any mode not of sort float_number.
194 * irmode.h for predefined values
195 * new_tarval_from_str()
196 * new_tarval_from_long()
198 tarval *new_tarval_from_double(long double d, ir_mode *mode);
201 * This returns a double with the value represented value, or
202 * gibberish, depending on the size of double and the size of the
204 * This will overflow silently, so use only if you know what
205 * you are doing! (better check with tarval_is_long...)
207 long double tarval_to_double(tarval *tv);
210 * This validates if tarval_to_double() will return a satisfying
211 * result. I.e. if tv is an float_number and between min, max
214 int tarval_is_double(tarval *tv);
217 * Construct a tarval that represents the address of the entity.
219 * The address must be constant, the entity must have as owner the global type.
221 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode);
224 * Returns the associated entity of a tarval.
226 #define get_tarval_entity tarval_to_entity
227 entity *tarval_to_entity(tarval *tv);
230 * Returns non-zero if a the given tarval represents an entity.
232 int tarval_is_entity(tarval *tv);
234 /** ********** Access routines for tarval fields ********** **/
242 * ir_mode *get_tarval_mode(tarval *tv)
246 * These are access function for tarval struct members. It is encouraged
247 * to use them instead of direct access to the struct fields.
250 * tv - The tarval to access fields of
253 * get_tv_mode: The mode of the tarval
259 /** Returns the mode of the tarval. */
260 ir_mode *get_tarval_mode (tarval *tv);
262 /* Testing properties of the represented values */
265 * Returns 1 if tv is negative
267 * @param a the tarval
269 int tarval_is_negative(tarval *a);
272 * Returns 1 if tv is null
274 * @param a the tarval
276 int tarval_is_null(tarval *a);
278 /** The 'bad' tarval. */
279 extern tarval *tarval_bad;
280 /** Returns the 'bad tarval. */
281 tarval *get_tarval_bad(void);
283 /** The 'undefined' tarval. */
284 extern tarval *tarval_undefined;
285 /** Returns the 'undefined' tarval. */
286 tarval *get_tarval_undefined(void);
288 /** The mode_b tarval 'false'. */
289 extern tarval *tarval_b_false;
290 /** Returns the mode_b tarval 'false'. */
291 tarval *get_tarval_b_false(void);
293 /** The mode_b tarval 'true'. */
294 extern tarval *tarval_b_true;
295 /** Returns the mode_b tarval 'true'. */
296 tarval *get_tarval_b_true(void);
298 /** The 'void' pointer tarval. */
299 extern tarval *tarval_P_void;
300 /** Returns the 'void' pointer tarval. */
301 tarval *get_tarval_P_void(void);
303 /* These functions calculate and return a tarval representing the requested
305 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
306 * functions, but these are stored on initialization of the irmode module and
307 * therefore the irmode functions should be prefered to the functions below. */
309 /** Returns the maximum value of a given mode. */
310 tarval *get_tarval_max(ir_mode *mode);
312 /** Returns the minimum value of a given mode. */
313 tarval *get_tarval_min(ir_mode *mode);
315 /** Returns the 0 value (additive neutral) of a given mode. */
316 tarval *get_tarval_null(ir_mode *mode);
318 /** Returns the 1 value (multiplicative neutral) of a given mode. */
319 tarval *get_tarval_one(ir_mode *mode);
321 /** Return quite nan for float_number modes. */
322 tarval *get_tarval_nan(ir_mode *mode);
324 /** Return +inf for float_number modes. */
325 tarval *get_tarval_inf(ir_mode *mode);
327 /* ******************** Arithmethic operations on tarvals ******************** */
330 * Compares two tarvals
332 * Compare a with b and return a pnc_number describing the relation
333 * between a and b. This is either Uo, Lt, Eq, Gt, or False if a or b
334 * are symbolic pointers which can not be compared at all.
336 * @param a A tarval to be compared
337 * @param b A tarval to be compared
340 * The pnc_number best describing the relation between a and b is returned.
341 * This means the mode with the least bits set is returned, e.g. if the
342 * tarvals are equal the pnc_number 'Eq' is returned, not 'Ge' which
343 * indicates 'greater or equal'
346 * irnode.h for the definition of pnc_numbers
348 pnc_number tarval_cmp(tarval *a, tarval *b);
351 * Converts a tarval to another mode.
353 * Convert tarval 'src' to mode 'mode', this will suceed if and only if mode
354 * 'mode' is wider than the mode of src, as defined in the firm documentation
355 * and as returned by the function mode_is_smaller defined in irmode.h.
357 * @param src The tarval to convert
358 * @param mode Tho mode to convert to
361 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
362 * tarvals value already exists, it will be returned, else a new tarval is
363 * constructed and returned
366 * Illegal conversations will trigger an assertion
369 * FIRM documentation for conversion rules
370 * mode_is_smaller defined in irmode.h
372 tarval *tarval_convert_to(tarval *src, ir_mode *m);
375 * These function implement basic computations representable as opcodes
381 * a - the tarval to operate on
384 * a - the first operand tarval
385 * b - the second operand tarval
388 * If neccessary a new tarval is constructed for the resulting value,
389 * or the one already carrying the computation result is retrieved and
390 * returned as result.
393 * The order the arguments are given in is important, imagine postfix
395 * Illegal operations will trigger an assertion.
396 * The sort member of the struct mode defines which operations are valid
399 /** bitwise Negation of a tarval. */
400 tarval *tarval_not(tarval *a);
402 /** arithmetic Negation of a tarval. */
403 tarval *tarval_neg(tarval *a);
405 /** Addition of two tarvals. */
406 tarval *tarval_add(tarval *a, tarval *b);
408 /** Subtraction from a tarval. */
409 tarval *tarval_sub(tarval *a, tarval *b);
411 /** Multiplication of tarvals. */
412 tarval *tarval_mul(tarval *a, tarval *b);
414 /** 'Exact' division. */
415 tarval *tarval_quo(tarval *a, tarval *b);
417 /** Integer division. */
418 tarval *tarval_div(tarval *a, tarval *b);
420 /** Remainder of integer division. */
421 tarval *tarval_mod(tarval *a, tarval *b);
423 /** Absolute value. */
424 tarval *tarval_abs(tarval *a);
427 tarval *tarval_and(tarval *a, tarval *b);
430 tarval *tarval_or(tarval *a, tarval *b);
432 /** Bitwise exclusive or. */
433 tarval *tarval_eor(tarval *a, tarval *b);
436 tarval *tarval_shl(tarval *a, tarval *b);
438 /** Unsigned (logical) right shift. */
439 tarval *tarval_shr(tarval *a, tarval *b);
441 /** Signed (arithmetic) right shift. */
442 tarval *tarval_shrs(tarval *a, tarval *b);
445 tarval *tarval_rot(tarval *a, tarval *b);
447 /* *********** Output of tarvals *********** */
450 * The output mode for tarval values.
452 * Some modes allow more that one representation, for instance integers
453 * can be represented hex or decimal. Of course it would be enough to have
454 * one and let every backend convert it into the 'right' one.
455 * However, we can do this in the tarval much simplier...
458 TVO_NATIVE, /**< the default output mode, depends on the mode */
459 TVO_HEX, /**< use hex representation, always possible */
460 TVO_DECIMAL, /**< use decimal representation */
461 TVO_OCTAL, /**< use octal representation */
462 TVO_BINARY, /**< use binary representation */
463 TVO_FLOAT, /**< use floating point representation (i.e 1.342e-2)*/
464 TVO_HEXFLOAT /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
468 * This structure contains helper information to format the output
469 * of a tarval of a mode.
471 typedef struct tarval_mode_info {
472 tv_output_mode mode_output; /**< if != TVO_NATIVE select a special mode */
473 const char *mode_prefix; /**< if set, this prefix will be printed
474 before a value of this mode */
475 const char *mode_suffix; /**< if set, this suffix will be printed
476 after a value of this mode */
480 * Specify the output options of one mode.
482 * This functions stores the modinfo, so DO NOT DESTROY it.
484 * @param mode a ir_mode that should be associated
485 * @param modeinfo the output format info
487 * @return zero on success.
489 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
492 * Returns the output options of one mode.
494 * This functions returns the modinfo of a given mode.
496 * @param mode a ir_mode that should be associated
498 * @return the output option
500 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode);
503 * Returns Bit representation of a tarval value, as string of '0' and '1'
505 * @param tv The tarval
507 * This function returns a printable bit representation of any value
508 * stored as tarval. This representation is a null terminated C string.
511 * As usual in C a pointer to a char is returned. The length of the
512 * returned string if fixed, just read as many chars as the mode defines
516 * The string is allocated using malloc() and is free()ed on the next call
518 * The string consists of the ascii characters '0' and '1' and is
522 * irmode.h for the definition of the ir_mode struct
523 * the size member of aforementioned struct
525 char *tarval_bitpattern(tarval *tv);
528 * Returns the bitpattern of the bytes_ofs byte.
530 * This function succeeds even if the mode of the tarval uses lesser bits
531 * than requested, in that case the bitpattern is filled with zero bits.
533 * To query a 32bit value the following code can be used:
535 * val0 = tarval_sub_bits(tv, 0);
536 * val1 = tarval_sub_bits(tv, 1);
537 * val2 = tarval_sub_bits(tv, 2);
538 * val3 = tarval_sub_bits(tv, 3);
540 * Because this is the bit representation of the target machine, only the following
541 * operations are legal on the result:
543 * - concatenation (endian dependance MUST be handled by the CALLER)
544 * - bitwise logical operations to select/mask bits
546 * @param tv the tarval
547 * @param byte_ofs the byte offset
550 * The result of this funcion is undefined if the mode is neither integer nor float.
552 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs);
555 * Identifying some tarvals ???
558 * - 0 for additive neutral,
559 * - +1 for multiplicative neutral,
560 * - -1 for bitwise-and neutral
564 * This function is deprecated and its use strongly discouraged.
565 * Implemented for completeness.
567 long tarval_classify(tarval *tv);
570 * Initialization of the tarval module.
572 * Call before init_mode().
574 void init_tarval_1(void);
577 * Initialization of the tarval module.
579 * Call after init_mode().
581 void init_tarval_2(void);
584 * Output of tarvals to a buffer.
586 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
589 * Output of tarvals to stdio.
591 int tarval_printf(tarval *tv);