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