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