2 * Copyright (C) 1995-2011 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
41 #include "pattern_dmp.h"
43 /** Obstack to hold all modes. */
44 static struct obstack modes;
46 /** The list of all currently existing modes. */
47 static ir_mode **mode_list;
49 static bool modes_are_equal(const ir_mode *m, const ir_mode *n)
51 return m->sort == n->sort &&
52 m->arithmetic == n->arithmetic &&
55 m->modulo_shift == n->modulo_shift;
59 * searches the modes obstack for the given mode and returns
60 * a pointer on an equal mode already in the array, NULL if
63 static ir_mode *find_mode(const ir_mode *m)
66 for (i = 0, n_modes = ARR_LEN(mode_list); i < n_modes; ++i) {
67 ir_mode *n = mode_list[i];
68 if (modes_are_equal(n, m))
75 * sets special values of modes
77 static void set_mode_values(ir_mode* mode)
79 switch (get_mode_sort(mode)) {
82 case irms_float_number:
83 mode->min = get_tarval_min(mode);
84 mode->max = get_tarval_max(mode);
85 mode->null = get_tarval_null(mode);
86 mode->one = get_tarval_one(mode);
87 mode->minus_one = get_tarval_minus_one(mode);
88 if (get_mode_sort(mode) != irms_float_number) {
89 mode->all_one = get_tarval_all_one(mode);
91 mode->all_one = tarval_bad;
95 case irms_internal_boolean:
96 mode->min = tarval_b_false;
97 mode->max = tarval_b_true;
98 mode->null = tarval_b_false;
99 mode->one = tarval_b_true;
100 mode->minus_one = tarval_bad;
101 mode->all_one = tarval_b_true;
104 case irms_control_flow:
110 mode->min = tarval_bad;
111 mode->max = tarval_bad;
112 mode->null = tarval_bad;
113 mode->one = tarval_bad;
114 mode->minus_one = tarval_bad;
120 * globals defined in irmode.h
123 /* --- Predefined modes --- */
125 /* FIRM internal modes: */
133 /* predefined numerical modes: */
138 ir_mode *mode_Bs; /* integral values, signed and unsigned */
139 ir_mode *mode_Bu; /* 8 bit */
140 ir_mode *mode_Hs; /* 16 bit */
142 ir_mode *mode_Is; /* 32 bit */
144 ir_mode *mode_Ls; /* 64 bit */
146 ir_mode *mode_LLs; /* 128 bit */
152 /* machine specific modes */
153 ir_mode *mode_P_code; /**< machine specific pointer mode for code addresses */
154 ir_mode *mode_P_data; /**< machine specific pointer mode for data addresses */
157 * functions defined in irmode.h
160 ir_mode *get_modeT(void) { return mode_T; }
161 ir_mode *get_modeF(void) { return mode_F; }
162 ir_mode *get_modeD(void) { return mode_D; }
163 ir_mode *get_modeQ(void) { return mode_Q; }
164 ir_mode *get_modeBs(void) { return mode_Bs; }
165 ir_mode *get_modeBu(void) { return mode_Bu; }
166 ir_mode *get_modeHs(void) { return mode_Hs; }
167 ir_mode *get_modeHu(void) { return mode_Hu; }
168 ir_mode *get_modeIs(void) { return mode_Is; }
169 ir_mode *get_modeIu(void) { return mode_Iu; }
170 ir_mode *get_modeLs(void) { return mode_Ls; }
171 ir_mode *get_modeLu(void) { return mode_Lu; }
172 ir_mode *get_modeLLs(void){ return mode_LLs; }
173 ir_mode *get_modeLLu(void){ return mode_LLu; }
174 ir_mode *get_modeb(void) { return mode_b; }
175 ir_mode *get_modeP(void) { return mode_P; }
176 ir_mode *get_modeX(void) { return mode_X; }
177 ir_mode *get_modeM(void) { return mode_M; }
178 ir_mode *get_modeBB(void) { return mode_BB; }
179 ir_mode *get_modeANY(void) { return mode_ANY; }
180 ir_mode *get_modeBAD(void) { return mode_BAD; }
183 ir_mode *(get_modeP_code)(void)
185 return get_modeP_code_();
188 ir_mode *(get_modeP_data)(void)
190 return get_modeP_data_();
193 void set_modeP_code(ir_mode *p)
195 assert(mode_is_reference(p));
199 void set_modeP_data(ir_mode *p)
201 assert(mode_is_reference(p));
207 * Creates a new mode.
209 static ir_mode *alloc_mode(const char *name, ir_mode_sort sort,
210 ir_mode_arithmetic arithmetic, unsigned bit_size,
211 int sign, unsigned modulo_shift)
213 ir_mode *mode_tmpl = OALLOCZ(&modes, ir_mode);
215 mode_tmpl->name = new_id_from_str(name);
216 mode_tmpl->sort = sort;
217 mode_tmpl->size = bit_size;
218 mode_tmpl->sign = sign ? 1 : 0;
219 mode_tmpl->modulo_shift = modulo_shift;
220 mode_tmpl->arithmetic = arithmetic;
221 mode_tmpl->link = NULL;
222 mode_tmpl->tv_priv = NULL;
226 static ir_mode *register_mode(ir_mode *mode)
228 /* does any of the existing modes have the same properties? */
229 ir_mode *old = find_mode(mode);
231 /* remove new mode from obstack */
232 obstack_free(&modes, mode);
236 mode->kind = k_ir_mode;
237 mode->type = new_type_primitive(mode);
238 ARR_APP1(ir_mode*, mode_list, mode);
240 set_mode_values(mode);
245 ir_mode *new_int_mode(const char *name, ir_mode_arithmetic arithmetic,
246 unsigned bit_size, int sign, unsigned modulo_shift)
248 ir_mode *result = alloc_mode(name, irms_int_number, arithmetic, bit_size,
250 return register_mode(result);
253 ir_mode *new_reference_mode(const char *name, ir_mode_arithmetic arithmetic,
254 unsigned bit_size, unsigned modulo_shift)
256 ir_mode *result = alloc_mode(name, irms_reference, arithmetic, bit_size,
258 return register_mode(result);
261 ir_mode *new_float_mode(const char *name, ir_mode_arithmetic arithmetic,
262 unsigned exponent_size, unsigned mantissa_size)
264 bool explicit_one = false;
265 unsigned bit_size = exponent_size + mantissa_size + 1;
268 if (arithmetic == irma_x86_extended_float) {
271 } else if (arithmetic != irma_ieee754) {
272 panic("Arithmetic %s invalid for float");
274 if (exponent_size >= 256)
275 panic("Exponents >= 256 bits not supported");
276 if (mantissa_size >= 256)
277 panic("Mantissa >= 256 bits not supported");
279 result = alloc_mode(name, irms_float_number, irma_x86_extended_float, bit_size, 1, 0);
280 result->float_desc.exponent_size = exponent_size;
281 result->float_desc.mantissa_size = mantissa_size;
282 result->float_desc.explicit_one = explicit_one;
283 return register_mode(result);
286 /* Functions for the direct access to all attributes of an ir_mode */
287 ident *(get_mode_ident)(const ir_mode *mode)
289 return get_mode_ident_(mode);
292 const char *get_mode_name(const ir_mode *mode)
294 return get_id_str(mode->name);
297 unsigned (get_mode_size_bits)(const ir_mode *mode)
299 return get_mode_size_bits_(mode);
302 unsigned (get_mode_size_bytes)(const ir_mode *mode)
304 return get_mode_size_bytes_(mode);
307 int (get_mode_sign)(const ir_mode *mode)
309 return get_mode_sign_(mode);
312 ir_mode_arithmetic (get_mode_arithmetic)(const ir_mode *mode)
314 return get_mode_arithmetic_(mode);
318 /* Attribute modulo shift specifies for modes of kind irms_int_number
319 * whether shift applies modulo to value of bits to shift. Asserts
320 * if mode is not irms_int_number.
322 unsigned int (get_mode_modulo_shift)(const ir_mode *mode)
324 return get_mode_modulo_shift_(mode);
327 void *(get_mode_link)(const ir_mode *mode)
329 return get_mode_link_(mode);
332 void (set_mode_link)(ir_mode *mode, void *l)
334 set_mode_link_(mode, l);
337 ir_tarval *get_mode_min(ir_mode *mode)
340 assert(mode_is_data(mode));
345 ir_tarval *get_mode_max(ir_mode *mode)
348 assert(mode_is_data(mode));
353 ir_tarval *get_mode_null(ir_mode *mode)
356 assert(mode_is_datab(mode));
361 ir_tarval *get_mode_one(ir_mode *mode)
364 assert(mode_is_datab(mode));
369 ir_tarval *get_mode_minus_one(ir_mode *mode)
372 assert(mode_is_data(mode));
374 return mode->minus_one;
377 ir_tarval *get_mode_all_one(ir_mode *mode)
380 assert(mode_is_datab(mode));
381 return mode->all_one;
384 ir_tarval *get_mode_infinite(ir_mode *mode)
387 assert(mode_is_float(mode));
389 return get_tarval_plus_inf(mode);
392 ir_tarval *get_mode_NAN(ir_mode *mode)
395 assert(mode_is_float(mode));
397 return get_tarval_nan(mode);
400 int is_mode(const void *thing)
402 return get_kind(thing) == k_ir_mode;
405 int (mode_is_signed)(const ir_mode *mode)
407 return mode_is_signed_(mode);
410 int (mode_is_float)(const ir_mode *mode)
412 return mode_is_float_(mode);
415 int (mode_is_int)(const ir_mode *mode)
417 return mode_is_int_(mode);
420 int (mode_is_reference)(const ir_mode *mode)
422 return mode_is_reference_(mode);
425 int (mode_is_num)(const ir_mode *mode)
427 return mode_is_num_(mode);
430 int (mode_is_data)(const ir_mode *mode)
432 return mode_is_data_(mode);
435 int (mode_is_datab)(const ir_mode *mode)
437 return mode_is_datab_(mode);
440 int (mode_is_dataM)(const ir_mode *mode)
442 return mode_is_dataM_(mode);
445 unsigned (get_mode_mantissa_size)(const ir_mode *mode)
447 return get_mode_mantissa_size_(mode);
450 unsigned (get_mode_exponent_size)(const ir_mode *mode)
452 return get_mode_exponent_size_(mode);
455 /* Returns true if sm can be converted to lm without loss. */
456 int smaller_mode(const ir_mode *sm, const ir_mode *lm)
458 int sm_bits, lm_bits;
463 if (sm == lm) return 1;
465 sm_bits = get_mode_size_bits(sm);
466 lm_bits = get_mode_size_bits(lm);
468 switch (get_mode_sort(sm)) {
469 case irms_int_number:
470 switch (get_mode_sort(lm)) {
471 case irms_int_number:
472 if (get_mode_arithmetic(sm) != get_mode_arithmetic(lm))
475 /* only two complement implemented */
476 assert(get_mode_arithmetic(sm) == irma_twos_complement);
478 /* integers are convertable if
479 * - both have the same sign and lm is the larger one
480 * - lm is the signed one and is at least two bits larger
481 * (one for the sign, one for the highest bit of sm)
482 * - sm & lm are two_complement and lm has greater or equal number of bits
484 if (mode_is_signed(sm)) {
485 if (!mode_is_signed(lm))
487 return sm_bits <= lm_bits;
489 if (mode_is_signed(lm)) {
490 return sm_bits < lm_bits;
492 return sm_bits <= lm_bits;
495 case irms_float_number:
496 /* int to float works if the float is large enough */
504 case irms_float_number:
505 if (get_mode_arithmetic(sm) == get_mode_arithmetic(lm)) {
506 if ( (get_mode_sort(lm) == irms_float_number)
507 && (get_mode_size_bits(lm) >= get_mode_size_bits(sm)) )
513 /* do exist machines out there with different pointer lengths ?*/
516 case irms_internal_boolean:
517 return mode_is_int(lm);
527 /* Returns true if a value of mode sm can be converted into mode lm
528 and backwards without loss. */
529 int values_in_mode(const ir_mode *sm, const ir_mode *lm)
531 ir_mode_arithmetic arith;
536 if (sm == lm) return 1;
539 return mode_is_int(lm);
541 arith = get_mode_arithmetic(sm);
542 if (arith != get_mode_arithmetic(lm))
546 case irma_twos_complement:
548 return get_mode_size_bits(sm) <= get_mode_size_bits(lm);
555 /* Return the signed integer equivalent mode for an reference mode. */
556 ir_mode *get_reference_mode_signed_eq(ir_mode *mode)
558 assert(mode_is_reference(mode));
559 return mode->eq_signed;
562 /* Sets the signed integer equivalent mode for an reference mode. */
563 void set_reference_mode_signed_eq(ir_mode *ref_mode, ir_mode *int_mode)
565 assert(mode_is_reference(ref_mode));
566 assert(mode_is_int(int_mode));
567 ref_mode->eq_signed = int_mode;
570 /* Return the unsigned integer equivalent mode for an reference mode. */
571 ir_mode *get_reference_mode_unsigned_eq(ir_mode *mode)
573 assert(mode_is_reference(mode));
574 return mode->eq_unsigned;
577 /* Sets the unsigned integer equivalent mode for an reference mode. */
578 void set_reference_mode_unsigned_eq(ir_mode *ref_mode, ir_mode *int_mode)
580 assert(mode_is_reference(ref_mode));
581 assert(mode_is_int(int_mode));
582 ref_mode->eq_unsigned = int_mode;
585 static ir_mode *new_internal_mode(const char *name, ir_mode_sort sort)
587 ir_mode *mode = alloc_mode(name, sort, irma_none, 0, 0, 0);
588 return register_mode(mode);
591 /* initialization, build the default modes */
594 obstack_init(&modes);
595 mode_list = NEW_ARR_F(ir_mode*, 0);
597 /* initialize predefined modes */
598 mode_BB = new_internal_mode("BB", irms_block);
599 mode_X = new_internal_mode("X", irms_control_flow);
600 mode_M = new_internal_mode("M", irms_memory);
601 mode_T = new_internal_mode("T", irms_tuple);
602 mode_ANY = new_internal_mode("ANY", irms_any);
603 mode_BAD = new_internal_mode("BAD", irms_bad);
604 mode_b = new_internal_mode("b", irms_internal_boolean);
606 mode_F = new_float_mode("F", irma_ieee754, 8, 23);
607 mode_D = new_float_mode("D", irma_ieee754, 11, 52);
608 mode_Q = new_float_mode("Q", irma_ieee754, 15, 112);
610 mode_Bs = new_int_mode("Bs", irma_twos_complement, 8, 1, 32);
611 mode_Bu = new_int_mode("Bu", irma_twos_complement, 8, 0, 32);
612 mode_Hs = new_int_mode("Hs", irma_twos_complement, 16, 1, 32);
613 mode_Hu = new_int_mode("Hu", irma_twos_complement, 16, 0, 32);
614 mode_Is = new_int_mode("Is", irma_twos_complement, 32, 1, 32);
615 mode_Iu = new_int_mode("Iu", irma_twos_complement, 32, 0, 32);
616 mode_Ls = new_int_mode("Ls", irma_twos_complement, 64, 1, 64);
617 mode_Lu = new_int_mode("Lu", irma_twos_complement, 64, 0, 64);
618 mode_LLs = new_int_mode("LLs", irma_twos_complement, 128, 1, 128);
619 mode_LLu = new_int_mode("LLu", irma_twos_complement, 128, 0, 128);
621 mode_P = new_reference_mode("P", irma_twos_complement, 32, 32);
622 set_reference_mode_signed_eq(mode_P, mode_Is);
623 set_reference_mode_unsigned_eq(mode_P, mode_Iu);
625 /* set the machine specific modes to the predefined ones */
626 mode_P_code = mode_P;
627 mode_P_data = mode_P;
630 /* find a signed mode for an unsigned integer mode */
631 ir_mode *find_unsigned_mode(const ir_mode *mode)
635 /* allowed for reference mode */
636 if (mode->sort == irms_reference)
637 n.sort = irms_int_number;
639 assert(n.sort == irms_int_number);
641 return find_mode(&n);
644 /* find an unsigned mode for a signed integer mode */
645 ir_mode *find_signed_mode(const ir_mode *mode)
649 assert(mode->sort == irms_int_number);
651 return find_mode(&n);
654 /* finds a integer mode with 2*n bits for an integer mode with n bits. */
655 ir_mode *find_double_bits_int_mode(const ir_mode *mode)
659 assert(mode->sort == irms_int_number && mode->arithmetic == irma_twos_complement);
661 n.size = 2*mode->size;
662 return find_mode(&n);
666 * Returns non-zero if the given mode honors signed zero's, i.e.,
667 * a +0 and a -0 exists and handled differently.
669 int mode_honor_signed_zeros(const ir_mode *mode)
671 /* for floating point, we know that IEEE 754 has +0 and -0,
672 * but always handles it identical.
675 mode->sort == irms_float_number &&
676 mode->arithmetic != irma_ieee754;
680 * Returns non-zero if the given mode might overflow on unary Minus.
682 * This does NOT happen on IEEE 754.
684 int mode_overflow_on_unary_Minus(const ir_mode *mode)
686 if (mode->sort == irms_float_number)
687 return mode->arithmetic == irma_ieee754 ? 0 : 1;
692 * Returns non-zero if the mode has a reversed wrap-around
693 * logic, especially (a + x) - x == a.
695 * This is normally true for integer modes, not for floating
698 int mode_wrap_around(const ir_mode *mode)
700 /* FIXME: better would be an extra mode property */
701 return mode_is_int(mode);
705 * Returns non-zero if the cast from mode src to mode dst is a
706 * reinterpret cast (ie. only the bit pattern is reinterpreted,
707 * no conversion is done)
709 int is_reinterpret_cast(const ir_mode *src, const ir_mode *dst)
711 ir_mode_arithmetic ma;
715 if (get_mode_size_bits(src) != get_mode_size_bits(dst))
717 ma = get_mode_arithmetic(src);
718 if (ma != get_mode_arithmetic(dst))
721 return ma == irma_twos_complement;
724 ir_type *(get_type_for_mode) (const ir_mode *mode)
726 return get_type_for_mode_(mode);
729 void finish_mode(void)
731 obstack_free(&modes, 0);
732 DEL_ARR_F(mode_list);