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