2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Data modes of operations.
23 * @author Martin Trapp, Christian Schaefer, Goetz Lindenmaier, Mathias Heil,
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
33 * UKA tech report 1999-44 for more information about modes.
35 #ifndef FIRM_IR_IRMODE_H
36 #define FIRM_IR_IRMODE_H
38 #include "firm_types.h"
41 /* ********** Predefined modes ********** */
44 * Predefined mode according to tech report 1999-14.
46 typedef enum ir_modecode { /* 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 */
66 irm_ANY, /**< undefined mode */
67 irm_BAD, /**< bad mode */
68 irm_max /**< maximum value for ir_modecode */
71 /** Helper values for ir_mode_sort. */
72 enum ir_mode_sort_helper {
73 irmsh_is_num = 0x10, /**< mode represents a number */
74 irmsh_is_data = 0x20, /**< mode represents data (can be carried in registers) */
75 irmsh_is_datab = 0x40, /**< mode represents data or is internal boolean */
76 irmsh_is_dataM = 0x80, /**< mode represents data or is memory */
80 * These values represent the different mode classes of value representations.
81 * Beware: do not change the order of these values without checking
84 typedef enum ir_mode_sort {
85 /* Predefined sorts of modes */
86 irms_auxiliary = 0, /**< Only for Firm use. Not extensible. (irm_T) */
87 irms_control_flow = 1, /**< Marks all control flow modes. Not extensible. (irm_BB, irm_X) */
88 irms_memory = 2, /**< Marks the memory mode. Not extensible. (irm_M) */
90 /** Internal boolean representation.
91 Storing to memory impossible, convert first. (irm_b) */
92 irms_internal_boolean = 3 | irmsh_is_datab,
94 /* user-extensible sorts of modes */
95 /** A mode to represent entities.
96 Restricted int computations can be performed */
97 irms_reference = 4 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM,
98 /** A mode to represent int numbers.
99 Integer computations can be performed. */
100 irms_int_number = 5 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM | irmsh_is_num,
101 /** A mode to represent float numbers.
102 Floating point computations can be performed. */
103 irms_float_number = 6 | irmsh_is_data | irmsh_is_datab | irmsh_is_dataM | irmsh_is_num,
106 /** These values represent the different arithmetic operations possible with a mode.
107 Further arithmetics can be defined, e.g., for @@@ modes.
109 typedef enum ir_mode_arithmetic {
110 irma_uninitialized = 0,
111 irma_none = 1, /**< For modes for which no representation is specified.
112 These are modes of sort auxiliary, internal_boolean and character. */
113 irma_twos_complement = 2, /**< Values of the mode are represented as two's complement.
114 Only legal for modes of sort int_number and reference. */
115 irma_ones_complement, /**< Values of the mode are represented as one's complement.
116 Only legal for modes of sort int_number and reference. */
117 irma_int_BCD, /**< Values of the mode are represented as binary coded decimals.
118 Only legal for modes of sort int_number and reference. */
119 irma_ieee754 = 256, /**< Values of the mode are represented according to ieee754
120 floating point standard. Only legal for modes of sort float_number. */
121 irma_float_BCD, /**< Values of the mode are represented as binary coded decimals
122 according to @@@ which standards??? Only legal for modes of
123 sort float_number. */
125 } ir_mode_arithmetic;
128 /* ********** Constructor for user defined modes **************** */
130 * Creates a new mode.
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 this mode allocate
135 * @param sign non-zero if this is a signed mode
136 * @param arithmetic arithmetic operations possible with a mode
137 * @param modulo_shift Is ignored for modes other than integer.
139 * This function constructs a new mode given by the parameters.
140 * If the parameters match an already defined mode, this mode is returned
141 * (including the default modes).
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
147 * The new mode or NULL on error.
150 * It is allowed to construct the default modes. So, a call
151 * new_ir_mode("Is", irms_int_number, 32, 1, irma_twos_complement, 32) will return mode_Is.
153 ir_mode *new_ir_mode(const char *name, ir_mode_sort sort, int bit_size, int sign,
154 ir_mode_arithmetic arithmetic, unsigned int modulo_shift);
157 * Creates a new vector mode.
159 * @param name the name of the mode to be created
160 * @param sort the ir_mode_sort of the mode to be created
161 * @param bit_size number of bits for one element of this mode
162 * @param num_of_elem number of elements in this vector mode
163 * @param sign non-zero if this is a signed mode
164 * @param arithmetic arithmetic operations possible with a mode
165 * @param modulo_shift Is ignored for modes other than integer.
167 * This function constructs a new vector mode given by the parameters.
168 * If the parameters match an already defined mode, this mode is returned.
169 * If the mode is newly allocated, a new unique mode_code is chosen.
170 * Also, special value tarvals will be calculated such as null,
171 * min, max and can be retrieved using the get_mode_* functions
174 * The new mode or NULL on error.
176 ir_mode *new_ir_vector_mode(const char *name, ir_mode_sort sort, int bit_size, unsigned num_of_elem, int sign,
177 ir_mode_arithmetic arithmetic, unsigned int modulo_shift);
180 * Checks whether a pointer points to a mode.
182 * @param thing an arbitrary pointer
185 * true if the thing is a mode, else false
187 int is_mode(void *thing);
189 /* ********** Access methods to read mode information *********** */
191 /** Returns the classification of the mode */
192 ir_modecode get_mode_modecode(const ir_mode *mode);
194 /** Returns the ident* of the mode */
195 ident *get_mode_ident(const ir_mode *mode);
197 /** Returns the null-terminated name of this mode. */
198 const char *get_mode_name(const ir_mode *mode);
200 /** Returns a coarse classification of the mode. */
201 ir_mode_sort get_mode_sort(const ir_mode *mode);
203 /** Returns the size of values of the mode in bits. */
204 unsigned get_mode_size_bits(const ir_mode *mode);
206 /** Returns the size of values of the mode in bytes.
207 * If the size is not dividable by 8 returns -1. */
208 unsigned get_mode_size_bytes(const ir_mode *mode);
210 /** Returns the signess of a mode.
212 * Returns the signess of a mode: 1 if mode is signed. */
213 int get_mode_sign(const ir_mode *mode);
215 /** Returns the arithmetic of a mode */
216 ir_mode_arithmetic get_mode_arithmetic(const ir_mode *mode);
218 /** Get the modulo shift attribute.
220 * Attribute modulo shift specifies for modes of kind irms_int_number
221 * whether shift applies modulo to value of bits to shift. Zero for
222 * modes that are not integer.
224 unsigned int get_mode_modulo_shift(const ir_mode *mode);
226 /** Return the number of vector elements.
228 * Attribute vector_elem specifies the number of vector elements of
229 * a vector mode. For non-vector modes it returns 1 for data and 0
230 * for all other modes
232 unsigned int get_mode_n_vector_elems(const ir_mode *mode);
234 /** Returns the stored intermediate information. */
235 void *get_mode_link(const ir_mode *mode);
237 /** Stores new intermediate information. */
238 void set_mode_link(ir_mode *mode, void *l);
241 * Returns the smallest representable value of a given mode.
243 * For modes of the sort float_number this is the most negative value
244 * bigger than -infinite.
246 tarval *get_mode_min(ir_mode *mode);
249 * Returns the biggest representable value o f a given mode.
251 * For modes of the sort float_number this is the largest value lower
254 tarval *get_mode_max(ir_mode *mode);
257 * Returns the value Zero represented in this mode.
259 * Zero is the additive neutral element and as such
260 * is defined only for modes allowing addition, i.e.
261 * op_pin_state_floats and ints, and references (NULL-Pointer)
262 * else returns tarval_bad.
264 tarval *get_mode_null(ir_mode *mode);
267 * Returns the value One, represented in this mode.
269 * One, being the multiplicative neutral element,
270 * is defined only for modes allowing multiplication,
271 * i.e. ints and floats.
273 tarval *get_mode_one(ir_mode *mode);
276 * Returns the value Minus One, represented in this mode.
278 * Minus One is defined only for modes allowing
279 * multiplication with signed values, i.e. signed ints and floats.
281 tarval *get_mode_minus_one(ir_mode *mode);
284 * Returns the value where all bits are One, represented in this mode.
286 * All One is defined only for modes integer, reference and boolean modes
288 tarval *get_mode_all_one(ir_mode *mode);
291 * Returns the positive infinite value of a mode.
293 * This is only valid for float_numbers, other modes
294 * will result in tarval_bad.
296 tarval *get_mode_infinite(ir_mode *mode);
299 * Returns the NAN value of a given mode.
301 * This is only valid for float_numbers, other modes
302 * will result in tarval_bad.
304 tarval *get_mode_NAN(ir_mode *mode);
306 extern ir_mode *mode_M; /**< memory */
308 /* -- A set of predefined, numerical modes according to Techreport 1999-44 -- */
309 extern ir_mode *mode_F; /**< signed float(32) */
310 extern ir_mode *mode_D; /**< signed double(64) */
311 extern ir_mode *mode_E; /**< signed extended(80) */
312 extern ir_mode *mode_Bs; /**< signed byte (former char) */
313 extern ir_mode *mode_Bu; /**< unsigned byte (former char) */
314 extern ir_mode *mode_Hs; /**< signed short integer */
315 extern ir_mode *mode_Hu; /**< unsigned short integer */
316 extern ir_mode *mode_Is; /**< signed integer */
317 extern ir_mode *mode_Iu; /**< unsigned integer */
318 extern ir_mode *mode_Ls; /**< signed long integer */
319 extern ir_mode *mode_Lu; /**< unsigned long integer */
320 extern ir_mode *mode_LLs; /**< signed long long integer */
321 extern ir_mode *mode_LLu; /**< unsigned long long integer */
323 extern ir_mode *mode_P; /**< pointer */
324 extern ir_mode *mode_P_code; /**< A pointer mode that is set by the client of libfirm. This mode
325 represents the pointer size of the target machine code addresses. Is initialized
327 extern ir_mode *mode_P_data; /**< A pointer mode that is set by the client of libfirm. This mode
328 represents the pointer size of the target machine data addresses. Is initialized
331 /* -- Auxiliary modes necessary for the Firm representation -- */
332 extern ir_mode *mode_b; /**< internal boolean */
334 extern ir_mode *mode_X; /**< execution */
335 extern ir_mode *mode_BB; /**< block */
337 extern ir_mode *mode_T; /**< tuple (none) */
338 extern ir_mode *mode_ANY;/**< undefined mode */
339 extern ir_mode *mode_BAD;/**< bad mode */
342 /** Access routines for JNI Interface */
343 ir_mode *get_modeF(void);
344 ir_mode *get_modeD(void);
345 ir_mode *get_modeE(void);
346 ir_mode *get_modeBs(void);
347 ir_mode *get_modeBu(void);
348 ir_mode *get_modeHs(void);
349 ir_mode *get_modeHu(void);
350 ir_mode *get_modeIs(void);
351 ir_mode *get_modeIu(void);
352 ir_mode *get_modeLs(void);
353 ir_mode *get_modeLu(void);
354 ir_mode *get_modeLLs(void);
355 ir_mode *get_modeLLu(void);
356 ir_mode *get_modeP(void);
357 ir_mode *get_modeb(void);
358 ir_mode *get_modeX(void);
359 ir_mode *get_modeBB(void);
360 ir_mode *get_modeM(void);
361 ir_mode *get_modeT(void);
362 ir_mode *get_modeANY(void);
363 ir_mode *get_modeBAD(void);
365 /** Returns the machine specific pointer mode for code addresses. */
366 ir_mode *get_modeP_code(void);
368 /** Returns the machine specific pointer mode for data addresses. */
369 ir_mode *get_modeP_data(void);
372 * Sets the machine specific pointer mode for code addresses.
373 * If not set, the predefined mode mode_P will be used.
375 void set_modeP_code(ir_mode *p);
378 * Sets the machine specific pointer mode for data addresses.
379 * If not set, the predefined mode mode_P will be used.
381 void set_modeP_data(ir_mode *p);
385 Functions to check, whether a ir_modecode is signed, float, int, character,
386 reference, num, data, datab or dataM.
388 For more exact definitions read the corresponding pages
389 in the firm documentation or the following enumeration
391 The set of "float" is defined as:
392 float = {irm_F, irm_D, irm_E}
394 The set of "int" is defined as:
395 int = {irm_Bs, irm_Bu, irm_Hs, irm_Hu, irm_Is, irm_Iu, irm_Ls, irm_Lu}
397 The set of "reference" is defined as:
400 The set of "num" is defined as:
403 The set of "data" is defined as:
404 data = {num || reference}
406 The set of "datab" is defined as:
407 datab = {data || irm_b }
409 The set of "dataM" is defined as:
410 dataM = {data || irm_M}
412 Vector "int" and "float" are defined by the arithmetic and vector_elem > 1.
414 /* Test for a certain class of modes. */
415 int mode_is_signed (const ir_mode *mode);
416 int mode_is_float (const ir_mode *mode);
417 int mode_is_int (const ir_mode *mode);
418 int mode_is_reference (const ir_mode *mode);
419 int mode_is_num (const ir_mode *mode);
420 int mode_is_data (const ir_mode *mode);
421 int mode_is_datab (const ir_mode *mode);
422 int mode_is_dataM (const ir_mode *mode);
423 int mode_is_float_vector (const ir_mode *mode);
424 int mode_is_int_vector (const ir_mode *mode);
428 * Returns true if sm can be converted to lm without loss
429 * according to firm definition.
431 * Note that mode_Iu is NOT smaller than mode_Is here.
433 * @see values_in_mode()
435 int smaller_mode(const ir_mode *sm, const ir_mode *lm);
438 * Returns true if a value of mode sm can be converted into mode lm
439 * and backwards without loss.
441 * Note that mode_Iu values CAN be converted in mode_Is and back.
443 * @see smaller_mode()
445 int values_in_mode(const ir_mode *sm, const ir_mode *lm);
448 * Returns a matching unsigned mode for a given integer signed mode.
449 * Returns NULL if no matching mode exists.
451 ir_mode *find_unsigned_mode(const ir_mode *mode);
454 * Returns a matching signed mode for a given integer unsigned mode.
455 * Returns NULL if no matching mode exists.
457 ir_mode *find_signed_mode(const ir_mode *mode);
460 * Returns an integer mode with 2*n bits for a given integer mode with n bits.
461 * Returns NULL if no matching mode exists.
463 ir_mode *find_double_bits_int_mode(const ir_mode *mode);
466 * Returns non-zero if the given mode honors signed zero's, i.e.,
467 * a +0 and a -0 exists and handled differently.
469 int mode_honor_signed_zeros(const ir_mode *mode);
472 * Returns non-zero if the given mode might overflow on unary Minus.
474 int mode_overflow_on_unary_Minus(const ir_mode *mode);
477 * Returns non-zero if the mode has a reversed wrap-around
478 * logic, especially (a + x) - x == a.
479 * This is normally true for integer modes, not for floating
482 int mode_wrap_around(const ir_mode *mode);
485 * Return the signed integer equivalent mode for an reference mode.
487 ir_mode *get_reference_mode_signed_eq(ir_mode *mode);
490 * Sets the signed integer equivalent mode for an reference mode.
492 void set_reference_mode_signed_eq(ir_mode *ref_mode, ir_mode *int_mode);
495 * Return the unsigned integer equivalent mode for an reference mode.
497 ir_mode *get_reference_mode_unsigned_eq(ir_mode *mode);
500 * Sets the unsigned integer equivalent mode for an reference mode.
502 void set_reference_mode_unsigned_eq(ir_mode *ref_mode, ir_mode *int_mode);
505 * Returns non-zero if the cast from mode src to mode dst is a
506 * reinterpret cast (ie. only the bit pattern is reinterpreted,
507 * no conversion is done)
509 int is_reinterpret_cast(const ir_mode *src, const ir_mode *dst);