New implementation of tarval module
[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 <stdbool.h>
50 # include "irnode.h"    /* for pnc_number enum */
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 /****f* tv/new_tarval_from_str
103  *
104  * NAME
105  *    new_tarval_from_str
106  *   Constructor function for new tarvals.
107  *
108  * SYNOPSIS
109  *    tarval *new_tarval_from_str(const char *s, size_t len, ir_mode *mode)
110  *
111  * DESCRIPTION
112  *    This function creates a new tarval representing the value represented
113  *   by a CString, aka char array. If a tarval representing this value already
114  *   exists, this tarval is returned instead of a new one. So tarvals are
115  *   directly comparable since their representation is unique.
116  *
117  * PARAMETERS
118  *   str  - The String representing the target value
119  *   len  - The length of the string
120  *   mode - The mode requested for the result tarval
121  *
122  *    This function accepts the following strings:
123  *   if mode is int_number:
124  *      0(x|X)[0-9a-fA-F]+ (hexadecimal representation)
125  *      0[0-7]*            (octal representation)
126  *      (+|-)?[1-9][0-9]*  (decimal representation)
127  *   if mode if float_number:
128  *      (+|-)?(decimal int) (. (decimal int))? ((e|E)(+|-)?(decimal int))?
129  *   if mode is boolean: true, True, TRUE ... False... 0, 1,
130  *   if mode is reference: hexadecimal of decimal number as int
131  *   if mode is character: hex or dec
132  *    Leading and/or trailing spaces are ignored
133  *
134  * RESULT
135  *    A tarval of proper type representing the requested value is returned.
136  *   Tarvals are unique, so for any value/mode pair at most one tarval will
137  *   exist, which will be returned upon further requests with an identical
138  *   value/mode pair.
139  *
140  * NOTES
141  *    If the string is not representable in the given mode an assertion is
142  *   thrown.
143  *
144  * SEE ALSO
145  *   irmode.h for predefined modes
146  *   new_tarval_from_long
147  *   new_tarval_from_double
148  *
149  ******/
150 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode);
151
152 /****f* tv/new_tarval_from_long
153  *
154  * NAME
155  *    new_tarval_from_long
156  *   Constructor function for new tarvals
157  *
158  * SYNOPSIS
159  *    tarval *new_tarval_from_long(const long l. ir_mode *mode)
160  *
161  * DESCRIPTION
162  *    This function creates a new tarval representing the value represented
163  *   by a long integer. If a tarval representing this value already exists,
164  *   this tarval is returned instead of a new one. So tarvals are directly
165  *   comparable since their representation is unique.
166  *
167  * PARAMETERS
168  *   l    - The long representing the value
169  *   mode - The mode requested for the result tarval
170  *
171  * RESULT
172  *    A tarval of proper type representing the requested value is returned.
173  *   Tarvals are unique, so for any value/mode pair at most one tarval will
174  *   exist, which will be returned upon further requests with an identical
175  *   value/mode pair.
176  *
177  * NOTES
178  *    If the long is not representable in the given mode an assertion is
179  *   thrown.
180  *
181  * SEE ALSO
182  *   irmode.h for predefined modes
183  *   new_tarval_from_str
184  *   new_tarval_from_double
185  *
186  ******/
187
188 tarval *new_tarval_from_long(long l, ir_mode *mode);
189 /**
190  * This returns a long int with the value represented value, or
191  * gibberish, depending on the size of long int and the size of the
192  * stored value. It works for e.g. 1 as mode_Ls, but not for
193  * get_mode_max(mode_Ls).
194  * This will overflow silently, so use only if you know what
195  * you are doing! (better check with tarval_is_long...)
196  */
197 long tarval_to_long(tarval *tv);
198 /**
199  * This validates if tarval_to_long will return a satisfying
200  * result. I.e. if tv is an int_number and between min, max
201  * of long int (signed!)
202  */
203 int tarval_is_long(tarval *tv);
204
205 /****f* tv/new_tarval_from_double
206  *
207  * NAME
208  *    new_tarval_from_double
209  *   Constructor function for new tarvals
210  *
211  * SYNOPSIS
212  *    tarval *new_tarval_from_double(const long double d, ir_mode *mode)
213  *
214  * DESCRIPTION
215  *    This function creates a new tarval representing the value represented
216  *   by a long double. If a tarval representing this value already exists,
217  *   this tarval is returned instead of a new one. So tarvals are directly
218  *   comparable since their representation is unique.
219  *
220  * PARAMETERS
221  *   d    - The long double representing the value
222  *   mode - The mode requested for the result tarval
223  *
224  *    Only modes of sort float_number can be constructed this way.
225  *
226  * RESULT
227  *    A tarval of proper type representing the requested value is returned.
228  *   Tarvals are unique, so for any value/mode pair at most one tarval will
229  *   exist, which will be returned upon further requests with an identical
230  *   value/mode pair.
231  *
232  * NOTES
233  *    If the long double is not representable in the given mode an assertion
234  *   is thrown. This will happen for any mode not of sort float_number
235  *
236  * SEE ALSO
237  *   irmode.h for predefined values
238  *   new_tarval_from_str
239  *   new_tarval_from_long
240  *
241  ******/
242 tarval *new_tarval_from_double(long double d, ir_mode *mode);
243 long double tarval_to_double(tarval *tv);
244 int tarval_is_double(tarval *tv);
245 /* The tarval represents the address of the entity.  As the address must
246    be constant the entity must have as owner the global type. */
247 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode);
248 entity *tarval_to_entity(tarval *tv);
249 int tarval_is_entity(tarval *tv);
250
251 /** ********** Access routines for tarval fields ********** **/
252
253 /****f* tv/get_tarval_*
254  *
255  * NAME
256  *   get_tarval_mode
257  *   get_tarval_ ...
258  *
259  * SYNOPSIS
260  *   ir_mode *get_tarval_mode(tarval *tv)
261  *   ...
262  *
263  * DESCRIPTION
264  *    These are access function for tarval struct members. It is encouraged
265  *   to use them instead of direct access to the struct fields.
266  *
267  * PARAMETERS
268  *   tv - The tarval to access fields of
269  *
270  * RESULT
271  *   get_tv_mode: The mode of the tarval
272  *
273  * SEE ALSO
274  *   the struct tarval
275  *
276  ******/
277 /* get the mode of the tarval */
278 #ifdef TARVAL_ACCESS_DEFINES
279 #  include "tv_t.h"
280 #  define get_tarval_mode(tv) (tv)->mode
281 #else
282 ir_mode *get_tarval_mode (tarval *tv);
283 #endif
284 /** Testing properties of the represented values **/
285 /* Returns 0 if tv is positive, else > 0. @@@ not tested! */
286 int tarval_is_negative(tarval *a);
287
288 /** Some special values **/
289 extern tarval *tarval_bad;                  tarval *get_tarval_bad(void);
290 extern tarval *tarval_undefined;            tarval *get_tarval_undefined(void);
291 /* These two are the only valid mode_b tarvals! */
292 extern tarval *tarval_b_false;              tarval *get_tarval_b_false(void);
293 extern tarval *tarval_b_true;               tarval *get_tarval_b_true(void);
294
295 extern tarval *tarval_P_void;               tarval *get_tarval_P_void(void);
296
297 /* These functions calculate and return a tarval representing the requested
298  * value.
299  * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
300  * functions, but these are stored on initialization of the irmode module and
301  * therefore the irmode functions should be prefered to the functions below. */
302 tarval *get_tarval_max(ir_mode *mode);
303 tarval *get_tarval_min(ir_mode *mode);
304 tarval *get_tarval_null(ir_mode *mode);
305 tarval *get_tarval_one(ir_mode *mode);
306 tarval *get_tarval_nan(ir_mode *mode);
307 tarval *get_tarval_inf(ir_mode *mode);
308
309 /* ******************** Arithmethic operations on tarvals ******************** */
310
311 /****f* tv/tarval_cmp
312  *
313  * NAME
314  *    tarval_cmp
315  *   Compares two tarvals
316  *
317  * SYNOPSIS
318  *    pnc_number tarval_comp(tarval *a, tarval *b)
319  *
320  * DESCRIPTION
321  *    Compare a with b and return a pnc_number describing the relation
322  *   between a and b.  This is either Uo, Lt, Eq, Gt, or False if a or b
323  *   are symbolic pointers which can not be compared at all.
324  *
325  * PARAMETERS
326  *    a - A tarval
327  *    b - A tarval
328  *   a and b are tarvals to be compared
329  *
330  * RESULT
331  *    The pnc_number best describing the relation between a and b is returned.
332  *   This means the mode with the least bits set is returned, e.g. if the
333  *   tarvals are equal the pnc_number 'Eq' is returned, not 'Ge' which
334  *   indicates 'greater or equal'
335  *
336  * SEE ALSO
337  *    irnode.h for the definition of pnc_numbers
338  *
339  ******/
340 pnc_number tarval_cmp(tarval *a, tarval *b);
341
342 /****f* tv/tarval_convert_to
343  *
344  * NAME
345  *    tarval_convert_to
346  *   Converts a tarval to another mode
347  *
348  * SYNOPSIS
349  *    tarval *tarval_convert_to(tarval *src, ir_mode *mode)
350  *
351  * DESCRIPTION
352  *    Convert tarval 'src' to mode 'mode', this will suceed if and only if mode
353  *   'mode' is wider than the mode of src, as defined in the firm documentation
354  *   and as returned by the function mode_is_smaller defined in irmode.h.
355  *
356  * PARAMETERS
357  *    src  - The tarval to convert
358  *    mode - Tho mode to convert to
359  *
360  * RESULT
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  * NOTES
366  *    Illegal conversations will trigger an assertion
367  *
368  * SEE ALSO
369  *    FIRM documentation for conversion rules
370  *    mode_is_smaller defined in irmode.h
371  *
372  ******/
373 tarval *tarval_convert_to(tarval *src, ir_mode *m);
374
375 /****f* tv/tarval_calculations
376  *
377  * NAME
378  *    tarval_neg  - Negation of a tarval
379  *    tarval_add  - Addition of two tarvals
380  *    tarval_sub  - Subtraction from a tarval
381  *    tarval_mul  - Multiplication of tarvals
382  *    tarval_quo  - 'Exact' division
383  *    tarval_div  - Integer division
384  *    tarval_mod  - Remainder of integer division
385  *    tarval_abs  - Absolute value
386  *    tarval_and  - Bitwise and
387  *    tarval_or   - Bitwise or
388  *    tarval_eor  - Bitwise exclusive or
389  *    tarval_shl  - Left shift
390  *    tarval_shr  - Unsigned right shift
391  *    tarval_shrs - Signed right shift
392  *    tarval_rot  - Rotation
393  *
394  * SYNOPSIS
395  *    tarval *tarval_neg (tarval *a)
396  *    tarval *tarval_add (tarval *a, tarval *b)
397  *    tarval *tarval_sub (tarval *a, tarval *b)
398  *    tarval *tarval_mul (tarval *a, tarval *b)
399  *    tarval *tarval_quo (tarval *a, tarval *b)
400  *    tarval *tarval_div (tarval *a, tarval *b)
401  *    tarval *tarval_mod (tarval *a, tarval *b)
402  *    tarval *tarval_abs (tarval *a)
403  *    tarval *tarval_and (tarval *a, tarval *b)
404  *    tarval *tarval_or  (tarval *a, tarval *b)
405  *    tarval *tarval_eor (tarval *a, tarval *b)
406  *    tarval *tarval_shl (tarval *a, tarval *b)
407  *    tarval *tarval_shr (tarval *a, tarval *b)
408  *    tarval *tarval_shrs(tarval *a, tarval *b)
409  *    tarval *tarval_rot (tarval *a, tarval *b)
410  *
411  * DESCRIPTION
412  *    These function implement basic computations representable as opcodes
413  *   in FIRM nodes.
414  *
415  * PARAMETERS
416  *    tarval_neg:
417  *    traval_abs:
418  *      a - the tarval to operate on
419  *
420  *    all oters:
421  *      a - the first operand tarval
422  *      b - the second operand tarval
423  *
424  * RESULT
425  *    If neccessary a new tarval is constructed for the resulting value,
426  *   or the one already carrying the computation result is retrieved and
427  *   returned as result.
428  *
429  * NOTES
430  *    The order the arguments are given in is important, imagine postfix
431  *   notation.
432  *    Illegal operations will trigger an assertion.
433  *    The sort member of the struct mode defines which operations are valid
434  *
435  ******/
436 tarval *tarval_neg(tarval *a);             /* negation */
437 tarval *tarval_add(tarval *a, tarval *b);  /* addition */
438 tarval *tarval_sub(tarval *a, tarval *b);  /* subtraction */
439 tarval *tarval_mul(tarval *a, tarval *b);  /* multiplication */
440 tarval *tarval_quo(tarval *a, tarval *b);  /* floating point division */
441 tarval *tarval_div(tarval *a, tarval *b);  /* integer division */
442 tarval *tarval_mod(tarval *a, tarval *b);  /* remainder */
443 tarval *tarval_abs(tarval *a);             /* absolute value */
444 tarval *tarval_and(tarval *a, tarval *b);  /* bitwise and */
445 tarval *tarval_or (tarval *a, tarval *b);  /* bitwise or */
446 tarval *tarval_eor(tarval *a, tarval *b);  /* bitwise exclusive or (xor) */
447 tarval *tarval_shl(tarval *a, tarval *b);  /* bitwise left shift */
448 tarval *tarval_shr(tarval *a, tarval *b);  /* bitwise unsigned right shift */
449 tarval *tarval_shrs(tarval *a, tarval *b); /* bitwise signed right shift */
450 tarval *tarval_rot(tarval *a, tarval *b);  /* bitwise rotation */
451
452 /** *********** Output of tarvals *********** **/
453 /****f* tv/tarval_bitpattern
454  *
455  * NAME
456  *    tarval_bitpattern
457  *   Bit representation of a tarval value, as string of '0' and '1'
458  *
459  * SYNOPSIS
460  *    char *tarval_bitpattern(tarval *tv)
461  *
462  * DESCRIPTION
463  *    This function returns a printable bit representation of any value
464  *   stored as tarval. This representation is a null terminated C string.
465  *
466  * PARAMETERS
467  *    tv - The tarval
468  *
469  * RESULT
470  *    As usual in C a pointer to a char is returned. The length of the
471  *   returned string if fixed, just read as many chars as the mode defines
472  *   as size.
473  *
474  * NOTE
475  *    The string is allocated using malloc() and is free()ed on the next call
476  *    of this function.
477  *    The string consists of the ascii characters '0' and '1' and is
478  *    null terminated
479  *
480  * SEE ALSO
481  *    irmode.h for the definition of the ir_mode struct
482  *    the size member of aforementioned struct
483  *
484  ******/
485 char *tarval_bitpattern(tarval *tv);
486
487 /* Identifying some tarvals ??? */
488 /* This function is deprecated and its use strongly discouraged */
489 long tarval_classify(tarval *tv);
490
491 /** Initialization of the tarval module **/
492 void init_tarval_1(void); /* call before init_mode */
493 void init_tarval_2(void); /* call after init_mode */
494
495 #endif  /* _TV_H_ */