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"
40 /* ********** Predefined modes ********** */
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 */
51 * These values represent the different mode classes of value representations.
52 * Beware: do not change the order of these values without checking
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) */
61 /** Internal boolean representation.
62 Storing to memory impossible, convert first. (irm_b) */
63 irms_internal_boolean = 3 | irmsh_is_datab,
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,
77 /** These values represent the different arithmetic operations possible with a mode.
78 Further arithmetics can be defined, e.g., for @@@ modes.
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
98 /** Returns the name of the arithmetic type. */
99 const char *get_mode_arithmetic_name(ir_mode_arithmetic ari);
101 /* ********** Constructor for user defined modes **************** */
103 * Creates a new mode.
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.
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
120 * The new mode or NULL on error.
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.
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);
130 * Creates a new vector 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 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.
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
147 * The new mode or NULL on error.
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);
153 * Checks whether a pointer points to a mode.
155 * @param thing an arbitrary pointer
158 * true if the thing is a mode, else false
160 int is_mode(const void *thing);
162 /* ********** Access methods to read mode information *********** */
164 /** Returns the ident* of the mode */
165 ident *get_mode_ident(const ir_mode *mode);
167 /** Returns the null-terminated name of this mode. */
168 const char *get_mode_name(const ir_mode *mode);
170 /** Returns a coarse classification of the mode. */
171 ir_mode_sort get_mode_sort(const ir_mode *mode);
173 /** Returns the size of values of the mode in bits. */
174 unsigned get_mode_size_bits(const ir_mode *mode);
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);
180 /** Returns the signess of a mode.
182 * Returns the signess of a mode: 1 if mode is signed. */
183 int get_mode_sign(const ir_mode *mode);
185 /** Returns the arithmetic of a mode */
186 ir_mode_arithmetic get_mode_arithmetic(const ir_mode *mode);
188 /** Get the modulo shift attribute.
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.
194 unsigned int get_mode_modulo_shift(const ir_mode *mode);
196 /** Return the number of vector elements.
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
202 unsigned int get_mode_n_vector_elems(const ir_mode *mode);
204 /** Returns the stored intermediate information. */
205 void *get_mode_link(const ir_mode *mode);
207 /** Stores new intermediate information. */
208 void set_mode_link(ir_mode *mode, void *l);
211 * Returns the smallest representable value of a given mode.
213 * For modes of the sort float_number this is the most negative value
214 * bigger than -infinite.
216 tarval *get_mode_min(ir_mode *mode);
219 * Returns the biggest representable value o f a given mode.
221 * For modes of the sort float_number this is the largest value lower
224 tarval *get_mode_max(ir_mode *mode);
227 * Returns the value Zero represented in this mode.
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.
234 tarval *get_mode_null(ir_mode *mode);
237 * Returns the value One, represented in this mode.
239 * One, being the multiplicative neutral element,
240 * is defined only for modes allowing multiplication,
241 * i.e. ints and floats.
243 tarval *get_mode_one(ir_mode *mode);
246 * Returns the value Minus One, represented in this mode.
248 * Minus One is defined only for modes allowing
249 * multiplication with signed values, i.e. signed ints and floats.
251 tarval *get_mode_minus_one(ir_mode *mode);
254 * Returns the value where all bits are One, represented in this mode.
256 * All One is defined only for modes integer, reference and boolean modes
258 tarval *get_mode_all_one(ir_mode *mode);
261 * Returns the positive infinite value of a mode.
263 * This is only valid for float_numbers, other modes
264 * will result in tarval_bad.
266 tarval *get_mode_infinite(ir_mode *mode);
269 * Returns the NAN value of a given mode.
271 * This is only valid for float_numbers, other modes
272 * will result in tarval_bad.
274 tarval *get_mode_NAN(ir_mode *mode);
276 extern ir_mode *mode_M; /**< memory */
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 */
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
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
301 /* -- Auxiliary modes necessary for the Firm representation -- */
302 extern ir_mode *mode_b; /**< internal boolean */
304 extern ir_mode *mode_X; /**< execution */
305 extern ir_mode *mode_BB; /**< block */
307 extern ir_mode *mode_T; /**< tuple (none) */
308 extern ir_mode *mode_ANY;/**< undefined mode */
309 extern ir_mode *mode_BAD;/**< bad mode */
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);
335 /** Returns the machine specific pointer mode for code addresses. */
336 ir_mode *get_modeP_code(void);
338 /** Returns the machine specific pointer mode for data addresses. */
339 ir_mode *get_modeP_data(void);
342 * Sets the machine specific pointer mode for code addresses.
343 * If not set, the predefined mode mode_P will be used.
345 void set_modeP_code(ir_mode *p);
348 * Sets the machine specific pointer mode for data addresses.
349 * If not set, the predefined mode mode_P will be used.
351 void set_modeP_data(ir_mode *p);
355 Functions to check, whether a mode is signed, float, int, character,
356 reference, num, data, datab or dataM.
358 For more exact definitions read the corresponding pages
359 in the firm documentation or the following enumeration
361 The set of "float" is defined as:
362 float = {irm_F, irm_D, irm_E}
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}
367 The set of "reference" is defined as:
370 The set of "num" is defined as:
373 The set of "data" is defined as:
374 data = {num || reference}
376 The set of "datab" is defined as:
377 datab = {data || irm_b }
379 The set of "dataM" is defined as:
380 dataM = {data || irm_M}
382 Vector "int" and "float" are defined by the arithmetic and vector_elem > 1.
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);
398 * Returns true if sm can be converted to lm without loss
399 * according to firm definition.
401 * Note that mode_Iu is NOT smaller than mode_Is here.
403 * @see values_in_mode()
405 int smaller_mode(const ir_mode *sm, const ir_mode *lm);
408 * Returns true if a value of mode sm can be converted into mode lm
409 * and backwards without loss.
411 * Note that mode_Iu values CAN be converted in mode_Is and back.
413 * @see smaller_mode()
415 int values_in_mode(const ir_mode *sm, const ir_mode *lm);
418 * Returns a matching unsigned mode for a given integer signed mode.
419 * Returns NULL if no matching mode exists.
421 ir_mode *find_unsigned_mode(const ir_mode *mode);
424 * Returns a matching signed mode for a given integer unsigned mode.
425 * Returns NULL if no matching mode exists.
427 ir_mode *find_signed_mode(const ir_mode *mode);
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.
433 ir_mode *find_double_bits_int_mode(const ir_mode *mode);
436 * Returns non-zero if the given mode honors signed zero's, i.e.,
437 * a +0 and a -0 exists and handled differently.
439 int mode_honor_signed_zeros(const ir_mode *mode);
442 * Returns non-zero if the given mode might overflow on unary Minus.
444 int mode_overflow_on_unary_Minus(const ir_mode *mode);
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
452 int mode_wrap_around(const ir_mode *mode);
455 * Return the signed integer equivalent mode for an reference mode.
457 ir_mode *get_reference_mode_signed_eq(ir_mode *mode);
460 * Sets the signed integer equivalent mode for an reference mode.
462 void set_reference_mode_signed_eq(ir_mode *ref_mode, ir_mode *int_mode);
465 * Return the unsigned integer equivalent mode for an reference mode.
467 ir_mode *get_reference_mode_unsigned_eq(ir_mode *mode);
470 * Sets the unsigned integer equivalent mode for an reference mode.
472 void set_reference_mode_unsigned_eq(ir_mode *ref_mode, ir_mode *int_mode);
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)
479 int is_reinterpret_cast(const ir_mode *src, const ir_mode *dst);
482 * Returns the primitive type matching the given mode
484 ir_type *get_type_for_mode(const ir_mode *mode);