replaced variable args macros by functions to make it c89 compatible
[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 /**
155  * This returns a long int with the value represented value, or
156  * gibberish, depending on the size of long int and the size of the
157  * stored value. It works for e.g. 1 as mode_Ls, but might not work for
158  * get_mode_max(mode_Ls).
159  * This will overflow silently, so use only if you know what
160  * you are doing! (better check with tarval_is_long()...)
161  */
162 long tarval_to_long(tarval *tv);
163
164 /**
165  * This validates if tarval_to_long() will return a satisfying
166  * result. I.e. if tv is an int_number and between min, max
167  * of long int (signed!)
168  */
169 int tarval_is_long(tarval *tv);
170
171 /**
172  * Constructor function for new tarvals.
173  *
174  * @param d     The (long) double representing the value
175  * @param mode  The mode requested for the result tarval
176  *
177  * This function creates a new tarval representing the value represented
178  * by a (long) double. If a tarval representing this value already exists,
179  * this tarval is returned instead of a new one. So tarvals are directly
180  * comparable since their representation is unique.
181  * Only modes of sort float_number can be constructed this way.
182  *
183  * @return
184  *   A tarval of proper type representing the requested value is returned.
185  *   Tarvals are unique, so for any value/mode pair at most one tarval will
186  *   exist, which will be returned upon further requests with an identical
187  *   value/mode pair.
188  *
189  * @note
190  *   If the (long) double is not representable in the given mode an assertion
191  *   is thrown. This will happen for any mode not of sort float_number.
192  *
193  * @sa
194  *   irmode.h for predefined values
195  *   new_tarval_from_str()
196  *   new_tarval_from_long()
197  */
198 tarval *new_tarval_from_double(long double d, ir_mode *mode);
199
200 /**
201  * This returns a double with the value represented value, or
202  * gibberish, depending on the size of double and the size of the
203  * stored value.
204  * This will overflow silently, so use only if you know what
205  * you are doing! (better check with tarval_is_long...)
206  */
207 long double tarval_to_double(tarval *tv);
208
209 /**
210  * This validates if tarval_to_double() will return a satisfying
211  * result. I.e. if tv is an float_number and between min, max
212  * of double
213  */
214 int tarval_is_double(tarval *tv);
215
216 /**
217  * Construct a tarval that represents the address of the entity.
218  *
219  * The address must be constant, the entity must have as owner the global type.
220  */
221 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode);
222
223 /**
224  * Returns the associated entity of a tarval.  Asserts if tarval does not
225  * contain an entity.
226  */
227 #define get_tarval_entity tarval_to_entity
228 entity *tarval_to_entity(tarval *tv);
229
230 /**
231  * Returns non-zero if a the given tarval represents an entity.
232  */
233 int tarval_is_entity(tarval *tv);
234
235 /** ********** Access routines for tarval fields ********** **/
236
237 /*
238  * NAME
239  *   get_tarval_mode
240  *   get_tarval_ ...
241  *
242  * SYNOPSIS
243  *   ir_mode *get_tarval_mode(tarval *tv)
244  *   ...
245  *
246  * DESCRIPTION
247  *    These are access function for tarval struct members. It is encouraged
248  *   to use them instead of direct access to the struct fields.
249  *
250  * PARAMETERS
251  *   tv - The tarval to access fields of
252  *
253  * RESULT
254  *   get_tv_mode: The mode of the tarval
255  *
256  * SEE ALSO
257  *   the struct tarval
258  */
259
260 /** Returns the mode of the tarval. */
261 ir_mode *get_tarval_mode (tarval *tv);
262
263 /* Testing properties of the represented values */
264
265 /**
266  * Returns 1 if tv is negative
267  *
268  * @param a     the tarval
269  */
270 int tarval_is_negative(tarval *a);
271
272 /**
273  * Returns 1 if tv is null
274  *
275  * @param a     the tarval
276  */
277 int tarval_is_null(tarval *a);
278
279 /** The 'bad' tarval. */
280 extern tarval *tarval_bad;
281 /** Returns the 'bad tarval. */
282 tarval *get_tarval_bad(void);
283
284 /** The 'undefined' tarval. */
285 extern tarval *tarval_undefined;
286 /** Returns the 'undefined' tarval. */
287 tarval *get_tarval_undefined(void);
288
289 /** The mode_b tarval 'false'. */
290 extern tarval *tarval_b_false;
291 /** Returns the mode_b tarval 'false'. */
292 tarval *get_tarval_b_false(void);
293
294 /** The mode_b tarval 'true'. */
295 extern tarval *tarval_b_true;
296 /** Returns the mode_b tarval 'true'. */
297 tarval *get_tarval_b_true(void);
298
299 /** The 'void' pointer tarval. */
300 extern tarval *tarval_P_void;
301 /** Returns the 'void' pointer tarval. */
302 tarval *get_tarval_P_void(void);
303
304 /* These functions calculate and return a tarval representing the requested
305  * value.
306  * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
307  * functions, but these are stored on initialization of the irmode module and
308  * therefore the irmode functions should be prefered to the functions below. */
309
310 /** Returns the maximum value of a given mode. */
311 tarval *get_tarval_max(ir_mode *mode);
312
313 /** Returns the minimum value of a given mode. */
314 tarval *get_tarval_min(ir_mode *mode);
315
316 /** Returns the 0 value (additive neutral) of a given mode. */
317 tarval *get_tarval_null(ir_mode *mode);
318
319 /** Returns the 1 value (multiplicative neutral) of a given mode. */
320 tarval *get_tarval_one(ir_mode *mode);
321
322 /** Return quite nan for float_number modes. */
323 tarval *get_tarval_nan(ir_mode *mode);
324
325 /** Return +inf for float_number modes. */
326 tarval *get_tarval_inf(ir_mode *mode);
327
328 /* ******************** Arithmethic operations on tarvals ******************** */
329
330 /**
331  * Compares two tarvals
332  *
333  * Compare a with b and return a pnc_number describing the relation
334  * between a and b.  This is either Uo, Lt, Eq, Gt, or False if a or b
335  * are symbolic pointers which can not be compared at all.
336  *
337  * @param a   A tarval to be compared
338  * @param b   A tarval to be compared
339  *
340  * @return
341  *   The pnc_number best describing the relation between a and b is returned.
342  *   This means the mode with the least bits set is returned, e.g. if the
343  *   tarvals are equal the pnc_number 'Eq' is returned, not 'Ge' which
344  *   indicates 'greater or equal'
345  *
346  * @sa
347  *    irnode.h for the definition of pnc_numbers
348  */
349 pnc_number tarval_cmp(tarval *a, tarval *b);
350
351 /**
352  * Converts a tarval to another mode.
353  *
354  * Convert tarval 'src' to mode 'mode', this will suceed if and only if mode
355  * 'mode' is wider than the mode of src, as defined in the firm documentation
356  * and as returned by the function mode_is_smaller defined in irmode.h.
357  *
358  * @param src    The tarval to convert
359  * @param mode   Tho mode to convert to
360  *
361  * @return
362  *   If a tarval of mode 'mode' with the result of the conversion of the 'src'
363  *   tarvals value already exists, it will be returned, else a new tarval is
364  *   constructed and returned
365  *
366  * @note
367  *    Illegal conversations will trigger an assertion
368  *
369  * @sa
370  *    FIRM documentation for conversion rules
371  *    mode_is_smaller defined in irmode.h
372  */
373 tarval *tarval_convert_to(tarval *src, ir_mode *m);
374
375 /*
376  * These function implement basic computations representable as opcodes
377  * in FIRM nodes.
378  *
379  * PARAMETERS
380  *    tarval_neg:
381  *    traval_abs:
382  *      a - the tarval to operate on
383  *
384  *    all oters:
385  *      a - the first operand tarval
386  *      b - the second operand tarval
387  *
388  * RESULT
389  *    If neccessary a new tarval is constructed for the resulting value,
390  *   or the one already carrying the computation result is retrieved and
391  *   returned as result.
392  *
393  * NOTES
394  *   The order the arguments are given in is important, imagine postfix
395  *   notation.
396  *   Illegal operations will trigger an assertion.
397  *   The sort member of the struct mode defines which operations are valid
398  */
399
400 /** bitwise Negation of a tarval. */
401 tarval *tarval_not(tarval *a);
402
403 /** arithmetic Negation of a tarval. */
404 tarval *tarval_neg(tarval *a);
405
406 /** Addition of two tarvals. */
407 tarval *tarval_add(tarval *a, tarval *b);
408
409 /** Subtraction from a tarval. */
410 tarval *tarval_sub(tarval *a, tarval *b);
411
412 /** Multiplication of tarvals. */
413 tarval *tarval_mul(tarval *a, tarval *b);
414
415 /** 'Exact' division. */
416 tarval *tarval_quo(tarval *a, tarval *b);
417
418 /** Integer division. */
419 tarval *tarval_div(tarval *a, tarval *b);
420
421 /** Remainder of integer division. */
422 tarval *tarval_mod(tarval *a, tarval *b);
423
424 /** Absolute value. */
425 tarval *tarval_abs(tarval *a);
426
427 /** Bitwise and. */
428 tarval *tarval_and(tarval *a, tarval *b);
429
430 /** Bitwise or. */
431 tarval *tarval_or(tarval *a, tarval *b);
432
433 /** Bitwise exclusive or. */
434 tarval *tarval_eor(tarval *a, tarval *b);
435
436 /** Left shift. */
437 tarval *tarval_shl(tarval *a, tarval *b);
438
439 /** Unsigned (logical) right shift. */
440 tarval *tarval_shr(tarval *a, tarval *b);
441
442 /** Signed (arithmetic) right shift. */
443 tarval *tarval_shrs(tarval *a, tarval *b);
444
445 /** Rotation. */
446 tarval *tarval_rot(tarval *a, tarval *b);
447
448 /* *********** Output of tarvals *********** */
449
450 /**
451  * The output mode for tarval values.
452  *
453  * Some modes allow more that one representation, for instance integers
454  * can be represented hex or decimal. Of course it would be enough to have
455  * one and let every backend convert it into the 'right' one.
456  * However, we can do this in the tarval much simplier...
457  */
458 typedef enum {
459   TVO_NATIVE,                   /**< the default output mode, depends on the mode */
460   TVO_HEX,                      /**< use hex representation, always possible */
461   TVO_DECIMAL,                  /**< use decimal representation */
462   TVO_OCTAL,                    /**< use octal representation */
463   TVO_BINARY,                   /**< use binary representation */
464   TVO_FLOAT,                    /**< use floating point representation (i.e 1.342e-2)*/
465   TVO_HEXFLOAT                  /**< use hexadecimal floating point representation (i.e 0x1.ea32p-12)*/
466 } tv_output_mode;
467
468 /**
469  * This structure contains helper information to format the output
470  * of a tarval of a mode.
471  */
472 typedef struct tarval_mode_info {
473     tv_output_mode mode_output;         /**< if != TVO_NATIVE select a special mode */
474     const char *mode_prefix;            /**< if set, this prefix will be printed
475                                              before a value of this mode */
476     const char *mode_suffix;            /**< if set, this suffix will be printed
477                                              after a value of this mode */
478 } tarval_mode_info;
479
480 /**
481  * Specify the output options of one mode.
482  *
483  * This functions stores the modinfo, so DO NOT DESTROY it.
484  *
485  * @param mode          a ir_mode that should be associated
486  * @param modeinfo      the output format info
487  *
488  * @return zero on success.
489  */
490 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo);
491
492 /**
493  * Returns the output options of one mode.
494  *
495  * This functions returns the modinfo of a given mode.
496  *
497  * @param mode          a ir_mode that should be associated
498  *
499  * @return the output option
500  */
501 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode);
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 to a buffer.
586  */
587 int tarval_snprintf(char *buf, size_t buflen, tarval *tv);
588
589 /**
590  * Output of tarvals to stdio.
591  */
592 int tarval_printf(tarval *tv);
593
594 #endif  /* _TV_H_ */