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