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