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
50 # include "irnode.h" /* for pnc_number enum */
55 * tv -- TargetValue, short tarval.
56 * Internal representation for machine values.
59 * Christian von Roques
63 * Tarvals represent target machine values. They are typed by modes.
64 * Tarvals only represent values of mode_sort:
71 * In case of references the module accepts an entity to represent the
73 * Furthermore, computations and conversions of these values can
77 * This module is closely related to the irmode module, as the modes
78 * defined there are thoroughly used throughout the whole module.
79 * Also, the comparison functions rely on the definition of comparison
80 * values in the irnode module.
83 * The original tv module originated in the fiasco compiler written ...
84 * This is the new version, described in the tech report 1999-14 by ...
88 * irmode.h for the modes definitions
89 * irnode.h for the pnc_numbers table
91 * tarval_init1 and tarval_init2 for initialization of the
96 #ifndef _TARVAL_TYPEDEF_
97 #define _TARVAL_TYPEDEF_
98 typedef struct tarval tarval;
101 /* ************************ Constructors for tarvals ************************ */
102 /****f* tv/new_tarval_from_str
105 * new_tarval_from_str
106 * Constructor function for new tarvals.
109 * tarval *new_tarval_from_str(const char *s, size_t len, ir_mode *mode)
112 * This function creates a new tarval representing the value represented
113 * by a CString, aka char array. If a tarval representing this value already
114 * exists, this tarval is returned instead of a new one. So tarvals are
115 * directly comparable since their representation is unique.
118 * str - The String representing the target value
119 * len - The length of the string
120 * mode - The mode requested for the result tarval
122 * This function accepts the following strings:
123 * if mode is int_number:
124 * 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
125 * 0[0-7]* (octal representation)
126 * (+|-)?[1-9][0-9]* (decimal representation)
127 * if mode if float_number:
128 * (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
129 * if mode is boolean: true, True, TRUE ... False... 0, 1,
130 * if mode is reference: hexadecimal of decimal number as int
131 * if mode is character: hex or dec
132 * Leading and/or trailing spaces are ignored
135 * A tarval of proper type representing the requested value is returned.
136 * Tarvals are unique, so for any value/mode pair at most one tarval will
137 * exist, which will be returned upon further requests with an identical
141 * If the string is not representable in the given mode an assertion is
145 * irmode.h for predefined modes
146 * new_tarval_from_long
147 * new_tarval_from_double
150 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
152 /****f* tv/new_tarval_from_long
155 * new_tarval_from_long
156 * Constructor function for new tarvals
159 * tarval *new_tarval_from_long(const long l. ir_mode *mode)
162 * This function creates a new tarval representing the value represented
163 * by a long integer. If a tarval representing this value already exists,
164 * this tarval is returned instead of a new one. So tarvals are directly
165 * comparable since their representation is unique.
168 * l - The long representing the value
169 * mode - The mode requested for the result tarval
172 * A tarval of proper type representing the requested value is returned.
173 * Tarvals are unique, so for any value/mode pair at most one tarval will
174 * exist, which will be returned upon further requests with an identical
178 * If the long is not representable in the given mode an assertion is
182 * irmode.h for predefined modes
183 * new_tarval_from_str
184 * new_tarval_from_double
188 tarval *new_tarval_from_long(long l, ir_mode *mode);
190 * This returns a long int with the value represented value, or
191 * gibberish, depending on the size of long int and the size of the
192 * stored value. It works for e.g. 1 as mode_Ls, but not for
193 * get_mode_max(mode_Ls).
194 * This will overflow silently, so use only if you know what
195 * you are doing! (better check with tarval_is_long...)
197 long tarval_to_long(tarval *tv);
199 * This validates if tarval_to_long will return a satisfying
200 * result. I.e. if tv is an int_number and between min, max
201 * of long int (signed!)
203 int tarval_is_long(tarval *tv);
205 /****f* tv/new_tarval_from_double
208 * new_tarval_from_double
209 * Constructor function for new tarvals
212 * tarval *new_tarval_from_double(const long double d, ir_mode *mode)
215 * This function creates a new tarval representing the value represented
216 * by a long double. If a tarval representing this value already exists,
217 * this tarval is returned instead of a new one. So tarvals are directly
218 * comparable since their representation is unique.
221 * d - The long double representing the value
222 * mode - The mode requested for the result tarval
224 * Only modes of sort float_number can be constructed this way.
227 * A tarval of proper type representing the requested value is returned.
228 * Tarvals are unique, so for any value/mode pair at most one tarval will
229 * exist, which will be returned upon further requests with an identical
233 * If the long double is not representable in the given mode an assertion
234 * is thrown. This will happen for any mode not of sort float_number
237 * irmode.h for predefined values
238 * new_tarval_from_str
239 * new_tarval_from_long
242 tarval *new_tarval_from_double(long double d, ir_mode *mode);
243 long double tarval_to_double(tarval *tv);
244 int tarval_is_double(tarval *tv);
245 /* The tarval represents the address of the entity. As the address must
246 be constant the entity must have as owner the global type. */
247 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode);
248 entity *tarval_to_entity(tarval *tv);
249 int tarval_is_entity(tarval *tv);
251 /** ********** Access routines for tarval fields ********** **/
253 /****f* tv/get_tarval_*
260 * ir_mode *get_tarval_mode(tarval *tv)
264 * These are access function for tarval struct members. It is encouraged
265 * to use them instead of direct access to the struct fields.
268 * tv - The tarval to access fields of
271 * get_tv_mode: The mode of the tarval
277 /* get the mode of the tarval */
278 #ifdef TARVAL_ACCESS_DEFINES
280 # define get_tarval_mode(tv) (tv)->mode
282 ir_mode *get_tarval_mode (tarval *tv);
284 /** Testing properties of the represented values **/
285 /* Returns 0 if tv is positive, else > 0. @@@ not tested! */
286 int tarval_is_negative(tarval *a);
288 /** Some special values **/
289 extern tarval *tarval_bad; tarval *get_tarval_bad(void);
290 extern tarval *tarval_undefined; tarval *get_tarval_undefined(void);
291 /* These two are the only valid mode_b tarvals! */
292 extern tarval *tarval_b_false; tarval *get_tarval_b_false(void);
293 extern tarval *tarval_b_true; tarval *get_tarval_b_true(void);
295 extern tarval *tarval_P_void; tarval *get_tarval_P_void(void);
297 /* These functions calculate and return a tarval representing the requested
299 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
300 * functions, but these are stored on initialization of the irmode module and
301 * therefore the irmode functions should be prefered to the functions below. */
302 tarval *get_tarval_max(ir_mode *mode);
303 tarval *get_tarval_min(ir_mode *mode);
304 tarval *get_tarval_null(ir_mode *mode);
305 tarval *get_tarval_one(ir_mode *mode);
306 tarval *get_tarval_nan(ir_mode *mode);
307 tarval *get_tarval_inf(ir_mode *mode);
309 /* ******************** Arithmethic operations on tarvals ******************** */
311 /****f* tv/tarval_cmp
315 * Compares two tarvals
318 * pnc_number tarval_comp(tarval *a, tarval *b)
321 * Compare a with b and return a pnc_number describing the relation
322 * between a and b. This is either Uo, Lt, Eq, Gt, or False if a or b
323 * are symbolic pointers which can not be compared at all.
328 * a and b are tarvals to be compared
331 * The pnc_number best describing the relation between a and b is returned.
332 * This means the mode with the least bits set is returned, e.g. if the
333 * tarvals are equal the pnc_number 'Eq' is returned, not 'Ge' which
334 * indicates 'greater or equal'
337 * irnode.h for the definition of pnc_numbers
340 pnc_number tarval_cmp(tarval *a, tarval *b);
342 /****f* tv/tarval_convert_to
346 * Converts a tarval to another mode
349 * tarval *tarval_convert_to(tarval *src, ir_mode *mode)
352 * Convert tarval 'src' to mode 'mode', this will suceed if and only if mode
353 * 'mode' is wider than the mode of src, as defined in the firm documentation
354 * and as returned by the function mode_is_smaller defined in irmode.h.
357 * src - The tarval to convert
358 * 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
373 tarval *tarval_convert_to(tarval *src, ir_mode *m);
375 /****f* tv/tarval_calculations
378 * tarval_neg - Negation of a tarval
379 * tarval_add - Addition of two tarvals
380 * tarval_sub - Subtraction from a tarval
381 * tarval_mul - Multiplication of tarvals
382 * tarval_quo - 'Exact' division
383 * tarval_div - Integer division
384 * tarval_mod - Remainder of integer division
385 * tarval_abs - Absolute value
386 * tarval_and - Bitwise and
387 * tarval_or - Bitwise or
388 * tarval_eor - Bitwise exclusive or
389 * tarval_shl - Left shift
390 * tarval_shr - Unsigned right shift
391 * tarval_shrs - Signed right shift
392 * tarval_rot - Rotation
395 * tarval *tarval_neg (tarval *a)
396 * tarval *tarval_add (tarval *a, tarval *b)
397 * tarval *tarval_sub (tarval *a, tarval *b)
398 * tarval *tarval_mul (tarval *a, tarval *b)
399 * tarval *tarval_quo (tarval *a, tarval *b)
400 * tarval *tarval_div (tarval *a, tarval *b)
401 * tarval *tarval_mod (tarval *a, tarval *b)
402 * tarval *tarval_abs (tarval *a)
403 * tarval *tarval_and (tarval *a, tarval *b)
404 * tarval *tarval_or (tarval *a, tarval *b)
405 * tarval *tarval_eor (tarval *a, tarval *b)
406 * tarval *tarval_shl (tarval *a, tarval *b)
407 * tarval *tarval_shr (tarval *a, tarval *b)
408 * tarval *tarval_shrs(tarval *a, tarval *b)
409 * tarval *tarval_rot (tarval *a, tarval *b)
412 * These function implement basic computations representable as opcodes
418 * a - the tarval to operate on
421 * a - the first operand tarval
422 * b - the second operand tarval
425 * If neccessary a new tarval is constructed for the resulting value,
426 * or the one already carrying the computation result is retrieved and
427 * returned as result.
430 * The order the arguments are given in is important, imagine postfix
432 * Illegal operations will trigger an assertion.
433 * The sort member of the struct mode defines which operations are valid
436 tarval *tarval_neg(tarval *a); /* negation */
437 tarval *tarval_add(tarval *a, tarval *b); /* addition */
438 tarval *tarval_sub(tarval *a, tarval *b); /* subtraction */
439 tarval *tarval_mul(tarval *a, tarval *b); /* multiplication */
440 tarval *tarval_quo(tarval *a, tarval *b); /* floating point division */
441 tarval *tarval_div(tarval *a, tarval *b); /* integer division */
442 tarval *tarval_mod(tarval *a, tarval *b); /* remainder */
443 tarval *tarval_abs(tarval *a); /* absolute value */
444 tarval *tarval_and(tarval *a, tarval *b); /* bitwise and */
445 tarval *tarval_or (tarval *a, tarval *b); /* bitwise or */
446 tarval *tarval_eor(tarval *a, tarval *b); /* bitwise exclusive or (xor) */
447 tarval *tarval_shl(tarval *a, tarval *b); /* bitwise left shift */
448 tarval *tarval_shr(tarval *a, tarval *b); /* bitwise unsigned right shift */
449 tarval *tarval_shrs(tarval *a, tarval *b); /* bitwise signed right shift */
450 tarval *tarval_rot(tarval *a, tarval *b); /* bitwise rotation */
452 /** *********** Output of tarvals *********** **/
453 /****f* tv/tarval_bitpattern
457 * Bit representation of a tarval value, as string of '0' and '1'
460 * char *tarval_bitpattern(tarval *tv)
463 * This function returns a printable bit representation of any value
464 * stored as tarval. This representation is a null terminated C string.
470 * As usual in C a pointer to a char is returned. The length of the
471 * returned string if fixed, just read as many chars as the mode defines
475 * The string is allocated using malloc() and is free()ed on the next call
477 * The string consists of the ascii characters '0' and '1' and is
481 * irmode.h for the definition of the ir_mode struct
482 * the size member of aforementioned struct
485 char *tarval_bitpattern(tarval *tv);
487 /* Identifying some tarvals ??? */
488 /* This function is deprecated and its use strongly discouraged */
489 long tarval_classify(tarval *tv);
491 /** Initialization of the tarval module **/
492 void init_tarval_1(void); /* call before init_mode */
493 void init_tarval_2(void); /* call after init_mode */