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