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