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