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