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