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 ************************ */
101 /****f* tv/new_tarval_from_str
104 * new_tarval_from_str
105 * Constructor function for new tarvals.
108 * tarval *new_tarval_from_str(const char *s, size_t len, ir_mode *mode)
111 * This function creates a new tarval representing the value represented
112 * by a CString, aka char array. If a tarval representing this value already
113 * exists, this tarval is returned instead of a new one. So tarvals are
114 * directly comparable since their representation is unique.
117 * str - The String representing the target value
118 * len - The length of the string
119 * mode - The mode requested for the result tarval
121 * This function accepts the following strings:
122 * if mode is int_number:
123 * 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
124 * 0[0-7]* (octal representation)
125 * (+|-)?[1-9][0-9]* (decimal representation)
126 * if mode if float_number:
127 * (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
128 * if mode is boolean: true, True, TRUE ... False... 0, 1,
129 * if mode is reference: hexadecimal of decimal number as int
130 * if mode is character: hex or dec
131 * Leading and/or trailing spaces are ignored
134 * A tarval of proper type representing the requested value is returned.
135 * Tarvals are unique, so for any value/mode pair at most one tarval will
136 * exist, which will be returned upon further requests with an identical
140 * If the string is not representable in the given mode an assertion is
144 * irmode.h for predefined modes
145 * new_tarval_from_long
146 * new_tarval_from_double
149 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
151 /****f* tv/new_tarval_from_long
154 * new_tarval_from_long
155 * Constructor function for new tarvals
158 * tarval *new_tarval_from_long(const long l. ir_mode *mode)
161 * This function creates a new tarval representing the value represented
162 * by a long integer. If a tarval representing this value already exists,
163 * this tarval is returned instead of a new one. So tarvals are directly
164 * comparable since their representation is unique.
167 * l - The long representing the value
168 * mode - The mode requested for the result tarval
171 * A tarval of proper type representing the requested value is returned.
172 * Tarvals are unique, so for any value/mode pair at most one tarval will
173 * exist, which will be returned upon further requests with an identical
177 * If the long is not representable in the given mode an assertion is
181 * irmode.h for predefined modes
182 * new_tarval_from_str
183 * new_tarval_from_double
187 tarval *new_tarval_from_long(long l, ir_mode *mode);
189 * This returns a long int with the value represented value, or
190 * gibberish, depending on the size of long int and the size of the
191 * stored value. It works for e.g. 1 as mode_Ls, but not for
192 * get_mode_max(mode_Ls).
193 * This will overflow silently, so use only if you know what
194 * you are doing! (better check with tarval_is_long...)
196 long tarval_to_long(tarval *tv);
198 * This validates if tarval_to_long will return a satisfying
199 * result. I.e. if tv is an int_number and between min, max
200 * of long int (signed!)
202 int tarval_is_long(tarval *tv);
204 /****f* tv/new_tarval_from_double
207 * new_tarval_from_double
208 * Constructor function for new tarvals
211 * tarval *new_tarval_from_double(const long double d, ir_mode *mode)
214 * This function creates a new tarval representing the value represented
215 * by a long double. If a tarval representing this value already exists,
216 * this tarval is returned instead of a new one. So tarvals are directly
217 * comparable since their representation is unique.
220 * d - The long double representing the value
221 * mode - The mode requested for the result tarval
223 * Only modes of sort float_number can be constructed this way.
226 * A tarval of proper type representing the requested value is returned.
227 * Tarvals are unique, so for any value/mode pair at most one tarval will
228 * exist, which will be returned upon further requests with an identical
232 * If the long double is not representable in the given mode an assertion
233 * is thrown. This will happen for any mode not of sort float_number
236 * irmode.h for predefined values
237 * new_tarval_from_str
238 * new_tarval_from_long
241 tarval *new_tarval_from_double(long double d, ir_mode *mode);
242 long double tarval_to_double(tarval *tv);
243 int tarval_is_double(tarval *tv);
244 /* The tarval represents the address of the entity. As the address must
245 be constant the entity must have as owner the global type. */
246 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode);
247 entity *tarval_to_entity(tarval *tv);
248 int tarval_is_entity(tarval *tv);
250 /** ********** Access routines for tarval fields ********** **/
252 /****f* tv/get_tarval_*
259 * ir_mode *get_tarval_mode(tarval *tv)
263 * These are access function for tarval struct members. It is encouraged
264 * to use them instead of direct access to the struct fields.
267 * tv - The tarval to access fields of
270 * get_tv_mode: The mode of the tarval
276 /* get the mode of the tarval */
277 #ifdef TARVAL_ACCESS_DEFINES
279 # define get_tarval_mode(tv) (tv)->mode
281 ir_mode *get_tarval_mode (tarval *tv);
283 /** Testing properties of the represented values **/
284 /* Returns 0 if tv is positive, else > 0. @@@ not tested! */
285 int tarval_is_negative(tarval *a);
287 /** Some special values **/
288 extern tarval *tarval_bad; tarval *get_tarval_bad(void);
289 extern tarval *tarval_undefined; tarval *get_tarval_undefined(void);
290 /* These two are the only valid mode_b tarvals! */
291 extern tarval *tarval_b_false; tarval *get_tarval_b_false(void);
292 extern tarval *tarval_b_true; tarval *get_tarval_b_true(void);
294 extern tarval *tarval_P_void; tarval *get_tarval_P_void(void);
296 /* These functions calculate and return a tarval representing the requested
298 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
299 * functions, but these are stored on initialization of the irmode module and
300 * therefore the irmode functions should be prefered to the functions below. */
301 tarval *get_tarval_max(ir_mode *mode);
302 tarval *get_tarval_min(ir_mode *mode);
303 tarval *get_tarval_null(ir_mode *mode);
304 tarval *get_tarval_one(ir_mode *mode);
305 tarval *get_tarval_nan(ir_mode *mode);
306 tarval *get_tarval_inf(ir_mode *mode);
308 /* ******************** Arithmethic operations on tarvals ******************** */
310 /****f* tv/tarval_cmp
314 * Compares two tarvals
317 * pnc_number tarval_comp(tarval *a, tarval *b)
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.
327 * a and b are tarvals to be compared
330 * The pnc_number best describing the relation between a and b is returned.
331 * This means the mode with the least bits set is returned, e.g. if the
332 * tarvals are equal the pnc_number 'Eq' is returned, not 'Ge' which
333 * indicates 'greater or equal'
336 * irnode.h for the definition of pnc_numbers
339 pnc_number tarval_cmp(tarval *a, tarval *b);
341 /****f* tv/tarval_convert_to
345 * Converts a tarval to another mode
348 * tarval *tarval_convert_to(tarval *src, ir_mode *mode)
351 * Convert tarval 'src' to mode 'mode', this will suceed if and only if mode
352 * 'mode' is wider than the mode of src, as defined in the firm documentation
353 * and as returned by the function mode_is_smaller defined in irmode.h.
356 * src - The tarval to convert
357 * mode - Tho mode to convert to
360 * If a tarval of mode 'mode' with the result of the conversion of the 'src'
361 * tarvals value already exists, it will be returned, else a new tarval is
362 * constructed and returned
365 * Illegal conversations will trigger an assertion
368 * FIRM documentation for conversion rules
369 * mode_is_smaller defined in irmode.h
372 tarval *tarval_convert_to(tarval *src, ir_mode *m);
374 /****f* tv/tarval_calculations
377 * tarval_neg - Negation of a tarval
378 * tarval_add - Addition of two tarvals
379 * tarval_sub - Subtraction from a tarval
380 * tarval_mul - Multiplication of tarvals
381 * tarval_quo - 'Exact' division
382 * tarval_div - Integer division
383 * tarval_mod - Remainder of integer division
384 * tarval_abs - Absolute value
385 * tarval_and - Bitwise and
386 * tarval_or - Bitwise or
387 * tarval_eor - Bitwise exclusive or
388 * tarval_shl - Left shift
389 * tarval_shr - Unsigned right shift
390 * tarval_shrs - Signed right shift
391 * tarval_rot - Rotation
394 * tarval *tarval_neg (tarval *a)
395 * tarval *tarval_add (tarval *a, tarval *b)
396 * tarval *tarval_sub (tarval *a, tarval *b)
397 * tarval *tarval_mul (tarval *a, tarval *b)
398 * tarval *tarval_quo (tarval *a, tarval *b)
399 * tarval *tarval_div (tarval *a, tarval *b)
400 * tarval *tarval_mod (tarval *a, tarval *b)
401 * tarval *tarval_abs (tarval *a)
402 * tarval *tarval_and (tarval *a, tarval *b)
403 * tarval *tarval_or (tarval *a, tarval *b)
404 * tarval *tarval_eor (tarval *a, tarval *b)
405 * tarval *tarval_shl (tarval *a, tarval *b)
406 * tarval *tarval_shr (tarval *a, tarval *b)
407 * tarval *tarval_shrs(tarval *a, tarval *b)
408 * tarval *tarval_rot (tarval *a, tarval *b)
411 * These function implement basic computations representable as opcodes
417 * a - the tarval to operate on
420 * a - the first operand tarval
421 * b - the second operand tarval
424 * If neccessary a new tarval is constructed for the resulting value,
425 * or the one already carrying the computation result is retrieved and
426 * returned as result.
429 * The order the arguments are given in is important, imagine postfix
431 * Illegal operations will trigger an assertion.
432 * The sort member of the struct mode defines which operations are valid
435 tarval *tarval_neg(tarval *a); /* negation */
436 tarval *tarval_add(tarval *a, tarval *b); /* addition */
437 tarval *tarval_sub(tarval *a, tarval *b); /* subtraction */
438 tarval *tarval_mul(tarval *a, tarval *b); /* multiplication */
439 tarval *tarval_quo(tarval *a, tarval *b); /* floating point division */
440 tarval *tarval_div(tarval *a, tarval *b); /* integer division */
441 tarval *tarval_mod(tarval *a, tarval *b); /* remainder */
442 tarval *tarval_abs(tarval *a); /* absolute value */
443 tarval *tarval_and(tarval *a, tarval *b); /* bitwise and */
444 tarval *tarval_or (tarval *a, tarval *b); /* bitwise or */
445 tarval *tarval_eor(tarval *a, tarval *b); /* bitwise exclusive or (xor) */
446 tarval *tarval_shl(tarval *a, tarval *b); /* bitwise left shift */
447 tarval *tarval_shr(tarval *a, tarval *b); /* bitwise unsigned right shift */
448 tarval *tarval_shrs(tarval *a, tarval *b); /* bitwise signed right shift */
449 tarval *tarval_rot(tarval *a, tarval *b); /* bitwise rotation */
451 /** *********** Output of tarvals *********** **/
452 /****f* tv/tarval_bitpattern
456 * Bit representation of a tarval value, as string of '0' and '1'
459 * char *tarval_bitpattern(tarval *tv)
462 * This function returns a printable bit representation of any value
463 * stored as tarval. This representation is a null terminated C string.
469 * As usual in C a pointer to a char is returned. The length of the
470 * returned string if fixed, just read as many chars as the mode defines
474 * The string is allocated using malloc() and is free()ed on the next call
476 * The string consists of the ascii characters '0' and '1' and is
480 * irmode.h for the definition of the ir_mode struct
481 * the size member of aforementioned struct
484 char *tarval_bitpattern(tarval *tv);
486 /* Identifying some tarvals ??? */
487 /* This function is deprecated and its use strongly discouraged */
488 long tarval_classify(tarval *tv);
490 /** Initialization of the tarval module **/
491 void init_tarval_1(void); /* call before init_mode */
492 void init_tarval_2(void); /* call after init_mode */