normalized name
[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 "entity.h"
25 # include "irnode.h"    /* for pnc_number enum */
26
27
28 /****h* libfirm/tv
29  *
30  * NAME
31  *    tv -- TargetValue, short tarval.
32  *   Internal representation for machine values.
33  *
34  * AUTHORS
35  *    Matthias Heil
36  *
37  * DESCRIPTION
38  *    Tarvals represent target machine values.  They are typed by modes.
39  *   Tarvals only represent values of mode_sort:
40  *     int_number,
41  *     float_number,
42  *     boolean,
43  *     reference,
44  *     character
45  *
46  *   In case of references the module accepts an entity to represent the
47  *   value.
48  *    Furthermore, computations and conversions of these values can
49  *   be performed.
50  *
51  * USES
52  *    This module is closely related to the irmode module, as the modes
53  *   defined there are thoroughly used throughout the whole module.
54  *    Also, the comparison functions rely on the definition of comparison
55  *   values in the irnode module.
56  *
57  * HISTORY
58  *    The original tv module originated in the fiasco compiler written ...
59  *    This is the new version, described in the tech report 1999-14 by ...
60  *
61  * SEE ALSO
62  *    Techreport 1999-14
63  *    irmode.h for the modes definitions
64  *    irnode.h for the pnc_numbers table
65  *
66  *    tarval_init1 and tarval_init2 for initialization of the
67  *   module
68  *
69  ******/
70
71 #ifndef _TARVAL_TYPEDEF_
72 #define _TARVAL_TYPEDEF_
73   typedef struct tarval tarval;
74 #endif
75
76 /* ************************ Constructors for tarvals ************************ */
77
78 /**
79  * Constructor function for new tarvals.
80  *
81  * @param str   The string representing the target value
82  * @param len   The length of the string
83  * @param mode  The mode requested for the result tarval
84  *
85  * This function creates a new tarval representing the value represented
86  * by a CString, aka char array. If a tarval representing this value already
87  * exists, this tarval is returned instead of a new one. So tarvals are
88  * directly comparable since their representation is unique.
89  *
90  * This function accepts the following strings:
91  *
92  * if mode is int_number:
93  *  - 0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
94  *  - 0[0-7]*            (octal representation)
95  *  - (+|-)?[1-9][0-9]*  (decimal representation)
96  *
97  * if mode if float_number:
98  *  - (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
99  *
100  * if mode is boolean: true, True, TRUE ... False... 0, 1,
101  *
102  * if mode is reference: hexadecimal of decimal number as int
103  *
104  * if mode is character: hex or dec
105  *
106  * Leading and/or trailing spaces are ignored
107  *
108  * @return
109  *   A tarval of proper type representing the requested value is returned.
110  *   Tarvals are unique, so for any value/mode pair at most one tarval will
111  *   exist, which will be returned upon further requests with an identical
112  *   value/mode pair.
113  *
114  * @note
115  *   If the string is not representable in the given mode an assertion is
116  *   thrown in assert build.
117  *
118  * @sa
119  *   irmode.h for predefined modes
120  *   new_tarval_from_long()
121  *   new_tarval_from_double()
122  */
123 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
124
125 /**
126  * Constructor function for new tarvals
127  *
128  * @param l     The long representing the value
129  * @param mode  The mode requested for the result tarval
130  *
131  * This function creates a new tarval representing the value represented
132  * by a long integer. If a tarval representing this value already exists,
133  * this tarval is returned instead of a new one. So tarvals are directly
134  * comparable since their representation is unique.
135  *
136  * @return
137  *   A tarval of proper type representing the requested value is returned.
138  *   Tarvals are unique, so for any value/mode pair at most one tarval will
139  *   exist, which will be returned upon further requests with an identical
140  *   value/mode pair.
141  *
142  * @note
143  *   If the long is not representable in the given mode an assertion is
144  *   thrown in assert build.
145  *
146  * @sa
147  *   irmode.h for predefined modes
148  *   new_tarval_from_str()
149  *   new_tarval_from_double()
150  *
151  */
152 tarval *new_tarval_from_long(long l, ir_mode *mode);
153
154 /** Return value as long if possible.
155  *
156  * This returns a long int with the value represented value, or
157  * gibberish, depending on the size of long int and the size of the
158  * stored value. It works for e.g. 1 as mode_Ls, but might not work for
159  * get_mode_max(mode_Ls).
160  * This will overflow silently, so use only if you know what
161  * you are doing! (better check with tarval_is_long()...)
162  * Works only for int modes, even not for character modes!
163  */
164 long tarval_to_long(tarval *tv);
165 #define get_tarval_long tarval_to_long
166
167 /**
168  * This validates if tarval_to_long() will return a satisfying
169  * result. I.e. if tv is an int_number and between min, max
170  * of long int (signed!)
171  */
172 int tarval_is_long(tarval *tv);
173
174 /**
175  * Constructor function for new tarvals.
176  *
177  * @param d     The (long) double representing the value
178  * @param mode  The mode requested for the result tarval
179  *
180  * This function creates a new tarval representing the value represented
181  * by a (long) double. If a tarval representing this value already exists,
182  * this tarval is returned instead of a new one. So tarvals are directly
183  * comparable since their representation is unique.
184  * Only modes of sort float_number can be constructed this way.
185  *
186  * @return
187  *   A tarval of proper type representing the requested value is returned.
188  *   Tarvals are unique, so for any value/mode pair at most one tarval will
189  *   exist, which will be returned upon further requests with an identical
190  *   value/mode pair.
191  *
192  * @note
193  *   If the (long) double is not representable in the given mode an assertion
194  *   is thrown. This will happen for any mode not of sort float_number.
195  *
196  * @sa
197  *   irmode.h for predefined values
198  *   new_tarval_from_str()
199  *   new_tarval_from_long()
200  */
201 tarval *new_tarval_from_double(long double d, ir_mode *mode);
202
203 /**
204  * This returns a double with the value represented value, or
205  * gibberish, depending on the size of double and the size of the
206  * stored value.
207  * This will overflow silently, so use only if you know what
208  * you are doing! (better check with tarval_is_long...)
209  */
210 long double tarval_to_double(tarval *tv);
211
212 /**
213  * This validates if tarval_to_double() will return a satisfying
214  * result. I.e. if tv is an float_number and between min, max
215  * of double
216  */
217 int tarval_is_double(tarval *tv);
218
219 /**
220  * Construct a tarval that represents the address of the entity.
221  *
222  * The address must be constant, the entity must have as owner the global type.
223  * We no more support this function: Use the new SymConst instead.
224  */
225 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode);
226
227 /**
228  * Returns the associated entity of a tarval.  Asserts if tarval does not
229  * contain an entity.
230  */
231 #define get_tarval_entity tarval_to_entity
232 entity *tarval_to_entity(tarval *tv);
233
234 /**
235  * Returns non-zero if a the given tarval represents an entity.
236  */
237 int tarval_is_entity(tarval *tv);
238
239 /** ********** Access routines for tarval fields ********** **/
240
241 /*
242  * NAME
243  *   get_tarval_mode
244  *   get_tarval_ ...
245  *
246  * SYNOPSIS
247  *   ir_mode *get_tarval_mode(tarval *tv)
248  *   ...
249  *
250  * DESCRIPTION
251  *    These are access function for tarval struct members. It is encouraged
252  *   to use them instead of direct access to the struct fields.
253  *
254  * PARAMETERS
255  *   tv - The tarval to access fields of
256  *
257  * RESULT
258  *   get_tv_mode: The mode of the tarval
259  *
260  * SEE ALSO
261  *   the struct tarval
262  */
263
264 /** Returns the mode of the tarval. */
265 ir_mode *get_tarval_mode (tarval *tv);
266
267 /* Testing properties of the represented values */
268
269 /**
270  * Returns 1 if tv is negative
271  *
272  * @param a     the tarval
273  */
274 int tarval_is_negative(tarval *a);
275
276 /**
277  * Returns 1 if tv is null
278  *
279  * @param a     the tarval
280  */
281 int tarval_is_null(tarval *a);
282
283 /**
284  * Returns 1 if tv is the "one"
285  *
286  * @param a     the tarval
287  */
288 int tarval_is_one(tarval *a);
289
290 /** The 'bad' tarval. */
291 extern tarval *tarval_bad;
292 /** Returns the 'bad tarval. */
293 tarval *get_tarval_bad(void);
294
295 /** The 'undefined' tarval. */
296 extern tarval *tarval_undefined;
297 /** Returns the 'undefined' tarval. */
298 tarval *get_tarval_undefined(void);
299
300 /** The mode_b tarval 'false'. */
301 extern tarval *tarval_b_false;
302 /** Returns the mode_b tarval 'false'. */
303 tarval *get_tarval_b_false(void);
304
305 /** The mode_b tarval 'true'. */
306 extern tarval *tarval_b_true;
307 /** Returns the mode_b tarval 'true'. */
308 tarval *get_tarval_b_true(void);
309
310 /** The 'void' pointer tarval. */
311 extern tarval *tarval_P_void;
312 /** Returns the 'void' pointer tarval. */
313 tarval *get_tarval_P_void(void);
314
315 /* These functions calculate and return a tarval representing the requested
316  * value.
317  * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
318  * functions, but these are stored on initialization of the irmode module and
319  * therefore the irmode functions should be prefered to the functions below. */
320
321 /** Returns the maximum value of a given mode. */
322 tarval *get_tarval_max(ir_mode *mode);
323
324 /** Returns the minimum value of a given mode. */
325 tarval *get_tarval_min(ir_mode *mode);
326
327 /** Returns the 0 value (additive neutral) of a given mode. */
328 tarval *get_tarval_null(ir_mode *mode);
329
330 /** Returns the 1 value (multiplicative neutral) of a given mode. */
331 tarval *get_tarval_one(ir_mode *mode);
332
333 /** Return quite nan for float_number modes. */
334 tarval *get_tarval_nan(ir_mode *mode);
335
336 /** Return +inf for float_number modes. */
337 tarval *get_tarval_inf(ir_mode *mode);
338
339 /* ******************** Arithmethic operations on tarvals ******************** */
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 /* *********** Output of tarvals *********** */
460
461 /**
462  * The output mode for tarval values.
463  *
464  * Some modes allow more that one representation, for instance integers
465  * can be represented hex or decimal. Of course it would be enough to have
466  * one and let every backend convert it into the 'right' one.
467  * However, we can do this in the tarval much simplier...
468  */
469 typedef enum {
470   TVO_NATIVE,                   /**< the default output mode, depends on the mode */
471   TVO_HEX,                      /**< use hex representation, always possible */
472   TVO_DECIMAL,                  /**< use decimal representation */
473   TVO_OCTAL,                    /**< use octal representation */
474   TVO_BINARY,                   /**< use binary representation */
475   TVO_FLOAT,                    /**< use floating point representation (i.e 1.342e-2)*/
476   TVO_HEXFLOAT                  /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
477 } tv_output_mode;
478
479 /**
480  * This structure contains helper information to format the output
481  * of a tarval of a mode.
482  */
483 typedef struct tarval_mode_info {
484     tv_output_mode mode_output;         /**< if != TVO_NATIVE select a special mode */
485     const char *mode_prefix;            /**< if set, this prefix will be printed
486                                              before a value of this mode */
487     const char *mode_suffix;            /**< if set, this suffix will be printed
488                                              after a value of this mode */
489 } tarval_mode_info;
490
491 /**
492  * Specify the output options of one mode.
493  *
494  * This functions stores the modinfo, so DO NOT DESTROY it.
495  *
496  * @param mode          a ir_mode that should be associated
497  * @param modeinfo      the output format info
498  *
499  * @return zero on success.
500  */
501 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
502
503 /**
504  * Returns the output options of one mode.
505  *
506  * This functions returns the modinfo of a given mode.
507  *
508  * @param mode          a ir_mode that should be associated
509  *
510  * @return the output option
511  */
512 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode);
513
514 /**
515  * Returns Bit representation of a tarval value, as string of '0' and '1'
516  *
517  * @param tv   The tarval
518  *
519  * This function returns a printable bit representation of any value
520  * stored as tarval. This representation is a null terminated C string.
521  *
522  * @return
523  *   As usual in C a pointer to a char is returned. The length of the
524  *   returned string if fixed, just read as many chars as the mode defines
525  *   as size.
526  *
527  * @note
528  *   The string is allocated using malloc() and is free()ed on the next call
529  *   of this function.
530  *   The string consists of the ascii characters '0' and '1' and is
531  *   null terminated
532  *
533  * @sa
534  *    irmode.h for the definition of the ir_mode struct
535  *    the size member of aforementioned struct
536  */
537 char *tarval_bitpattern(tarval *tv);
538
539 /**
540  * Returns the bitpattern of the bytes_ofs byte.
541  *
542  * This function succeeds even if the mode of the tarval uses lesser bits
543  * than requested, in that case the bitpattern is filled with zero bits.
544  *
545  * To query a 32bit value the following code can be used:
546  *
547  * val0 = tarval_sub_bits(tv, 0);
548  * val1 = tarval_sub_bits(tv, 1);
549  * val2 = tarval_sub_bits(tv, 2);
550  * val3 = tarval_sub_bits(tv, 3);
551  *
552  * Because this is the bit representation of the target machine, only the following
553  * operations are legal on the result:
554  *
555  * - concatenation (endian dependance MUST be handled by the CALLER)
556  * - bitwise logical operations to select/mask bits
557  *
558  * @param tv            the tarval
559  * @param byte_ofs      the byte offset
560  *
561  * @note
562  *   The result of this funcion is undefined if the mode is neither integer nor float.
563  */
564 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs);
565
566 /**
567  * Return values of tarval classify
568  */
569 typedef enum _tarval_classification_t {
570   TV_CLASSIFY_NULL    =  0,     /**< the tarval represents the additive neutral element */
571   TV_CLASSIFY_ONE     = +1,     /**< the tarval represents the multiplicative neutral element */
572   TV_CLASSIFY_ALL_ONE = -1,     /**< the tarval represents the bitwise-and neutral element */
573   TV_CLASSIFY_OTHER   =  2      /**< all other tarvals */
574 } tarval_classification_t;
575
576 /**
577  * Identifying tarvals values for algebraic simplifications.
578  * @param tv
579  * @return
580  *   - TV_CLASSIFY_NULL    for additive neutral,
581  *   - TV_CLASSIFY_ONE     for multiplicative neutral,
582  *   - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
583  *   - TV_CLASSIFY_OTHER   else
584  */
585 tarval_classification_t tarval_classify(tarval *tv);
586
587 /**
588  * Initialization of the tarval module.
589  *
590  * Call before init_mode().
591  */
592 void init_tarval_1(void);
593
594 /**
595  * Initialization of the tarval module.
596  *
597  * Call after init_mode().
598  */
599 void init_tarval_2(void);
600
601 void finish_tarval(void);
602
603 /**
604  * Output of tarvals to a buffer.
605  */
606 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
607
608 /**
609  * Output of tarvals to stdio.
610  */
611 int tarval_printf(tarval *tv);
612
613 #endif  /* _TV_H_ */