Introduce flip-flopping normalisations
[libfirm] / include / libfirm / irmode.h
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Data modes of operations.
23  * @author  Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Mathias Heil,
24  *          Michael Beck
25  * @version $Id$
26  * @brief
27  *  This module specifies the modes that type the firm nodes.  It defines
28  *  a datasturcture that describes a mode and implements constructors and
29  *  access routines to this datastructure. Further it defines a set of
30  *  predefined modes.
31  *
32  *  SEE ALSO:
33  *    UKA tech report 1999-44 for more information about modes.
34  */
35 #ifndef FIRM_IR_IRMODE_H
36 #define FIRM_IR_IRMODE_H
37
38 #include "firm_types.h"
39 #include "begin.h"
40
41 /* ********** Predefined modes ********** */
42
43 /** Helper values for ir_mode_sort. */
44 enum ir_mode_sort_helper {
45         irmsh_is_num   = 0x10, /**< mode represents a number */
46         irmsh_is_data  = 0x20, /**< mode represents data (can be carried in registers) */
47         irmsh_is_datab = 0x40, /**< mode represents data or is internal boolean */
48         irmsh_is_dataM = 0x80, /**< mode represents data or is memory */
49 };
50
51 /**
52  * These values represent the different mode classes of value representations.
53  * Beware: do not change the order of these values without checking
54  * the mode_is
55  */
56 typedef enum ir_mode_sort {
57         /* Predefined sorts of modes */
58         irms_auxiliary        = 0, /**< Only for Firm use. Not extensible. (irm_T) */
59         irms_control_flow     = 1, /**< Marks all control flow modes. Not extensible. (irm_BB, irm_X) */
60         irms_memory           = 2 | irmsh_is_dataM, /**< Marks the memory mode.  Not extensible. (irm_M) */
61
62         /** Internal boolean representation.
63              Storing to memory impossible, convert first. (irm_b) */
64         irms_internal_boolean = 3 | irmsh_is_datab,
65
66         /* user-extensible sorts of modes */
67         /** A mode to represent entities.
68             Restricted int computations can be performed */
69         irms_reference        = 4 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM,
70         /** A mode to represent int numbers.
71             Integer computations can be performed. */
72         irms_int_number       = 5 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM | irmsh_is_num,
73         /** A mode to represent float numbers.
74             Floating point computations can be performed. */
75         irms_float_number     = 6 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM | irmsh_is_num,
76 } ir_mode_sort;
77
78 /** These values represent the different arithmetic operations possible with a mode.
79     Further arithmetics can be defined, e.g., for @@@ modes.
80  */
81 typedef enum ir_mode_arithmetic {
82         irma_uninitialized = 0,
83         irma_none = 1,              /**< For modes for which no representation is specified.
84                                          These are modes of sort auxiliary, internal_boolean and character. */
85         irma_twos_complement = 2,   /**< Values of the mode are represented as two's complement.
86                                      Only legal for modes of sort int_number and reference. */
87         irma_ones_complement,       /**< Values of the mode are represented  as one's complement.
88                                          Only legal for modes of sort int_number and reference. */
89         irma_int_BCD,               /**< Values of the mode are represented as binary coded decimals.
90                                          Only legal for modes of sort int_number and reference. */
91         irma_ieee754 = 256,         /**< Values of the mode are represented according to ieee754
92                                      floating point standard.  Only legal for modes of sort float_number. */
93         irma_float_BCD,             /**< Values of the mode are represented  as binary coded decimals
94                                          according to @@@ which standards??? Only legal for modes of
95                                          sort float_number. */
96         irma_max
97 } ir_mode_arithmetic;
98
99 /** Returns the name of the arithmetic type. */
100 FIRM_API const char *get_mode_arithmetic_name(ir_mode_arithmetic ari);
101
102 /* ********** Constructor for user defined modes **************** */
103 /**
104  * Creates a new mode.
105  *
106  * @param name          the name of the mode to be created
107  * @param sort          the ir_mode_sort of the mode to be created
108  * @param bit_size      number of bits this mode allocate
109  * @param sign          non-zero if this is a signed mode
110  * @param arithmetic    arithmetic operations possible with a mode
111  * @param modulo_shift  Is ignored for modes other than integer.
112  *
113  * This function constructs a new mode given by the parameters.
114  * If the parameters match an already defined mode, this mode is returned
115  * (including the default modes).
116  * If the mode is newly allocated, a new unique mode_code is chosen.
117  * Also, special value tarvals will be calculated such as null,
118  * min, max and can be retrieved using the get_mode_* functions
119  *
120  * @return
121  *   The new mode or NULL on error.
122  *
123  * @note
124  *   It is allowed to construct the default modes. So, a call
125  *   new_ir_mode("Is", irms_int_number, 32, 1, irma_twos_complement, 32) will return mode_Is.
126  */
127 FIRM_API ir_mode *new_ir_mode(const char *name, ir_mode_sort sort, int bit_size,
128                               int sign, ir_mode_arithmetic arithmetic,
129                               unsigned int modulo_shift);
130
131 /**
132  * Creates a new vector mode.
133  *
134  * @param name          the name of the mode to be created
135  * @param sort          the ir_mode_sort of the mode to be created
136  * @param bit_size      number of bits for one element of this mode
137  * @param num_of_elem   number of elements in this vector mode
138  * @param sign          non-zero if this is a signed mode
139  * @param arithmetic    arithmetic operations possible with a mode
140  * @param modulo_shift  Is ignored for modes other than integer.
141  *
142  * This function constructs a new vector mode given by the parameters.
143  * If the parameters match an already defined mode, this mode is returned.
144  * If the mode is newly allocated, a new unique mode_code is chosen.
145  * Also, special value tarvals will be calculated such as null,
146  * min, max and can be retrieved using the get_mode_* functions
147  *
148  * @return
149  *   The new mode or NULL on error.
150  */
151 FIRM_API ir_mode *new_ir_vector_mode(const char *name, ir_mode_sort sort,
152                                      int bit_size, unsigned num_of_elem,
153                                      int sign, ir_mode_arithmetic arithmetic,
154                                      unsigned int modulo_shift);
155
156 /**
157  * Checks whether a pointer points to a mode.
158  *
159  * @param thing     an arbitrary pointer
160  *
161  * @return
162  *     true if the thing is a mode, else false
163  */
164 FIRM_API int is_mode(const void *thing);
165
166 /* ********** Access methods to read mode information *********** */
167
168 /** Returns the ident* of the mode */
169 FIRM_API ident *get_mode_ident(const ir_mode *mode);
170
171 /** Returns the null-terminated name of this mode. */
172 FIRM_API const char *get_mode_name(const ir_mode *mode);
173
174 /** Returns a coarse classification of the mode. */
175 FIRM_API ir_mode_sort get_mode_sort(const ir_mode *mode);
176
177 /** Returns the size of values of the mode in bits. */
178 FIRM_API unsigned get_mode_size_bits(const ir_mode *mode);
179
180 /** Returns the size of values of the mode in bytes.
181  *  If the size is not dividable by 8 returns -1. */
182 FIRM_API unsigned get_mode_size_bytes(const ir_mode *mode);
183
184 /** Returns the signess of a mode.
185  *
186  * Returns the signess of a mode: 1 if mode is signed. */
187 FIRM_API int get_mode_sign(const ir_mode *mode);
188
189 /** Returns the arithmetic of a mode */
190 FIRM_API ir_mode_arithmetic get_mode_arithmetic(const ir_mode *mode);
191
192 /** Get the modulo shift attribute.
193  *
194  *  Attribute modulo shift specifies for modes of kind irms_int_number
195  *  whether shift applies modulo to value of bits to shift.  Zero for
196  *  modes that are not integer.
197  */
198 FIRM_API unsigned int get_mode_modulo_shift(const ir_mode *mode);
199
200 /** Return the number of vector elements.
201  *
202  *  Attribute vector_elem specifies the number of vector elements of
203  *  a vector mode. For non-vector modes it returns 1 for data and 0
204  *  for all other modes
205  */
206 FIRM_API unsigned int get_mode_n_vector_elems(const ir_mode *mode);
207
208 /** Returns the stored intermediate information. */
209 FIRM_API void *get_mode_link(const ir_mode *mode);
210
211 /** Stores new intermediate information. */
212 FIRM_API void set_mode_link(ir_mode *mode, void *l);
213
214 /**
215  * Returns the smallest representable value of a given mode.
216  *
217  * For modes of the sort float_number this is the most negative value
218  * bigger than -infinite.
219  */
220 FIRM_API ir_tarval *get_mode_min(ir_mode *mode);
221
222 /**
223  * Returns the biggest representable value o f a given mode.
224  *
225  * For modes of the sort float_number this is the largest value lower
226  * than infinite.
227  */
228 FIRM_API ir_tarval *get_mode_max(ir_mode *mode);
229
230 /**
231  * Returns the value Zero represented in this mode.
232  *
233  * Zero is the additive neutral element and as such
234  * is defined only for modes allowing addition, i.e.
235  * op_pin_state_floats and ints, and references (NULL-Pointer)
236  * else returns tarval_bad.
237  */
238 FIRM_API ir_tarval *get_mode_null(ir_mode *mode);
239
240 /**
241  * Returns the value One, represented in this mode.
242  *
243  * One, being the multiplicative neutral element,
244  * is defined only for modes allowing multiplication,
245  * i.e. ints and floats.
246  */
247 FIRM_API ir_tarval *get_mode_one(ir_mode *mode);
248
249 /**
250  * Returns the value Minus One, represented in this mode.
251  *
252  * Minus One is defined only for modes allowing
253  * multiplication with signed values, i.e. signed ints and floats.
254  */
255 FIRM_API ir_tarval *get_mode_minus_one(ir_mode *mode);
256
257 /**
258  * Returns the value where all bits are One, represented in this mode.
259  *
260  * All One is defined only for modes integer, reference and boolean modes
261  */
262 FIRM_API ir_tarval *get_mode_all_one(ir_mode *mode);
263
264 /**
265  * Returns the positive infinite value of a mode.
266  *
267  * This is only valid for float_numbers, other modes
268  * will result in tarval_bad.
269  */
270 FIRM_API ir_tarval *get_mode_infinite(ir_mode *mode);
271
272 /**
273  * Returns the NAN value of a given mode.
274  *
275  * This is only valid for float_numbers, other modes
276  * will result in tarval_bad.
277  */
278 FIRM_API ir_tarval *get_mode_NAN(ir_mode *mode);
279
280 FIRM_API ir_mode *mode_M; /**< memory */
281
282 /* -- A set of predefined, numerical modes according to Techreport 1999-44 -- */
283 FIRM_API ir_mode *mode_F;   /**< signed float(32) */
284 FIRM_API ir_mode *mode_D;   /**< signed double(64) */
285 FIRM_API ir_mode *mode_E;   /**< signed extended(80) */
286 FIRM_API ir_mode *mode_Bs;  /**< signed byte (former char) */
287 FIRM_API ir_mode *mode_Bu;  /**< unsigned byte (former char) */
288 FIRM_API ir_mode *mode_Hs;  /**< signed short integer */
289 FIRM_API ir_mode *mode_Hu;  /**< unsigned short integer */
290 FIRM_API ir_mode *mode_Is;  /**< signed integer */
291 FIRM_API ir_mode *mode_Iu;  /**< unsigned integer */
292 FIRM_API ir_mode *mode_Ls;  /**< signed long integer */
293 FIRM_API ir_mode *mode_Lu;  /**< unsigned long integer */
294 FIRM_API ir_mode *mode_LLs; /**< signed long long integer */
295 FIRM_API ir_mode *mode_LLu; /**< unsigned long long integer */
296
297 FIRM_API ir_mode *mode_P;   /**< pointer */
298 FIRM_API ir_mode *mode_P_code; /**< A pointer mode that is set by the client of libfirm.  This mode
299                                   represents the pointer size of the target machine code addresses. Is initialized
300                                   to mode_P. */
301 FIRM_API ir_mode *mode_P_data; /**< A pointer mode that is set by the client of libfirm.  This mode
302                                   represents the pointer size of the target machine data addresses. Is initialized
303                                   to mode_P. */
304
305 /* -- Auxiliary modes necessary for the Firm representation -- */
306 FIRM_API ir_mode *mode_b;  /**< internal boolean */
307
308 FIRM_API ir_mode *mode_X;  /**< execution */
309 FIRM_API ir_mode *mode_BB; /**< block */
310
311 FIRM_API ir_mode *mode_T;  /**< tuple (none) */
312 FIRM_API ir_mode *mode_ANY;/**< undefined mode */
313 FIRM_API ir_mode *mode_BAD;/**< bad mode */
314
315 /*@{*/
316 FIRM_API ir_mode *get_modeF(void);
317 FIRM_API ir_mode *get_modeD(void);
318 FIRM_API ir_mode *get_modeE(void);
319 FIRM_API ir_mode *get_modeBs(void);
320 FIRM_API ir_mode *get_modeBu(void);
321 FIRM_API ir_mode *get_modeHs(void);
322 FIRM_API ir_mode *get_modeHu(void);
323 FIRM_API ir_mode *get_modeIs(void);
324 FIRM_API ir_mode *get_modeIu(void);
325 FIRM_API ir_mode *get_modeLs(void);
326 FIRM_API ir_mode *get_modeLu(void);
327 FIRM_API ir_mode *get_modeLLs(void);
328 FIRM_API ir_mode *get_modeLLu(void);
329 FIRM_API ir_mode *get_modeP(void);
330 FIRM_API ir_mode *get_modeb(void);
331 FIRM_API ir_mode *get_modeX(void);
332 FIRM_API ir_mode *get_modeBB(void);
333 FIRM_API ir_mode *get_modeM(void);
334 FIRM_API ir_mode *get_modeT(void);
335 FIRM_API ir_mode *get_modeANY(void);
336 FIRM_API ir_mode *get_modeBAD(void);
337
338 /** Returns the machine specific pointer mode for code addresses. */
339 FIRM_API ir_mode *get_modeP_code(void);
340
341 /** Returns the machine specific pointer mode for data addresses. */
342 FIRM_API ir_mode *get_modeP_data(void);
343
344 /**
345  * Sets the machine specific pointer mode for code addresses.
346  * If not set, the predefined mode mode_P will be used.
347  */
348 FIRM_API void set_modeP_code(ir_mode *p);
349
350 /**
351  * Sets the machine specific pointer mode for data addresses.
352  * If not set, the predefined mode mode_P will be used.
353  */
354 FIRM_API void set_modeP_data(ir_mode *p);
355
356 /*@{*/
357 /**
358    Functions to check, whether a mode is signed, float, int, character,
359    reference, num, data, datab or dataM.
360
361    For more exact definitions read the corresponding pages
362    in the firm documentation or the following enumeration
363
364    The set of "float" is defined as:
365    float = {irm_F, irm_D, irm_E}
366
367    The set of "int" is defined as:
368    int   = {irm_Bs, irm_Bu, irm_Hs, irm_Hu, irm_Is, irm_Iu, irm_Ls, irm_Lu}
369
370    The set of "reference" is defined as:
371    reference  = {irm_P}
372
373    The set of "num" is defined as:
374    num   = {float || int}
375
376    The set of "data" is defined as:
377    data  =  {num || reference}
378
379    The set of "datab" is defined as:
380    datab =  {data || irm_b }
381
382    The set of "dataM" is defined as:
383    dataM =  {data || irm_M}
384
385    Vector "int" and "float" are defined by the arithmetic and vector_elem > 1.
386 */
387 /* Test for a certain class of modes. */
388 FIRM_API int mode_is_signed (const ir_mode *mode);
389 FIRM_API int mode_is_float (const ir_mode *mode);
390 FIRM_API int mode_is_int (const ir_mode *mode);
391 FIRM_API int mode_is_reference (const ir_mode *mode);
392 FIRM_API int mode_is_num (const ir_mode *mode);
393 FIRM_API int mode_is_data (const ir_mode *mode);
394 FIRM_API int mode_is_datab (const ir_mode *mode);
395 FIRM_API int mode_is_dataM (const ir_mode *mode);
396 FIRM_API int mode_is_float_vector (const ir_mode *mode);
397 FIRM_API int mode_is_int_vector (const ir_mode *mode);
398 /*@}*/
399
400 /**
401  * Returns true if sm can be converted to lm without loss
402  * according to firm definition.
403  *
404  * Note that mode_Iu is NOT smaller than mode_Is here.
405  *
406  * @see values_in_mode()
407  */
408 FIRM_API int smaller_mode(const ir_mode *sm, const ir_mode *lm);
409
410 /**
411  * Returns true if a value of mode sm can be converted into mode lm
412  * and backwards without loss.
413  *
414  * Note that mode_Iu values CAN be converted in mode_Is and back.
415  *
416  * @see smaller_mode()
417  */
418 FIRM_API int values_in_mode(const ir_mode *sm, const ir_mode *lm);
419
420 /**
421  * Returns a matching unsigned mode for a given integer signed mode.
422  * Returns NULL if no matching mode exists.
423  */
424 FIRM_API ir_mode *find_unsigned_mode(const ir_mode *mode);
425
426 /**
427  * Returns a matching signed mode for a given integer unsigned mode.
428  * Returns NULL if no matching mode exists.
429  */
430 FIRM_API ir_mode *find_signed_mode(const ir_mode *mode);
431
432 /**
433  * Returns an integer mode with 2*n bits for a given integer mode with n bits.
434  * Returns NULL if no matching mode exists.
435  */
436 FIRM_API ir_mode *find_double_bits_int_mode(const ir_mode *mode);
437
438 /**
439  * Returns non-zero if the given mode honors signed zero's, i.e.,
440  * a +0 and a -0 exists and handled differently.
441  */
442 FIRM_API int mode_honor_signed_zeros(const ir_mode *mode);
443
444 /**
445  * Returns non-zero if the given mode might overflow on unary Minus.
446  */
447 FIRM_API int mode_overflow_on_unary_Minus(const ir_mode *mode);
448
449 /**
450  * Returns non-zero if the mode has a reversed wrap-around
451  * logic, especially (a + x) - x == a.
452  * This is normally true for integer modes, not for floating
453  * point modes.
454  */
455 FIRM_API int mode_wrap_around(const ir_mode *mode);
456
457 /**
458  * Return the signed integer equivalent mode for an reference mode.
459  */
460 FIRM_API ir_mode *get_reference_mode_signed_eq(ir_mode *mode);
461
462 /**
463  * Sets the signed integer equivalent mode for an reference mode.
464  */
465 FIRM_API void set_reference_mode_signed_eq(ir_mode *ref_mode, ir_mode *int_mode);
466
467 /**
468  * Return the unsigned integer equivalent mode for an reference mode.
469  */
470 FIRM_API ir_mode *get_reference_mode_unsigned_eq(ir_mode *mode);
471
472 /**
473  * Sets the unsigned integer equivalent mode for an reference mode.
474  */
475 FIRM_API void set_reference_mode_unsigned_eq(ir_mode *ref_mode, ir_mode *int_mode);
476
477 /**
478  * Returns non-zero if the cast from mode src to mode dst is a
479  * reinterpret cast (ie. only the bit pattern is reinterpreted,
480  * no conversion is done)
481  */
482 FIRM_API int is_reinterpret_cast(const ir_mode *src, const ir_mode *dst);
483
484 /**
485  * Returns the primitive type matching the given mode
486  */
487 FIRM_API ir_type *get_type_for_mode(const ir_mode *mode);
488
489 #include "end.h"
490
491 #endif