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