a6e7b69fc5799a43ed0f805d8f29a836eb73587b
[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
40 /* ********** Predefined modes ********** */
41
42 /** Helper values for ir_mode_sort. */
43 enum ir_mode_sort_helper {
44         irmsh_is_num   = 0x10, /**< mode represents a number */
45         irmsh_is_data  = 0x20, /**< mode represents data (can be carried in registers) */
46         irmsh_is_datab = 0x40, /**< mode represents data or is internal boolean */
47         irmsh_is_dataM = 0x80, /**< mode represents data or is memory */
48 };
49
50 /**
51  * These values represent the different mode classes of value representations.
52  * Beware: do not change the order of these values without checking
53  * the mode_is
54  */
55 typedef enum ir_mode_sort {
56         /* Predefined sorts of modes */
57         irms_auxiliary        = 0, /**< Only for Firm use. Not extensible. (irm_T) */
58         irms_control_flow     = 1, /**< Marks all control flow modes. Not extensible. (irm_BB, irm_X) */
59         irms_memory           = 2 | irmsh_is_dataM, /**< Marks the memory mode.  Not extensible. (irm_M) */
60
61         /** Internal boolean representation.
62              Storing to memory impossible, convert first. (irm_b) */
63         irms_internal_boolean = 3 | irmsh_is_datab,
64
65         /* user-extensible sorts of modes */
66         /** A mode to represent entities.
67             Restricted int computations can be performed */
68         irms_reference        = 4 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM,
69         /** A mode to represent int numbers.
70             Integer computations can be performed. */
71         irms_int_number       = 5 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM | irmsh_is_num,
72         /** A mode to represent float numbers.
73             Floating point computations can be performed. */
74         irms_float_number     = 6 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM | irmsh_is_num,
75 } ir_mode_sort;
76
77 /** These values represent the different arithmetic operations possible with a mode.
78     Further arithmetics can be defined, e.g., for @@@ modes.
79  */
80 typedef enum ir_mode_arithmetic {
81         irma_uninitialized = 0,
82         irma_none = 1,              /**< For modes for which no representation is specified.
83                                          These are modes of sort auxiliary, internal_boolean and character. */
84         irma_twos_complement = 2,   /**< Values of the mode are represented as two's complement.
85                                      Only legal for modes of sort int_number and reference. */
86         irma_ones_complement,       /**< Values of the mode are represented  as one's complement.
87                                          Only legal for modes of sort int_number and reference. */
88         irma_int_BCD,               /**< Values of the mode are represented as binary coded decimals.
89                                          Only legal for modes of sort int_number and reference. */
90         irma_ieee754 = 256,         /**< Values of the mode are represented according to ieee754
91                                      floating point standard.  Only legal for modes of sort float_number. */
92         irma_float_BCD,             /**< Values of the mode are represented  as binary coded decimals
93                                          according to @@@ which standards??? Only legal for modes of
94                                          sort float_number. */
95         irma_max
96 } ir_mode_arithmetic;
97
98 /** Returns the name of the arithmetic type. */
99 const char *get_mode_arithmetic_name(ir_mode_arithmetic ari);
100
101 /* ********** Constructor for user defined modes **************** */
102 /**
103  * Creates a new mode.
104  *
105  * @param name          the name of the mode to be created
106  * @param sort          the ir_mode_sort of the mode to be created
107  * @param bit_size      number of bits this mode allocate
108  * @param sign          non-zero if this is a signed mode
109  * @param arithmetic    arithmetic operations possible with a mode
110  * @param modulo_shift  Is ignored for modes other than integer.
111  *
112  * This function constructs a new mode given by the parameters.
113  * If the parameters match an already defined mode, this mode is returned
114  * (including the default modes).
115  * If the mode is newly allocated, a new unique mode_code is chosen.
116  * Also, special value tarvals will be calculated such as null,
117  * min, max and can be retrieved using the get_mode_* functions
118  *
119  * @return
120  *   The new mode or NULL on error.
121  *
122  * @note
123  *   It is allowed to construct the default modes. So, a call
124  *   new_ir_mode("Is", irms_int_number, 32, 1, irma_twos_complement, 32) will return mode_Is.
125  */
126 ir_mode *new_ir_mode(const char *name, ir_mode_sort sort, int bit_size, int sign,
127                      ir_mode_arithmetic arithmetic, unsigned int modulo_shift);
128
129 /**
130  * Creates a new vector mode.
131  *
132  * @param name          the name of the mode to be created
133  * @param sort          the ir_mode_sort of the mode to be created
134  * @param bit_size      number of bits for one element of this mode
135  * @param num_of_elem   number of elements in this vector mode
136  * @param sign          non-zero if this is a signed mode
137  * @param arithmetic    arithmetic operations possible with a mode
138  * @param modulo_shift  Is ignored for modes other than integer.
139  *
140  * This function constructs a new vector mode given by the parameters.
141  * If the parameters match an already defined mode, this mode is returned.
142  * If the mode is newly allocated, a new unique mode_code is chosen.
143  * Also, special value tarvals will be calculated such as null,
144  * min, max and can be retrieved using the get_mode_* functions
145  *
146  * @return
147  *   The new mode or NULL on error.
148  */
149 ir_mode *new_ir_vector_mode(const char *name, ir_mode_sort sort, int bit_size, unsigned num_of_elem, int sign,
150                             ir_mode_arithmetic arithmetic, unsigned int modulo_shift);
151
152 /**
153  * Checks whether a pointer points to a mode.
154  *
155  * @param thing     an arbitrary pointer
156  *
157  * @return
158  *     true if the thing is a mode, else false
159  */
160 int is_mode(const void *thing);
161
162 /* ********** Access methods to read mode information *********** */
163
164 /** Returns the ident* of the mode */
165 ident      *get_mode_ident(const ir_mode *mode);
166
167 /** Returns the null-terminated name of this mode. */
168 const char *get_mode_name(const ir_mode *mode);
169
170 /** Returns a coarse classification of the mode. */
171 ir_mode_sort get_mode_sort(const ir_mode *mode);
172
173 /** Returns the size of values of the mode in bits. */
174 unsigned get_mode_size_bits(const ir_mode *mode);
175
176 /** Returns the size of values of the mode in bytes.
177  *  If the size is not dividable by 8 returns -1. */
178 unsigned get_mode_size_bytes(const ir_mode *mode);
179
180 /** Returns the signess of a mode.
181  *
182  * Returns the signess of a mode: 1 if mode is signed. */
183 int get_mode_sign(const ir_mode *mode);
184
185 /** Returns the arithmetic of a mode */
186 ir_mode_arithmetic get_mode_arithmetic(const ir_mode *mode);
187
188 /** Get the modulo shift attribute.
189  *
190  *  Attribute modulo shift specifies for modes of kind irms_int_number
191  *  whether shift applies modulo to value of bits to shift.  Zero for
192  *  modes that are not integer.
193  */
194 unsigned int get_mode_modulo_shift(const ir_mode *mode);
195
196 /** Return the number of vector elements.
197  *
198  *  Attribute vector_elem specifies the number of vector elements of
199  *  a vector mode. For non-vector modes it returns 1 for data and 0
200  *  for all other modes
201  */
202 unsigned int get_mode_n_vector_elems(const ir_mode *mode);
203
204 /** Returns the stored intermediate information. */
205 void *get_mode_link(const ir_mode *mode);
206
207 /** Stores new intermediate information. */
208 void  set_mode_link(ir_mode *mode, void *l);
209
210 /**
211  * Returns the smallest representable value of a given mode.
212  *
213  * For modes of the sort float_number this is the most negative value
214  * bigger than -infinite.
215  */
216 tarval *get_mode_min(ir_mode *mode);
217
218 /**
219  * Returns the biggest representable value o f a given mode.
220  *
221  * For modes of the sort float_number this is the largest value lower
222  * than infinite.
223  */
224 tarval *get_mode_max(ir_mode *mode);
225
226 /**
227  * Returns the value Zero represented in this mode.
228  *
229  * Zero is the additive neutral element and as such
230  * is defined only for modes allowing addition, i.e.
231  * op_pin_state_floats and ints, and references (NULL-Pointer)
232  * else returns tarval_bad.
233  */
234 tarval *get_mode_null(ir_mode *mode);
235
236 /**
237  * Returns the value One, represented in this mode.
238  *
239  * One, being the multiplicative neutral element,
240  * is defined only for modes allowing multiplication,
241  * i.e. ints and floats.
242  */
243 tarval *get_mode_one(ir_mode *mode);
244
245 /**
246  * Returns the value Minus One, represented in this mode.
247  *
248  * Minus One is defined only for modes allowing
249  * multiplication with signed values, i.e. signed ints and floats.
250  */
251 tarval *get_mode_minus_one(ir_mode *mode);
252
253 /**
254  * Returns the value where all bits are One, represented in this mode.
255  *
256  * All One is defined only for modes integer, reference and boolean modes
257  */
258 tarval *get_mode_all_one(ir_mode *mode);
259
260 /**
261  * Returns the positive infinite value of a mode.
262  *
263  * This is only valid for float_numbers, other modes
264  * will result in tarval_bad.
265  */
266 tarval *get_mode_infinite(ir_mode *mode);
267
268 /**
269  * Returns the NAN value of a given mode.
270  *
271  * This is only valid for float_numbers, other modes
272  * will result in tarval_bad.
273  */
274 tarval *get_mode_NAN(ir_mode *mode);
275
276 extern ir_mode *mode_M;  /**< memory */
277
278 /* -- A set of predefined, numerical modes according to Techreport 1999-44 -- */
279 extern ir_mode *mode_F;   /**< signed float(32) */
280 extern ir_mode *mode_D;   /**< signed double(64) */
281 extern ir_mode *mode_E;   /**< signed extended(80) */
282 extern ir_mode *mode_Bs;  /**< signed byte (former char) */
283 extern ir_mode *mode_Bu;  /**< unsigned byte (former char) */
284 extern ir_mode *mode_Hs;  /**< signed short integer */
285 extern ir_mode *mode_Hu;  /**< unsigned short integer */
286 extern ir_mode *mode_Is;  /**< signed integer */
287 extern ir_mode *mode_Iu;  /**< unsigned integer */
288 extern ir_mode *mode_Ls;  /**< signed long integer */
289 extern ir_mode *mode_Lu;  /**< unsigned long integer */
290 extern ir_mode *mode_LLs; /**< signed long long integer */
291 extern ir_mode *mode_LLu; /**< unsigned long long integer */
292
293 extern ir_mode *mode_P;   /**< pointer */
294 extern ir_mode *mode_P_code; /**< A pointer mode that is set by the client of libfirm.  This mode
295                                   represents the pointer size of the target machine code addresses. Is initialized
296                                   to mode_P. */
297 extern ir_mode *mode_P_data; /**< A pointer mode that is set by the client of libfirm.  This mode
298                                   represents the pointer size of the target machine data addresses. Is initialized
299                                   to mode_P. */
300
301 /* -- Auxiliary modes necessary for the Firm representation -- */
302 extern ir_mode *mode_b;  /**< internal boolean */
303
304 extern ir_mode *mode_X;  /**< execution */
305 extern ir_mode *mode_BB; /**< block */
306
307 extern ir_mode *mode_T;  /**< tuple (none) */
308 extern ir_mode *mode_ANY;/**< undefined mode */
309 extern ir_mode *mode_BAD;/**< bad mode */
310
311 /*@{*/
312 /** Access routines for JNI Interface */
313 ir_mode *get_modeF(void);
314 ir_mode *get_modeD(void);
315 ir_mode *get_modeE(void);
316 ir_mode *get_modeBs(void);
317 ir_mode *get_modeBu(void);
318 ir_mode *get_modeHs(void);
319 ir_mode *get_modeHu(void);
320 ir_mode *get_modeIs(void);
321 ir_mode *get_modeIu(void);
322 ir_mode *get_modeLs(void);
323 ir_mode *get_modeLu(void);
324 ir_mode *get_modeLLs(void);
325 ir_mode *get_modeLLu(void);
326 ir_mode *get_modeP(void);
327 ir_mode *get_modeb(void);
328 ir_mode *get_modeX(void);
329 ir_mode *get_modeBB(void);
330 ir_mode *get_modeM(void);
331 ir_mode *get_modeT(void);
332 ir_mode *get_modeANY(void);
333 ir_mode *get_modeBAD(void);
334
335 /** Returns the machine specific pointer mode for code addresses. */
336 ir_mode *get_modeP_code(void);
337
338 /** Returns the machine specific pointer mode for data addresses. */
339 ir_mode *get_modeP_data(void);
340
341 /**
342  * Sets the machine specific pointer mode for code addresses.
343  * If not set, the predefined mode mode_P will be used.
344  */
345 void set_modeP_code(ir_mode *p);
346
347 /**
348  * Sets the machine specific pointer mode for data addresses.
349  * If not set, the predefined mode mode_P will be used.
350  */
351 void set_modeP_data(ir_mode *p);
352
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 /* Test for a certain class of modes. */
385 int mode_is_signed (const ir_mode *mode);
386 int mode_is_float (const ir_mode *mode);
387 int mode_is_int (const ir_mode *mode);
388 int mode_is_reference (const ir_mode *mode);
389 int mode_is_num (const ir_mode *mode);
390 int mode_is_data (const ir_mode *mode);
391 int mode_is_datab (const ir_mode *mode);
392 int mode_is_dataM (const ir_mode *mode);
393 int mode_is_float_vector (const ir_mode *mode);
394 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 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 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 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 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 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 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 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 int mode_wrap_around(const ir_mode *mode);
453
454 /**
455  * Return the signed integer equivalent mode for an reference mode.
456  */
457 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 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 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 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 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 ir_type *get_type_for_mode(const ir_mode *mode);
485
486 #endif