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