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