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