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