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