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