56c5c1f579fce1f5577704c71959794770b29b47
[libfirm] / ir / ir / irmode.h
1 /*
2  * Copyright (C) 1995-2007 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  * Project:     libFIRM
22  * File name:   ir/ir/irmode.h
23  * Purpose:     Data modes of operations.
24  * Author:      Martin Trapp, Christian Schaefer
25  * Modified by: Goetz Lindenmaier, Mathias Heil, Michael Beck
26  * Created:
27  * CVS-ID:      $Id$
28  * Copyright:   (c) 1998-2007 Universität Karlsruhe
29  */
30
31 /**
32  * @file irmode.h
33  *    irmode -- Modes for ir operators
34  *
35  * @author Christian Schaefer, Matthias Heil
36  *
37  * This module specifies the modes that type the firm nodes.  It defines
38  * a datasturcture that describes a mode and implements constructors and
39  * access routines to this datastructure. Further it defines a set of
40  * predefined modes.
41  *
42  * SEE ALSO:
43  *    UKA tech report 1999-44 for more information about modes.
44  *
45  */
46
47 #ifndef _FIRM_IR_IRMODE_H_
48 #define _FIRM_IR_IRMODE_H_
49
50 #include "firm_types.h"
51 #include "ident.h"
52
53 /**
54  * Contains relevant information about a mode.
55  *
56  * Necessary information about a mode is stored in this struct
57  * which is used by the tarval module to perform calculations
58  * and comparisons of values of a such described mode.
59  *
60  * ATTRIBUTES:
61  *  -  modecode code:           An unambiguous int (enum) for the mode
62  *  -  ident *name:             Name of this mode. Two modes are different if the name is different.
63  *  -  mode_sort sort:          sort of mode specifying possible usage categories
64  *  -  int    size:             size of the mode in Bits.
65  *  -  unsigned sign:1:         signedness of this mode
66  *  -  ... more to come
67  *  -  modulo_shift             specifies for modes of kind irms_int_number
68  *                              whether shift applies modulo to value of bits to shift
69  *
70  * SEE ALSO:
71  *    The tech report 1999-44 describing FIRM and predefined modes
72  *    tarval.h
73  */
74 #ifndef _IR_MODE_TYPEDEF_
75 #define _IR_MODE_TYPEDEF_
76 typedef struct ir_mode ir_mode;
77 #endif
78
79 /* ********** Predefined modes ********** */
80
81 /**
82  * Predefined mode according to tech report 1999-14.
83  */
84 typedef enum { /* irm is short for `ir mode' */
85         irm_BB,                       /**< basic block */
86         irm_X,                        /**< execution */
87         irm_F,                        /**< float(32) */
88         irm_D,                        /**< double(64) */
89         irm_E,                        /**< extended(80) */
90         irm_Bs,                       /**< signed byte(8) */
91         irm_Bu,                       /**< unsigned byte(8) */
92         irm_Hs,                       /**< signed short(16) */
93         irm_Hu,                       /**< unsigned short(16) */
94         irm_Is,                       /**< signed int(32) */
95         irm_Iu,                       /**< unsigned int(32) */
96         irm_Ls,                       /**< signed long(64) */
97         irm_Lu,                       /**< unsigned long(64) */
98         irm_LLs,                      /**< signed long long(128) */
99         irm_LLu,                      /**< unsigned long long(128) */
100         irm_C,                        /**< character */
101         irm_P,                        /**< pointer */
102         irm_b,                        /**< internal boolean */
103         irm_M,                        /**< memory */
104         irm_T,                        /**< tuple */
105         irm_U,                        /**< unicode character */
106         irm_ANY,                      /**< undefined mode */
107         irm_BAD,                      /**< bad mode */
108         irm_max                       /**< maximum value for modecode */
109 } modecode;
110
111 /** These values represent the different mode classes of value representations.
112  */
113 typedef enum {
114         /* Predefined sorts of modes */
115         irms_auxiliary,         /**< Only for Firm use. Not extensible. (irm_T) */
116         irms_control_flow,      /**< Marks all control flow modes. Not extensible. (irm_BB, irm_X) */
117         irms_memory,            /**< Marks the memory mode.  Not extensible. (irm_M) */
118         irms_internal_boolean,  /**< Internal boolean representation.
119                                      Storing to memory impossible, convert first. (irm_b) */
120         /* user-extensible sorts of modes */
121         irms_int_number,        /**< A mode to represent int numbers.
122                                      Integer computations can be performed. */
123         irms_float_number,      /**< A mode to represent float numbers.
124                                      Floating point computations can be performed. */
125         irms_reference,         /**< A mode to represent entities.
126                                      Restricted int computations can be performed */
127         irms_character          /**< A mode to represent characters/symbols
128                                      ?? Are computations allowed? as int?? */
129 } mode_sort;
130
131 /** These values represent the different arithmetic operations possible with a mode.
132     Further arithmetics can be defined, e.g., for @@@ modes.
133  */
134 typedef enum {
135         irma_uninitialized = 0,
136         irma_none = 1,              /**< For modes for which no representation is specified.
137                                          These are modes of sort auxiliary, internal_boolean and character. */
138         irma_twos_complement = 2,   /**< Values of the mode are represented as two's complement.
139                                      Only legal for modes of sort int_number and reference. */
140         irma_ones_complement,       /**< Values of the mode are represented  as one's complement.
141                                          Only legal for modes of sort int_number and reference. */
142         irma_int_BCD,               /**< Values of the mode are represented as binary coded decimals.
143                                          Only legal for modes of sort int_number and reference. */
144         irma_ieee754 = 256,         /**< Values of the mode are represented according to ieee754
145                                      floatingpoint standard.  Only legal for modes of sort float_number. */
146         irma_float_BCD,             /**< Values of the mode are represented  as binary coded decimals
147                                          according to @@@ which standards??? Only legal for modes of
148                                          sort float_number. */
149         irma_max
150 } mode_arithmetic;
151
152
153 /* ********** Constructor for user defined modes **************** */
154 /**
155  * Creates a new mode.
156  *
157  * @param name          the name of the mode to be created
158  * @param sort          the mode_sort of the mode to be created
159  * @param bit_size      number of bits this mode allocate
160  * @param sign          non-zero if this is a signed mode
161  * @param arithmetic    arithmetic operations possible with a mode
162  * @param modulo_shift  Is ignored for modes other than integer.
163  *
164  * This function constructs a new mode given by the parameters.
165  * If the parameters match an already defined mode, this mode is returned
166  * (including the default modes).
167  * If the mode is newly allocated, a new unique mode_code is chosen.
168  * Also, special value tarvals will be calculated such as null,
169  * min, max and can be retrieved using the get_mode_* functions
170  *
171  * @return
172  *   The new mode or NULL on error.
173  *
174  * @note
175  *   It is allowed to construct the default modes. So, a call
176  *   new_ir_mode("Is", irms_int_number, 32, 1, irma_twos_complement, 32) will return mode_Is.
177  */
178 ir_mode *new_ir_mode(const char *name, mode_sort sort, int bit_size, int sign, mode_arithmetic arithmetic, unsigned int modulo_shift);
179
180 /**
181  * Creates a new vector mode.
182  *
183  * @param name          the name of the mode to be created
184  * @param sort          the mode_sort of the mode to be created
185  * @param bit_size      number of bits for one element of this mode
186  * @param num_of_elem   number of elements in this vector mode
187  * @param sign          non-zero if this is a signed mode
188  * @param arithmetic    arithmetic operations possible with a mode
189  * @param modulo_shift  Is ignored for modes other than integer.
190  *
191  * This function constructs a new vector mode given by the parameters.
192  * If the parameters match an already defined mode, this mode is returned.
193  * If the mode is newly allocated, a new unique mode_code is chosen.
194  * Also, special value tarvals will be calculated such as null,
195  * min, max and can be retrieved using the get_mode_* functions
196  *
197  * @return
198  *   The new mode or NULL on error.
199  */
200 ir_mode *new_ir_vector_mode(const char *name, mode_sort sort, int bit_size, unsigned num_of_elem, int sign,
201                             mode_arithmetic arithmetic, unsigned int modulo_shift);
202
203 /**
204  * Checks whether a pointer points to a mode.
205  *
206  * @param thing     an arbitrary pointer
207  *
208  * @return
209  *     true if the thing is a mode, else false
210  */
211 int is_mode(void *thing);
212
213 /* ********** Access methods to read mode information *********** */
214
215 /** Returns the classification of the mode */
216 modecode    get_mode_modecode(const ir_mode *mode);
217
218 /** Returns the ident* of the mode */
219 ident      *get_mode_ident(const ir_mode *mode);
220
221 /** Returns the null-terminated name of this mode. */
222 const char *get_mode_name(const ir_mode *mode);
223
224 /** Returns a coarse classification of the mode. */
225 mode_sort   get_mode_sort(const ir_mode *mode);
226
227 /** Returns the size of values of the mode in bits. */
228 int get_mode_size_bits(const ir_mode *mode);
229
230 /** Returns the size of values of the mode in bytes.
231  *  If the size is not dividable by 8 returns -1. */
232 int get_mode_size_bytes(const ir_mode *mode);
233
234 /** Returns the signess of a mode.
235  *
236  * Returns the signess of a mode: 1 if mode is signed. */
237 int get_mode_sign(const ir_mode *mode);
238
239 /** Returns the arithmetic of a mode */
240 int get_mode_arithmetic(const ir_mode *mode);
241
242 /** Get the modulo shift attribute.
243  *
244  *  Attribute modulo shift specifies for modes of kind irms_int_number
245  *  whether shift applies modulo to value of bits to shift.  Zero for
246  *  modes that are not integer.
247  */
248 unsigned int get_mode_modulo_shift(const ir_mode *mode);
249
250 /** Return the number of vector elements.
251  *
252  *  Attribute vector_elem specifies the number of vector elements of
253  *  a vector mode. For non-vector modes it returns 1 for data and 0
254  *  for all other modes
255  */
256 unsigned int get_mode_n_vector_elems(const ir_mode *mode);
257
258 /** Returns the stored intermediate information. */
259 void *get_mode_link(const ir_mode *mode);
260
261 /** Stores new intermediate information. */
262 void  set_mode_link(ir_mode *mode, void *l);
263
264 /**
265  * Returns the smallest representable value of a given mode.
266  *
267  * For modes of the sort float_number this is the most negative value
268  * bigger than -infinite.
269  */
270 tarval *get_mode_min(ir_mode *mode);
271
272 /**
273  * Returns the biggest representable value o f a given mode.
274  *
275  * For modes of the sort float_number this is the largest value lower
276  * than infinite.
277  */
278 tarval *get_mode_max(ir_mode *mode);
279
280 /**
281  * Returns the value Zero represented in this mode.
282  *
283  * Zero is the additive neutral element and as such
284  * is defined only for modes allowing addition, i.e.
285  * op_pin_state_floats and ints, and references (NULL-Pointer)
286  * else returns tarval_bad.
287  */
288 tarval *get_mode_null(ir_mode *mode);
289
290 /**
291  * Returns the value One, represented in this mode.
292  *
293  * One, being the multiplicative neutral element,
294  * is defined only for modes allowing multiplication,
295  * i.e. ints and floats.
296  */
297 tarval *get_mode_one(ir_mode *mode);
298
299 /**
300  * Returns the value Minus One, represented in this mode.
301  *
302  * Minus One is defined only for modes allowing
303  * multiplication with signed values, i.e. signed ints and floats.
304  */
305 tarval *get_mode_minus_one(ir_mode *mode);
306
307 /**
308  * Returns the positive infinite value of a mode.
309  *
310  * This is only valid for float_numbers, other modes
311  * will result in tarval_bad.
312  */
313 tarval *get_mode_infinite(ir_mode *mode);
314
315 /**
316  * Returns the NAN value of a given mode.
317  *
318  * This is only valid for float_numbers, other modes
319  * will result in tarval_bad.
320  */
321 tarval *get_mode_NAN(ir_mode *mode);
322
323 extern ir_mode *mode_M;  /**< memory */
324
325 /* -- A set of predefined, numerical modes according to Techreport 1999-44 -- */
326 extern ir_mode *mode_F;   /**< signed float(32) */
327 extern ir_mode *mode_D;   /**< signed double(64) */
328 extern ir_mode *mode_E;   /**< signed extended(80) */
329 extern ir_mode *mode_Bs;  /**< signed byte (former char) */
330 extern ir_mode *mode_Bu;  /**< unsigned byte (former char) */
331 extern ir_mode *mode_Hs;  /**< signed short integer */
332 extern ir_mode *mode_Hu;  /**< unsigned short integer */
333 extern ir_mode *mode_Is;  /**< signed integer */
334 extern ir_mode *mode_Iu;  /**< unsigned integer */
335 extern ir_mode *mode_Ls;  /**< signed long integer */
336 extern ir_mode *mode_Lu;  /**< unsigned long integer */
337 extern ir_mode *mode_LLs; /**< signed long long integer */
338 extern ir_mode *mode_LLu; /**< unsigned long long integer */
339
340 extern ir_mode *mode_C;   /**< 8 bit char */
341 extern ir_mode *mode_U;   /**< 16 bit unicode char */
342
343 extern ir_mode *mode_P;   /**< pointer */
344 extern ir_mode *mode_P_code; /**< A pointer mode that is set by the client of libfirm.  This mode
345                                   represents the pointer size of the target machine code addresses. Is initialized
346                                   to mode_P. */
347 extern ir_mode *mode_P_data; /**< A pointer mode that is set by the client of libfirm.  This mode
348                                   represents the pointer size of the target machine data addresses. Is initialized
349                                   to mode_P. */
350
351 /* -- Auxiliary modes necessary for the Firm representation -- */
352 extern ir_mode *mode_b;  /**< internal boolean */
353
354 extern ir_mode *mode_X;  /**< execution */
355 extern ir_mode *mode_BB; /**< block */
356
357 extern ir_mode *mode_T;  /**< tuple (none) */
358 extern ir_mode *mode_ANY;/**< undefined mode */
359 extern ir_mode *mode_BAD;/**< bad mode */
360
361 /*@{*/
362 /** Access routines for JNI Interface */
363 ir_mode *get_modeF(void);
364 ir_mode *get_modeD(void);
365 ir_mode *get_modeE(void);
366 ir_mode *get_modeBs(void);
367 ir_mode *get_modeBu(void);
368 ir_mode *get_modeHs(void);
369 ir_mode *get_modeHu(void);
370 ir_mode *get_modeIs(void);
371 ir_mode *get_modeIu(void);
372 ir_mode *get_modeLs(void);
373 ir_mode *get_modeLu(void);
374 ir_mode *get_modeLLs(void);
375 ir_mode *get_modeLLu(void);
376 ir_mode *get_modeC(void);
377 ir_mode *get_modeU(void);
378 ir_mode *get_modeP(void);
379 ir_mode *get_modeb(void);
380 ir_mode *get_modeX(void);
381 ir_mode *get_modeBB(void);
382 ir_mode *get_modeM(void);
383 ir_mode *get_modeT(void);
384 ir_mode *get_modeANY(void);
385 ir_mode *get_modeBAD(void);
386
387 /** Returns the machine specific pointer mode for code addresses. */
388 ir_mode *get_modeP_code(void);
389
390 /** Returns the machine specific pointer mode for data addresses. */
391 ir_mode *get_modeP_data(void);
392
393 /**
394  * Sets the machine specific pointer mode for code addresses.
395  * If not set, the predefined mode mode_P will be used.
396  */
397 void set_modeP_code(ir_mode *p);
398
399 /**
400  * Sets the machine specific pointer mode for data addresses.
401  * If not set, the predefined mode mode_P will be used.
402  */
403 void set_modeP_data(ir_mode *p);
404
405 /**
406    Functions to check, whether a modecode is signed, float, int, character,
407    reference, num, numP, data, datab or dataM.
408
409    For more exact definitions read the corresponding pages
410    in the firm documentation or the following enumeration
411
412    The set of "float" is defined as:
413    float = {irm_F, irm_D, irm_E}
414
415    The set of "int" is defined as:
416    int   = {irm_Bs, irm_Bu, irm_Hs, irm_Hu, irm_Is, irm_Iu, irm_Ls, irm_Lu}
417
418    The set of "character" is defined as:
419    character  = {irm_C, irm_U}
420
421    The set of "reference" is defined as:
422    reference  = {irm_P}
423
424    The set of "num" is defined as:
425    num   = {float || int}
426
427    The set of "numP" is defined as:
428    numP  =  {float || int || reference}
429
430    The set of "data" is defined as:
431    data  =  {num || character || reference}
432
433    The set of "datab" is defined as:
434    datab =  {data || irm_b }
435
436    The set of "dataM" is defined as:
437    dataM =  {data || irm_M}
438
439    Vector "int" and "float" are defined by the arithmetic and vector_elem > 1.
440 */
441 /*@}*/
442 /* Test for a certain class of modes. */
443 int mode_is_signed (const ir_mode *mode);
444 int mode_is_float (const ir_mode *mode);
445 int mode_is_int (const ir_mode *mode);
446 int mode_is_character (const ir_mode *mode);
447 int mode_is_reference (const ir_mode *mode);
448 int mode_is_num (const ir_mode *mode);
449 int mode_is_numP (const ir_mode *mode);
450 int mode_is_data (const ir_mode *mode);
451 int mode_is_datab (const ir_mode *mode);
452 int mode_is_dataM (const ir_mode *mode);
453 int mode_is_float_vector (const ir_mode *mode);
454 int mode_is_int_vector (const ir_mode *mode);
455
456 /** Returns true if sm can be converted to lm without loss
457    according to firm definiton */
458 int smaller_mode(const ir_mode *sm, const ir_mode *lm);
459
460 /**
461  * Returns a matching unsigned mode for a given integer signed mode.
462  * Returns NULL if no matching mode exists.
463  */
464 ir_mode *find_unsigned_mode(const ir_mode *mode);
465
466 /**
467  * Returns a matching signed mode for a given integer unsigned mode.
468  * Returns NULL if no matching mode exists.
469  */
470 ir_mode *find_signed_mode(const ir_mode *mode);
471
472 /**
473  * Returns an integer mode with 2*n bits for a given integer mode with n bits.
474  * Returns NULL if no matching mode exists.
475  */
476 ir_mode *find_double_bits_int_mode(const ir_mode *mode);
477
478 /**
479  * Returns non-zero if the given mode honors signed zero's, i.e.,
480  * a +0 and a -0 exists and handled differently.
481  */
482 int mode_honor_signed_zeros(const ir_mode *mode);
483
484 /**
485  * Returns non-zero if the given mode might overflow on unary Minus.
486  */
487 int mode_overflow_on_unary_Minus(const ir_mode *mode);
488
489 /**
490  * Returns non-zero if the mode has a reversed wrap-around
491  * logic, especially (a + x) - x == a.
492  * This is normally true for integer modes, not for floating
493  * point modes.
494  */
495 int mode_wrap_around(const ir_mode *mode);
496
497 /**
498  * Return the signed integer equivalent mode for an reference mode.
499  */
500 ir_mode *get_reference_mode_signed_eq(ir_mode *mode);
501
502 /**
503  * Sets the signed integer equivalent mode for an reference mode.
504  */
505 void set_reference_mode_signed_eq(ir_mode *ref_mode, ir_mode *int_mode);
506
507 /**
508  * Return the unsigned integer equivalent mode for an reference mode.
509  */
510 ir_mode *get_reference_mode_unsigned_eq(ir_mode *mode);
511
512 /**
513  * Sets the unsigned integer equivalent mode for an reference mode.
514  */
515 void set_reference_mode_unsigned_eq(ir_mode *ref_mode, ir_mode *int_mode);
516
517 #endif /* _FIRM_IR_IRMODE_H_ */