tarval_ieee754_set_immediate_precision(), tarval_enable_fp_ops() added
[libfirm] / include / libfirm / tv.h
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Representation of and static computations on target machine
23  *           values.
24  * @date     2003
25  * @author   Mathias Heil
26  * @version  $Id$
27  * @summary
28  *   Tarvals represent target machine values.  They are typed by modes.
29  *   Tarvals only represent values of mode_sort:
30  *    - int_number,
31  *    - float_number,
32  *    - boolean,
33  *    - reference,
34  *    - character
35  *
36  *   In case of references the module accepts an entity to represent the
37  *   value.
38  *   Furthermore, computations and conversions of these values can
39  *   be performed.
40  *
41  * HISTORY
42  *    The original tv module originated in the fiasco compiler written ...
43  *    This is the new version, described in the tech report 1999-14 by ...
44  *
45  * @sa
46  *    Techreport 1999-14
47  *    irmode.h for the modes definitions
48  *    irnode.h for the pn_Cmp table
49  */
50 #ifndef FIRM_TV_TV_H
51 #define FIRM_TV_TV_H
52
53 #include "firm_types.h"
54 #include "irnode.h"
55
56 /* ************************ Constructors for tarvals ************************ */
57
58 /**
59  * Constructor function for new tarvals.
60  *
61  * @param str   The string representing the target value
62  * @param len   The length of the string
63  * @param mode  The mode requested for the result tarval
64  *
65  * This function creates a new tarval representing the value represented
66  * by a CString, aka char array. If a tarval representing this value already
67  * exists, this tarval is returned instead of a new one. So tarvals are
68  * directly comparable since their representation is unique.
69  *
70  * This function accepts the following strings:
71  *
72  * if mode is int_number:
73  *  - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
74  *  - 0[0-7]*            (octal representation)
75  *  - (+|-)?[1-9][0-9]*  (decimal representation)
76  *
77  * if mode if float_number:
78  *  - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
79  *
80  * if mode is boolean: true, True, TRUE ... False... 0, 1,
81  *
82  * if mode is reference: hexadecimal of decimal number as int
83  *
84  * if mode is character: hex or dec
85  *
86  * Leading and/or trailing spaces are ignored
87  *
88  * @return
89  *   A tarval of proper type representing the requested value is returned.
90  *   Tarvals are unique, so for any value/mode pair at most one tarval will
91  *   exist, which will be returned upon further requests with an identical
92  *   value/mode pair.
93  *
94  * @note
95  *   If the string is not representable in the given mode an assertion is
96  *   thrown in assert build.
97  *
98  * @sa
99  *   irmode.h for predefined modes
100  *   new_tarval_from_long()
101  *   new_tarval_from_double()
102  */
103 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
104
105 /**
106  * Constructor function for new tarvals
107  *
108  * @param l     The long representing the value
109  * @param mode  The mode requested for the result tarval
110  *
111  * This function creates a new tarval representing the value represented
112  * by a long integer. If a tarval representing this value already exists,
113  * this tarval is returned instead of a new one. So tarvals are directly
114  * comparable since their representation is unique.
115  *
116  * @return
117  *   A tarval of proper type representing the requested value is returned.
118  *   Tarvals are unique, so for any value/mode pair at most one tarval will
119  *   exist, which will be returned upon further requests with an identical
120  *   value/mode pair.
121  *
122  * @note
123  *   If the long is not representable in the given mode an assertion is
124  *   thrown in assert build.
125  *
126  * @sa
127  *   irmode.h for predefined modes
128  *   new_tarval_from_str()
129  *   new_tarval_from_double()
130  *
131  */
132 tarval *new_tarval_from_long(long l, ir_mode *mode);
133
134 /** Return value as long if possible.
135  *
136  * This returns a long int with the value represented value, or
137  * gibberish, depending on the size of long int and the size of the
138  * stored value. It works for e.g. 1 as mode_Ls, but might not work for
139  * get_mode_max(mode_Ls).
140  * This will overflow silently, so use only if you know what
141  * you are doing! (better check with tarval_is_long()...)
142  * Works only for int modes, even not for character modes!
143  */
144 long get_tarval_long(tarval *tv);
145
146 /**
147  * This validates if get_tarval_long() will return a satisfying
148  * result. I.e. if tv is an int_number and between min, max
149  * of long int (signed!)
150  */
151 int tarval_is_long(tarval *tv);
152
153 /**
154  * Constructor function for new tarvals.
155  *
156  * @param d     The (long) double representing the value
157  * @param mode  The mode requested for the result tarval
158  *
159  * This function creates a new tarval representing the value represented
160  * by a (long) double. If a tarval representing this value already exists,
161  * this tarval is returned instead of a new one. So tarvals are directly
162  * comparable since their representation is unique.
163  * Only modes of sort float_number can be constructed this way.
164  *
165  * @return
166  *   A tarval of proper type representing the requested value is returned.
167  *   Tarvals are unique, so for any value/mode pair at most one tarval will
168  *   exist, which will be returned upon further requests with an identical
169  *   value/mode pair.
170  *
171  * @note
172  *   If the (long) double is not representable in the given mode an assertion
173  *   is thrown. This will happen for any mode not of sort float_number.
174  *
175  * @sa
176  *   irmode.h for predefined values
177  *   new_tarval_from_str()
178  *   new_tarval_from_long()
179  */
180 tarval *new_tarval_from_double(long double d, ir_mode *mode);
181
182 /**
183  * This returns a double with the value represented value, or
184  * gibberish, depending on the size of double and the size of the
185  * stored value.
186  * This will overflow silently, so use only if you know what
187  * you are doing! (better check with tarval_is_long...)
188  */
189 long double get_tarval_double(tarval *tv);
190
191 /**
192  * This validates if tarval_to_double() will return a satisfying
193  * result. I.e. if tv is an float_number and between min, max
194  * of double
195  */
196 int tarval_is_double(tarval *tv);
197
198
199 /** ********** Access routines for tarval fields ********** **/
200
201 /*
202  * NAME
203  *   get_tarval_mode
204  *   get_tarval_ ...
205  *
206  * SYNOPSIS
207  *   ir_mode *get_tarval_mode(tarval *tv)
208  *   ...
209  *
210  * DESCRIPTION
211  *    These are access function for tarval struct members. It is encouraged
212  *   to use them instead of direct access to the struct fields.
213  *
214  * PARAMETERS
215  *   tv - The tarval to access fields of
216  *
217  * RESULT
218  *   get_tv_mode: The mode of the tarval
219  *
220  * SEE ALSO
221  *   the struct tarval
222  */
223
224 /** Returns the mode of the tarval. */
225 ir_mode *get_tarval_mode (const tarval *tv);
226
227 /** Returns the contents of the 'link' field of the tarval */
228 /* void *get_tarval_link (tarval*); */
229
230 /* Testing properties of the represented values */
231
232 /**
233  * Returns 1 if tv is negative
234  *
235  * @param a the tarval
236  */
237 int tarval_is_negative(tarval *a);
238
239 /**
240  * Returns 1 if tv is null
241  *
242  * @param a the tarval
243  */
244 int tarval_is_null(tarval *a);
245
246 /**
247  * Returns 1 if tv is the "one"
248  *
249  * @param a the tarval
250  */
251 int tarval_is_one(tarval *a);
252
253 /** The 'bad' tarval. */
254 extern tarval *tarval_bad;
255 /** Returns the 'bad tarval. */
256 tarval *get_tarval_bad(void);
257
258 /** The 'undefined' tarval. */
259 extern tarval *tarval_undefined;
260 /** Returns the 'undefined' tarval. */
261 tarval *get_tarval_undefined(void);
262
263 /** The mode_b tarval 'false'. */
264 extern tarval *tarval_b_false;
265
266 /** Returns the mode_b tarval 'false'. */
267 tarval *get_tarval_b_false(void);
268
269 /** The mode_b tarval 'true'. */
270 extern tarval *tarval_b_true;
271 /** Returns the mode_b tarval 'true'. */
272 tarval *get_tarval_b_true(void);
273
274 /* These functions calculate and return a tarval representing the requested
275  * value.
276  * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
277  * functions, but these are stored on initialization of the irmode module and
278  * therefore the irmode functions should be preferred to the functions below. */
279
280 /** Returns the maximum value of a given mode. */
281 tarval *get_tarval_max(ir_mode *mode);
282
283 /** Returns the minimum value of a given mode. */
284 tarval *get_tarval_min(ir_mode *mode);
285
286 /** Returns the 0 value (additive neutral) of a given mode.
287     For reference modes, the NULL value is returned (old tarval_P_void) */
288 tarval *get_tarval_null(ir_mode *mode);
289
290 /** Returns the 1 value (multiplicative neutral) of a given mode. */
291 tarval *get_tarval_one(ir_mode *mode);
292
293 /** Returns the -1 value (multiplicative neutral) of a given mode.
294  *  Returns tarval bad for unsigned modes */
295 tarval *get_tarval_minus_one(ir_mode *mode);
296
297 /** Return quite nan for float_number modes. */
298 tarval *get_tarval_nan(ir_mode *mode);
299
300 /** Return +inf for float_number modes. */
301 tarval *get_tarval_plus_inf(ir_mode *mode);
302
303 /** Return -inf for float_number modes. */
304 tarval *get_tarval_minus_inf(ir_mode *mode);
305
306 /* ******************** Arithmetic operations on tarvals ******************** */
307
308 typedef enum _tarval_int_overflow_mode_t {
309   TV_OVERFLOW_BAD,      /**< tarval module will return tarval_bad if a overflow occurs */
310   TV_OVERFLOW_WRAP,     /**< tarval module will overflow will be ignored, wrap around occurs */
311   TV_OVERFLOW_SATURATE  /**< tarval module will saturate the overflow */
312 } tarval_int_overflow_mode_t;
313
314 /**
315  * Sets the overflow mode for integer operations.
316  */
317 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode);
318
319 /**
320  * Get the overflow mode for integer operations.
321  */
322 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void);
323
324 /**
325  * Compares two tarvals
326  *
327  * Compare a with b and return a pn_Cmp describing the relation
328  * between a and b.  This is either pn_Cmp_Uo, pn_Cmp_Lt, pn_Cmp_Eq, pn_Cmp_Gt,
329  * or pn_Cmp_False if a or b are symbolic pointers which can not be compared at all.
330  *
331  * @param a   A tarval to be compared
332  * @param b   A tarval to be compared
333  *
334  * @return
335  *   The pn_Cmp best describing the relation between a and b is returned.
336  *   This means the mode with the least bits set is returned, e.g. if the
337  *   tarvals are equal the pn_Cmp 'pn_Cmp_Eq' is returned, not 'pn_Cmp_Ge' which
338  *   indicates 'greater or equal'
339  *
340  * @sa
341  *    irnode.h for the definition of pn_Cmp
342  */
343 pn_Cmp tarval_cmp(tarval *a, tarval *b);
344
345 /**
346  * Converts a tarval to another mode.
347  *
348  * Convert tarval 'src' to mode 'mode', this will succeed if and only if mode
349  * 'mode' is wider than the mode of src, as defined in the firm documentation
350  * and as returned by the function mode_is_smaller defined in irmode.h.
351  *
352  * @param src    The tarval to convert
353  * @param mode   Tho mode to convert to
354  *
355  * @return
356  *   If a tarval of mode 'mode' with the result of the conversion of the 'src'
357  *   tarvals value already exists, it will be returned, else a new tarval is
358  *   constructed and returned
359  *
360  * @note
361  *    Illegal conversations will trigger an assertion
362  *
363  * @sa
364  *    FIRM documentation for conversion rules
365  *    mode_is_smaller defined in irmode.h
366  */
367 tarval *tarval_convert_to(tarval *src, ir_mode *mode);
368
369 /*
370  * These function implement basic computations representable as opcodes
371  * in FIRM nodes.
372  *
373  * PARAMETERS
374  *    tarval_neg:
375  *    traval_abs:
376  *      a - the tarval to operate on
377  *
378  *    all others:
379  *      a - the first operand tarval
380  *      b - the second operand tarval
381  *
382  * RESULT
383  *    If necessary a new tarval is constructed for the resulting value,
384  *   or the one already carrying the computation result is retrieved and
385  *   returned as result.
386  *
387  * NOTES
388  *   The order the arguments are given in is important, imagine postfix
389  *   notation.
390  *   Illegal operations will trigger an assertion.
391  *   The sort member of the struct mode defines which operations are valid
392  */
393
394 /** bitwise Negation of a tarval. */
395 tarval *tarval_not(tarval *a);
396
397 /** arithmetic Negation of a tarval. */
398 tarval *tarval_neg(tarval *a);
399
400 /** Addition of two tarvals. */
401 tarval *tarval_add(tarval *a, tarval *b);
402
403 /** Subtraction from a tarval. */
404 tarval *tarval_sub(tarval *a, tarval *b);
405
406 /** Multiplication of tarvals. */
407 tarval *tarval_mul(tarval *a, tarval *b);
408
409 /** 'Exact' division. */
410 tarval *tarval_quo(tarval *a, tarval *b);
411
412 /** Integer division. */
413 tarval *tarval_div(tarval *a, tarval *b);
414
415 /** Remainder of integer division. */
416 tarval *tarval_mod(tarval *a, tarval *b);
417
418 /** Absolute value. */
419 tarval *tarval_abs(tarval *a);
420
421 /** Bitwise and. */
422 tarval *tarval_and(tarval *a, tarval *b);
423
424 /** Bitwise or. */
425 tarval *tarval_or(tarval *a, tarval *b);
426
427 /** Bitwise exclusive or. */
428 tarval *tarval_eor(tarval *a, tarval *b);
429
430 /** Left shift. */
431 tarval *tarval_shl(tarval *a, tarval *b);
432
433 /** Unsigned (logical) right shift. */
434 tarval *tarval_shr(tarval *a, tarval *b);
435
436 /** Signed (arithmetic) right shift. */
437 tarval *tarval_shrs(tarval *a, tarval *b);
438
439 /** Rotation. */
440 tarval *tarval_rot(tarval *a, tarval *b);
441
442 /** Carry flag of the last operation */
443 int tarval_carry(void);
444
445 /* *********** Output of tarvals *********** */
446
447 /**
448  * The output mode for tarval values.
449  *
450  * Some modes allow more that one representation, for instance integers
451  * can be represented hex or decimal. Of course it would be enough to have
452  * one and let every backend convert it into the 'right' one.
453  * However, we can do this in the tarval much simpler...
454  */
455 typedef enum {
456   TVO_NATIVE,       /**< the default output mode, depends on the mode */
457   TVO_HEX,          /**< use hex representation, always possible */
458   TVO_DECIMAL,      /**< use decimal representation */
459   TVO_OCTAL,        /**< use octal representation */
460   TVO_BINARY,       /**< use binary representation */
461   TVO_FLOAT,        /**< use floating point representation (i.e 1.342e-2)*/
462   TVO_HEXFLOAT      /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
463 } tv_output_mode;
464
465 /**
466  * This structure contains helper information to format the output
467  * of a tarval of a mode.
468  */
469 typedef struct tarval_mode_info {
470   tv_output_mode mode_output;  /**< if != TVO_NATIVE select a special mode */
471   const char *mode_prefix;     /**< if set, this prefix will be printed
472                                     before a value of this mode */
473   const char *mode_suffix;     /**< if set, this suffix will be printed
474                                     after a value of this mode */
475 } tarval_mode_info;
476
477 /**
478  * Specify the output options of one mode.
479  *
480  * This functions stores the mode info, so DO NOT DESTROY it.
481  *
482  * @param mode      a ir_mode that should be associated
483  * @param modeinfo  the output format info
484  *
485  * @return zero on success.
486  */
487 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
488
489 /**
490  * Returns the output options of one mode.
491  *
492  * This functions returns the mode info of a given mode.
493  *
494  * @param mode      a ir_mode that should be associated
495  *
496  * @return the output option
497  */
498 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode);
499
500 /**
501  * Returns Bit representation of a tarval value, as string of '0' and '1'
502  *
503  * @param tv   The tarval
504  *
505  * This function returns a printable bit representation of any value
506  * stored as tarval. This representation is a null terminated C string.
507  *
508  * @return
509  *   As usual in C a pointer to a char is returned. The length of the
510  *   returned string if fixed, just read as many chars as the mode defines
511  *   as size.
512  *
513  * @note
514  *   The string is allocated using malloc() and is free()ed on the next call
515  *   of this function.
516  *   The string consists of the ASCII characters '0' and '1' and is
517  *   null terminated
518  *
519  * @sa
520  *    irmode.h for the definition of the ir_mode struct
521  *    the size member of aforementioned struct
522  */
523 char *get_tarval_bitpattern(tarval *tv);
524
525 /**
526  * Returns the bitpattern of the bytes_ofs byte.
527  *
528  * This function succeeds even if the mode of the tarval uses lesser bits
529  * than requested, in that case the bitpattern is filled with zero bits.
530  *
531  * To query a 32bit value the following code can be used:
532  *
533  * val0 = tarval_sub_bits(tv, 0);
534  * val1 = tarval_sub_bits(tv, 1);
535  * val2 = tarval_sub_bits(tv, 2);
536  * val3 = tarval_sub_bits(tv, 3);
537  *
538  * Because this is the bit representation of the target machine, only the following
539  * operations are legal on the result:
540  *
541  * - concatenation (endian dependence MUST be handled by the CALLER)
542  * - bitwise logical operations to select/mask bits
543  *
544  * @param tv        the tarval
545  * @param byte_ofs  the byte offset
546  *
547  * @note
548  *   The result of this function is undefined if the mode is neither integer nor float.
549  */
550 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs);
551
552 /**
553  * Return values of tarval classify
554  */
555 typedef enum _tarval_classification_t {
556   TV_CLASSIFY_NULL    =  0, /**< the tarval represents the additive neutral element */
557   TV_CLASSIFY_ONE     = +1, /**< the tarval represents the multiplicative neutral element */
558   TV_CLASSIFY_ALL_ONE = -1, /**< the tarval represents the bitwise-and neutral element */
559   TV_CLASSIFY_OTHER   =  2  /**< all other tarvals */
560 } tarval_classification_t;
561
562 /**
563  * Identifying tarvals values for algebraic simplifications.
564  *
565  * @param tv        the tarval
566  *
567  * @return
568  *   - TV_CLASSIFY_NULL    for additive neutral or the NULL tarval for reference modes,
569  *   - TV_CLASSIFY_ONE     for multiplicative neutral,
570  *   - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
571  *   - TV_CLASSIFY_OTHER   else
572  */
573 tarval_classification_t classify_tarval(tarval *tv);
574
575 /**
576  * Returns non-zero if a given (integer) tarval has only one single bit
577  * set.
578  */
579 int tarval_is_single_bit(tarval *tv);
580
581 /**
582  * Output of tarvals to a buffer.
583  */
584 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
585
586 /**
587  * Output of tarvals to stdio.
588  */
589 int tarval_printf(tarval *tv);
590
591 /**
592  * Returns non-zero if the mantissa of a floating point IEEE-754
593  * tarval is zero (i.e. 1.0Exxx)
594  */
595 int tarval_ieee754_zero_mantissa(tarval *tv);
596
597 /**
598  * Returns the exponent of a floating point IEEE-754
599  * tarval.
600  */
601 int tarval_ieee754_get_exponent(tarval *tv);
602
603 /**
604  * Set the immediate precision for IEEE-754 results. Set this to
605  * 0 to get the same precision as the operands.
606  * For x87 compatibility, set this to 80.
607  *
608  * @return the old setting
609  */
610 unsigned tarval_ieee754_set_immediate_precision(unsigned bits);
611
612 /**
613  * Enable/Disable floating point constant folding.
614  */
615 int tarval_enable_fp_ops(int enable);
616
617 #endif  /* FIRM_TV_TV_H */