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