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 Representation of and static computations on target machine
25 * @author Mathias Heil
28 * Values are stored in a format depending upon chosen arithmetic
29 * module. Default uses strcalc and fltcalc.
30 * This implementation assumes:
31 * - target has IEEE-754 floating-point arithmetic.
41 #include "bitfiddle.h"
51 #include "firm_common.h"
54 /** Size of hash tables. Should correspond to average number of distinct constant
56 #define N_CONSTANTS 2048
58 /* unused, float to int doesn't work yet */
59 typedef enum float_to_int_mode {
64 static float_to_int_mode current_float_to_int_mode = TRUNCATE;
66 /* set this to true if infinity should be clipped to +/- MAX_FLOAT */
67 #define SWITCH_NOINFINITY 0
68 /* set this to true if denormals should be clipped to zero */
69 #define SWITCH_NODENORMALS 0
71 /****************************************************************************
72 * local definitions and macros
73 ****************************************************************************/
75 # define TARVAL_VERIFY(a) tarval_verify((a))
77 # define TARVAL_VERIFY(a) ((void)0)
80 #define INSERT_TARVAL(tv) (set_insert(ir_tarval, tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
81 #define FIND_TARVAL(tv) (set_find(ir_tarval, tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
83 #define INSERT_VALUE(val, size) (set_insert(char, values, (val), size, hash_val((val), size)))
84 #define FIND_VALUE(val, size) (set_find(char, values, (val), size, hash_val((val), size)))
86 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
88 /** A set containing all existing tarvals. */
89 static struct set *tarvals = NULL;
90 /** A set containing all existing values. */
91 static struct set *values = NULL;
93 /** The carry flag for SOME operations. -1 means UNDEFINED here */
94 static int carry_flag = -1;
96 /** The integer overflow mode. */
97 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
99 /** if this is set non-zero, the constant folding for floating point is OFF */
100 static int no_float = 0;
102 /****************************************************************************
104 ****************************************************************************/
106 static unsigned hash_val(const void *value, size_t length);
107 static unsigned hash_tv(ir_tarval *tv);
108 static void _fail_verify(ir_tarval *tv, const char* file, int line)
110 /* print a memory image of the tarval and throw an assertion */
112 panic("%s:%d: Invalid tarval: mode: %F\n value: [%p]", file, line, tv->mode, tv->value);
114 panic("%s:%d: Invalid tarval (null)", file, line);
119 __attribute__((unused))
121 void tarval_verify(ir_tarval *tv)
127 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
128 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
130 if (!FIND_TARVAL(tv)) fail_verify(tv);
131 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
135 /** Hash a tarval. */
136 static unsigned hash_tv(ir_tarval *tv)
138 return (unsigned)((PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length);
141 /** Hash a value. Treat it as a byte array. */
142 static unsigned hash_val(const void *value, size_t length)
147 /* scramble the byte - array */
148 for (i = 0; i < length; ++i) {
149 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
150 hash += (hash << 11) ^ (hash >> 17);
156 static int cmp_tv(const void *p1, const void *p2, size_t n)
158 const ir_tarval *tv1 = (const ir_tarval*) p1;
159 const ir_tarval *tv2 = (const ir_tarval*) p2;
162 assert(tv1->kind == k_tarval);
163 assert(tv2->kind == k_tarval);
164 if (tv1->mode < tv2->mode)
166 if (tv1->mode > tv2->mode)
168 if (tv1->length < tv2->length)
170 if (tv1->length > tv2->length)
172 if (tv1->value < tv2->value)
174 if (tv1->value > tv2->value)
180 /** finds tarval with value/mode or creates new tarval */
181 static ir_tarval *get_tarval(const void *value, size_t length, ir_mode *mode)
189 /* if there already is such a value, it is returned, else value
190 * is copied into the set */
191 char *temp = (char*) alloca(length);
192 memcpy(temp, value, length);
193 if (get_mode_arithmetic(mode) == irma_twos_complement) {
194 sign_extend(temp, mode);
196 tv.value = INSERT_VALUE(temp, length);
200 /* if there is such a tarval, it is returned, else tv is copied
202 return INSERT_TARVAL(&tv);
208 static ir_tarval *get_tarval_overflow(const void *value, size_t length, ir_mode *mode)
212 switch (get_mode_sort(mode)) {
214 /* addresses always wrap around */
215 temp = (char*) alloca(sc_get_buffer_length());
216 memcpy(temp, value, sc_get_buffer_length());
217 sc_truncate(get_mode_size_bits(mode), temp);
218 /* the sc_ module expects that all bits are set ... */
219 sign_extend(temp, mode);
220 return get_tarval(temp, length, mode);
222 case irms_int_number:
223 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
224 switch (tarval_get_integer_overflow_mode()) {
225 case TV_OVERFLOW_SATURATE:
226 return get_mode_max(mode);
227 case TV_OVERFLOW_WRAP:
228 temp = (char*) alloca(sc_get_buffer_length());
229 memcpy(temp, value, sc_get_buffer_length());
230 sc_truncate(get_mode_size_bits(mode), temp);
231 /* the sc_ module expects that all bits are set ... */
232 sign_extend(temp, mode);
233 return get_tarval(temp, length, mode);
234 case TV_OVERFLOW_BAD:
237 return get_tarval(value, length, mode);
240 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
241 switch (tarval_get_integer_overflow_mode()) {
242 case TV_OVERFLOW_SATURATE:
243 return get_mode_min(mode);
244 case TV_OVERFLOW_WRAP: {
245 temp = (char*) alloca(sc_get_buffer_length());
246 memcpy(temp, value, sc_get_buffer_length());
247 sc_truncate(get_mode_size_bits(mode), temp);
248 return get_tarval(temp, length, mode);
250 case TV_OVERFLOW_BAD:
253 return get_tarval(value, length, mode);
258 case irms_float_number:
259 #if SWITCH_NOINFINITY
260 if (fc_is_inf((const fp_value*) value)) {
261 /* clip infinity to maximum value */
262 return fc_is_negative((const fp_value*) value) ? get_mode_min(mode) : get_mode_max(mode);
265 #if SWITCH_NODENORMALS
266 if (fc_is_subnormal((const fp_value*) value)) {
267 /* clip denormals to zero */
268 return get_mode_null(mode);
276 return get_tarval(value, length, mode);
280 * public variables declared in tv.h
282 static ir_tarval reserved_tv[6];
284 ir_tarval *tarval_b_false = &reserved_tv[0];
285 ir_tarval *tarval_b_true = &reserved_tv[1];
286 ir_tarval *tarval_bad = &reserved_tv[2];
287 ir_tarval *tarval_undefined = &reserved_tv[3];
288 ir_tarval *tarval_reachable = &reserved_tv[4];
289 ir_tarval *tarval_unreachable = &reserved_tv[5];
292 * get the float descriptor for given mode.
294 static const float_descriptor_t *get_descriptor(const ir_mode *mode)
296 return &mode->float_desc;
299 ir_tarval *new_integer_tarval_from_str(const char *str, size_t len, char sign,
300 unsigned char base, ir_mode *mode)
305 buffer = alloca(sc_get_buffer_length());
307 ok = sc_val_from_str(sign, base, str, len, buffer);
311 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
314 static ir_tarval *new_tarval_from_str_int(const char *str, size_t len,
322 /* skip leading spaces */
323 while (len > 0 && str[0] == ' ') {
330 /* 1 sign character allowed */
335 } else if (str[0] == '+') {
340 /* a number starting with '0x' is hexadeciaml,
341 * a number starting with '0' (and at least 1 more char) is octal */
342 if (len >= 2 && str[0] == '0') {
343 if (str[1] == 'x' || str[1] == 'X') {
347 } else if (str[1] == 'b' || str[1] == 'B') {
360 buffer = alloca(sc_get_buffer_length());
362 ok = sc_val_from_str(sign, base, str, len, buffer);
366 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
370 * Constructors =============================================================
372 ir_tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
374 const float_descriptor_t *desc;
380 switch (get_mode_sort(mode)) {
381 case irms_internal_boolean:
382 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
383 if (!strcasecmp(str, "true"))
384 return tarval_b_true;
385 else if (!strcasecmp(str, "false"))
386 return tarval_b_false;
388 /* XXX This is C semantics */
389 return atoi(str) ? tarval_b_true : tarval_b_false;
391 case irms_float_number:
392 desc = get_descriptor(mode);
393 fc_val_from_str(str, len, desc, NULL);
394 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
397 if (!strcasecmp(str, "null"))
398 return get_tarval_null(mode);
400 case irms_int_number:
401 return new_tarval_from_str_int(str, len, mode);
403 panic("Unsupported tarval creation with mode %F", mode);
408 * helper function, create a tarval from long
410 ir_tarval *new_tarval_from_long(long l, ir_mode *mode)
414 switch (get_mode_sort(mode)) {
415 case irms_internal_boolean:
416 /* XXX C semantics ! */
417 return l ? tarval_b_true : tarval_b_false ;
420 /* same as integer modes */
421 case irms_int_number:
422 sc_val_from_long(l, NULL);
423 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
425 case irms_float_number:
426 return new_tarval_from_double((long double)l, mode);
429 panic("unsupported mode sort");
433 /* returns non-zero if can be converted to long */
434 int tarval_is_long(ir_tarval *tv)
436 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
439 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
440 /* the value might be too big to fit in a long */
441 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
442 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
443 /* really doesn't fit */
450 /* this might overflow the machine's long, so use only with small values */
451 long get_tarval_long(ir_tarval* tv)
453 assert(tarval_is_long(tv) && "tarval too big to fit in long");
455 return sc_val_to_long(tv->value);
458 ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode)
460 const float_descriptor_t *desc;
462 assert(mode && (get_mode_sort(mode) == irms_float_number));
463 desc = get_descriptor(mode);
464 fc_val_from_ieee754(d, desc, NULL);
465 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
468 ir_tarval *new_tarval_from_double(double d, ir_mode *mode)
470 return new_tarval_from_long_double(d, mode);
473 /* returns non-zero if can be converted to double */
474 int tarval_is_double(ir_tarval *tv)
478 return (get_mode_sort(tv->mode) == irms_float_number);
481 long double get_tarval_long_double(ir_tarval *tv)
483 assert(tarval_is_double(tv));
485 return fc_val_to_ieee754((const fp_value*) tv->value);
488 double get_tarval_double(ir_tarval *tv)
490 return get_tarval_long_double(tv);
495 * Access routines for tarval fields ========================================
498 /* get the mode of the tarval */
499 ir_mode *(get_tarval_mode)(const ir_tarval *tv)
501 return _get_tarval_mode(tv);
505 * Special value query functions ============================================
507 * These functions calculate and return a tarval representing the requested
509 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
510 * functions, but these are stored on initialization of the irmode module and
511 * therefore the irmode functions should be preferred to the functions below.
514 ir_tarval *(get_tarval_bad)(void)
516 return _get_tarval_bad();
519 ir_tarval *(get_tarval_undefined)(void)
521 return _get_tarval_undefined();
524 ir_tarval *(get_tarval_b_false)(void)
526 return _get_tarval_b_false();
529 ir_tarval *(get_tarval_b_true)(void)
531 return _get_tarval_b_true();
534 ir_tarval *(get_tarval_reachable)(void)
536 return _get_tarval_reachable();
539 ir_tarval *(get_tarval_unreachable)(void)
541 return _get_tarval_unreachable();
544 ir_tarval *get_tarval_max(ir_mode *mode)
546 const float_descriptor_t *desc;
548 switch (get_mode_sort(mode)) {
549 case irms_internal_boolean:
550 return tarval_b_true;
552 case irms_float_number:
553 desc = get_descriptor(mode);
554 fc_get_max(desc, NULL);
555 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
558 case irms_int_number:
559 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
560 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
562 panic("mode %F does not support maximum value", mode);
566 ir_tarval *get_tarval_min(ir_mode *mode)
568 const float_descriptor_t *desc;
570 switch (get_mode_sort(mode)) {
571 case irms_internal_boolean:
572 return tarval_b_false;
574 case irms_float_number:
575 desc = get_descriptor(mode);
576 fc_get_min(desc, NULL);
577 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
580 case irms_int_number:
581 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
582 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
584 panic("mode %F does not support minimum value", mode);
588 /** The bit pattern for the pointer NULL */
589 static long _null_value = 0;
591 ir_tarval *get_tarval_null(ir_mode *mode)
593 switch (get_mode_sort(mode)) {
594 case irms_float_number:
595 return new_tarval_from_double(0.0, mode);
597 case irms_internal_boolean:
598 case irms_int_number:
599 return new_tarval_from_long(0l, mode);
602 return new_tarval_from_long(_null_value, mode);
604 panic("mode %F does not support null value", mode);
608 ir_tarval *get_tarval_one(ir_mode *mode)
610 switch (get_mode_sort(mode)) {
611 case irms_internal_boolean:
612 return tarval_b_true;
614 case irms_float_number:
615 return new_tarval_from_double(1.0, mode);
618 case irms_int_number:
619 return new_tarval_from_long(1l, mode);
621 panic("mode %F does not support one value", mode);
625 ir_tarval *get_tarval_all_one(ir_mode *mode)
627 switch (get_mode_sort(mode)) {
628 case irms_int_number:
629 case irms_internal_boolean:
631 return tarval_not(get_mode_null(mode));
633 case irms_float_number:
634 return new_tarval_from_double(1.0, mode);
637 panic("mode %F does not support all-one value", mode);
641 int tarval_is_constant(ir_tarval *tv)
643 size_t const num_res = ARRAY_SIZE(reserved_tv);
645 /* reserved tarvals are NOT constants. Note that although
646 tarval_b_true and tarval_b_false are reserved, they are constants of course. */
647 return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
650 ir_tarval *get_tarval_minus_one(ir_mode *mode)
652 switch (get_mode_sort(mode)) {
656 case irms_float_number:
657 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
659 case irms_int_number:
660 return new_tarval_from_long(-1l, mode);
663 panic("mode %F does not support minus one value", mode);
667 ir_tarval *get_tarval_nan(ir_mode *mode)
669 const float_descriptor_t *desc;
671 if (get_mode_sort(mode) == irms_float_number) {
672 desc = get_descriptor(mode);
673 fc_get_qnan(desc, NULL);
674 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
676 panic("mode %F does not support NaN value", mode);
679 ir_tarval *get_tarval_plus_inf(ir_mode *mode)
681 if (get_mode_sort(mode) == irms_float_number) {
682 const float_descriptor_t *desc = get_descriptor(mode);
683 fc_get_plusinf(desc, NULL);
684 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
686 panic("mode %F does not support +inf value", mode);
689 ir_tarval *get_tarval_minus_inf(ir_mode *mode)
691 if (get_mode_sort(mode) == irms_float_number) {
692 const float_descriptor_t *desc = get_descriptor(mode);
693 fc_get_minusinf(desc, NULL);
694 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
696 panic("mode %F does not support -inf value", mode);
700 * Arithmetic operations on tarvals ========================================
704 * test if negative number, 1 means 'yes'
706 int tarval_is_negative(ir_tarval *a)
708 switch (get_mode_sort(a->mode)) {
709 case irms_int_number:
710 if (!mode_is_signed(a->mode)) return 0;
712 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
714 case irms_float_number:
715 return fc_is_negative((const fp_value*) a->value);
718 panic("mode %F does not support negation value", a->mode);
723 * test if null, 1 means 'yes'
725 int tarval_is_null(ir_tarval *a)
729 a == get_mode_null(get_tarval_mode(a));
733 * test if one, 1 means 'yes'
735 int tarval_is_one(ir_tarval *a)
739 a == get_mode_one(get_tarval_mode(a));
742 int tarval_is_all_one(ir_tarval *tv)
746 tv == get_mode_all_one(get_tarval_mode(tv));
750 * test if one, 1 means 'yes'
752 int tarval_is_minus_one(ir_tarval *a)
756 a == get_mode_minus_one(get_tarval_mode(a));
762 ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b)
766 if (a == tarval_bad || b == tarval_bad) {
767 panic("Comparison with tarval_bad");
770 if (a == tarval_undefined || b == tarval_undefined)
771 return ir_relation_false;
773 if (a->mode != b->mode)
774 return ir_relation_false;
776 /* Here the two tarvals are unequal and of the same mode */
777 switch (get_mode_sort(a->mode)) {
778 case irms_float_number:
780 * BEWARE: we cannot compare a == b here, because
781 * a NaN is always Unordered to any other value, even to itself!
783 switch (fc_comp((const fp_value*) a->value, (const fp_value*) b->value)) {
784 case -1: return ir_relation_less;
785 case 0: return ir_relation_equal;
786 case 1: return ir_relation_greater;
787 case 2: return ir_relation_unordered;
788 default: return ir_relation_false;
791 case irms_int_number:
793 return ir_relation_equal;
794 return sc_comp(a->value, b->value) == 1 ? ir_relation_greater : ir_relation_less;
796 case irms_internal_boolean:
798 return ir_relation_equal;
799 return a == tarval_b_true ? ir_relation_greater : ir_relation_less;
802 panic("can't compare values of mode %F", a->mode);
807 * convert to other mode
809 ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *dst_mode)
812 fp_value *res = NULL;
813 const float_descriptor_t *desc;
821 if (src->mode == dst_mode)
824 switch (get_mode_sort(src->mode)) {
825 /* cast float to something */
826 case irms_float_number:
827 switch (get_mode_sort(dst_mode)) {
828 case irms_float_number:
829 desc = get_descriptor(dst_mode);
830 fc_cast((const fp_value*) src->value, desc, NULL);
831 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
833 case irms_int_number:
834 switch (current_float_to_int_mode) {
836 res = fc_int((const fp_value*) src->value, NULL);
839 res = fc_rnd((const fp_value*) src->value, NULL);
842 buffer = (char*) alloca(sc_get_buffer_length());
843 if (! fc_flt2int(res, buffer, dst_mode))
845 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
850 /* the rest can't be converted */
853 /* cast int/characters to something */
854 case irms_int_number:
855 switch (get_mode_sort(dst_mode)) {
858 case irms_int_number:
859 buffer = (char*) alloca(sc_get_buffer_length());
860 memcpy(buffer, src->value, sc_get_buffer_length());
861 return get_tarval_overflow(buffer, src->length, dst_mode);
863 case irms_internal_boolean:
864 /* XXX C semantics */
865 if (src == get_mode_null(src->mode)) return tarval_b_false;
866 else return tarval_b_true;
868 case irms_float_number:
869 /* XXX floating point unit does not understand internal integer
870 * representation, convert to string first, then create float from
872 buffer = (char*) alloca(100);
873 /* decimal string representation because hexadecimal output is
874 * interpreted unsigned by fc_val_from_str, so this is a HACK */
875 len = snprintf(buffer, 100, "%s",
876 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
877 buffer[100 - 1] = '\0';
878 desc = get_descriptor(dst_mode);
879 fc_val_from_str(buffer, len, desc, NULL);
880 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
887 case irms_internal_boolean:
888 /* beware: this is C semantic for the INTERNAL boolean mode */
889 if (get_mode_sort(dst_mode) == irms_int_number)
890 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
894 if (get_mode_sort(dst_mode) == irms_int_number) {
895 buffer = (char*) alloca(sc_get_buffer_length());
896 memcpy(buffer, src->value, sc_get_buffer_length());
897 sign_extend(buffer, src->mode);
898 return get_tarval_overflow(buffer, src->length, dst_mode);
911 ir_tarval *tarval_not(ir_tarval *a)
917 /* works for vector mode without changes */
919 switch (get_mode_sort(a->mode)) {
921 case irms_int_number:
922 buffer = (char*) alloca(sc_get_buffer_length());
923 sc_not(a->value, buffer);
924 return get_tarval(buffer, a->length, a->mode);
926 case irms_internal_boolean:
927 if (a == tarval_b_true)
928 return tarval_b_false;
929 if (a == tarval_b_false)
930 return tarval_b_true;
934 panic("bitwise negation is only allowed for integer and boolean");
939 * arithmetic negation
941 ir_tarval *tarval_neg(ir_tarval *a)
945 assert(mode_is_num(a->mode)); /* negation only for numerical values */
949 /* note: negation is allowed even for unsigned modes. */
951 switch (get_mode_sort(a->mode)) {
952 case irms_int_number:
953 buffer = (char*) alloca(sc_get_buffer_length());
954 sc_neg(a->value, buffer);
955 return get_tarval_overflow(buffer, a->length, a->mode);
957 case irms_float_number:
958 /* it should be safe to enable this even if other arithmetic is disabled */
962 fc_neg((const fp_value*) a->value, NULL);
963 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
973 ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b)
979 if (mode_is_reference(a->mode) && a->mode != b->mode) {
980 b = tarval_convert_to(b, a->mode);
981 } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
982 a = tarval_convert_to(a, b->mode);
985 assert(a->mode == b->mode);
987 switch (get_mode_sort(a->mode)) {
989 case irms_int_number:
990 /* modes of a,b are equal, so result has mode of a as this might be the character */
991 buffer = (char*) alloca(sc_get_buffer_length());
992 sc_add(a->value, b->value, buffer);
993 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
994 return get_tarval_overflow(buffer, a->length, a->mode);
996 case irms_float_number:
1000 fc_add((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1001 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1011 ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode)
1017 if (dst_mode != NULL) {
1018 if (a->mode != dst_mode)
1019 a = tarval_convert_to(a, dst_mode);
1020 if (b->mode != dst_mode)
1021 b = tarval_convert_to(b, dst_mode);
1023 assert(a->mode == b->mode);
1025 switch (get_mode_sort(a->mode)) {
1026 case irms_reference:
1027 case irms_int_number:
1028 /* modes of a,b are equal, so result has mode of a as this might be the character */
1029 buffer = (char*) alloca(sc_get_buffer_length());
1030 sc_sub(a->value, b->value, buffer);
1031 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1032 return get_tarval_overflow(buffer, a->length, a->mode);
1034 case irms_float_number:
1038 fc_sub((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1039 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1049 ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b)
1053 assert(a->mode == b->mode);
1057 switch (get_mode_sort(a->mode)) {
1058 case irms_int_number:
1059 /* modes of a,b are equal */
1060 buffer = (char*) alloca(sc_get_buffer_length());
1061 sc_mul(a->value, b->value, buffer);
1062 return get_tarval_overflow(buffer, a->length, a->mode);
1064 case irms_float_number:
1068 fc_mul((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1069 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1078 * overflow is impossible, but look out for division by zero
1080 ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b)
1082 ir_mode *mode = a->mode;
1083 assert(mode == b->mode);
1087 if (mode_is_int(mode)) {
1089 if (b == get_mode_null(mode))
1092 /* modes of a,b are equal */
1093 sc_div(a->value, b->value, NULL);
1094 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1096 assert(mode_is_float(mode));
1097 fc_div((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1098 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), mode);
1104 * overflow is impossible, but look out for division by zero
1106 ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b)
1108 assert((a->mode == b->mode) && mode_is_int(a->mode));
1113 if (b == get_mode_null(b->mode)) return tarval_bad;
1114 /* modes of a,b are equal */
1115 sc_mod(a->value, b->value, NULL);
1116 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1120 * integer division AND remainder
1121 * overflow is impossible, but look out for division by zero
1123 ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod)
1125 int len = sc_get_buffer_length();
1126 char *div_res = (char*) alloca(len);
1127 char *mod_res = (char*) alloca(len);
1129 assert((a->mode == b->mode) && mode_is_int(a->mode));
1134 if (b == get_mode_null(b->mode)) return tarval_bad;
1135 /* modes of a,b are equal */
1136 sc_divmod(a->value, b->value, div_res, mod_res);
1137 *mod = get_tarval(mod_res, len, a->mode);
1138 return get_tarval(div_res, len, a->mode);
1144 ir_tarval *tarval_abs(ir_tarval *a)
1149 assert(mode_is_num(a->mode));
1151 switch (get_mode_sort(a->mode)) {
1152 case irms_int_number:
1153 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1154 buffer = (char*) alloca(sc_get_buffer_length());
1155 sc_neg(a->value, buffer);
1156 return get_tarval_overflow(buffer, a->length, a->mode);
1160 case irms_float_number:
1161 /* it should be safe to enable this even if other arithmetic is disabled */
1163 return tarval_bad;*/
1165 if (fc_comp((const fp_value*) a->value,
1166 (const fp_value*) get_mode_null(a->mode)->value) == -1) {
1167 fc_neg((const fp_value*) a->value, NULL);
1168 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1181 ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b)
1183 assert(a->mode == b->mode);
1185 /* works even for vector modes */
1188 switch (get_mode_sort(a->mode)) {
1189 case irms_internal_boolean:
1190 return (a == tarval_b_false) ? a : b;
1192 case irms_reference:
1193 case irms_int_number:
1194 sc_and(a->value, b->value, NULL);
1195 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1198 panic("operation not defined on mode");
1202 ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b)
1204 assert(a->mode == b->mode);
1206 /* works even for vector modes */
1209 switch (get_mode_sort(a->mode)) {
1210 case irms_internal_boolean:
1211 return a == tarval_b_true && b == tarval_b_false ? tarval_b_true : tarval_b_false;
1213 case irms_reference:
1214 case irms_int_number:
1215 sc_andnot(a->value, b->value, NULL);
1216 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1219 panic("operation not defined on mode");
1226 ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b)
1228 assert(a->mode == b->mode);
1230 /* works even for vector modes */
1233 switch (get_mode_sort(a->mode)) {
1234 case irms_internal_boolean:
1235 return (a == tarval_b_true) ? a : b;
1237 case irms_reference:
1238 case irms_int_number:
1239 sc_or(a->value, b->value, NULL);
1240 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1243 panic("operation not defined on mode");
1248 * bitwise exclusive or (xor)
1250 ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b)
1252 assert((a->mode == b->mode));
1254 /* works even for vector modes */
1257 switch (get_mode_sort(a->mode)) {
1258 case irms_internal_boolean:
1259 return (a == b)? tarval_b_false : tarval_b_true;
1261 case irms_reference:
1262 case irms_int_number:
1263 sc_xor(a->value, b->value, NULL);
1264 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1267 panic("operation not defined on mode");
1272 * bitwise left shift
1274 ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b)
1276 char *temp_val = NULL;
1278 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1282 if (get_mode_modulo_shift(a->mode) != 0) {
1283 temp_val = (char*) alloca(sc_get_buffer_length());
1285 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1286 sc_mod(b->value, temp_val, temp_val);
1288 temp_val = (char*)b->value;
1290 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1291 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1294 ir_tarval *tarval_shl_unsigned(ir_tarval *a, unsigned b)
1296 ir_mode *mode = a->mode;
1297 unsigned modulo = get_mode_modulo_shift(mode);
1300 assert((unsigned)(long)b==b);
1301 sc_shlI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1302 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1306 * bitwise unsigned right shift
1308 ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b)
1310 char *temp_val = NULL;
1312 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1316 if (get_mode_modulo_shift(a->mode) != 0) {
1317 temp_val = (char*) alloca(sc_get_buffer_length());
1319 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1320 sc_mod(b->value, temp_val, temp_val);
1322 temp_val = (char*)b->value;
1324 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1325 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1328 ir_tarval *tarval_shr_unsigned(ir_tarval *a, unsigned b)
1330 ir_mode *mode = a->mode;
1331 unsigned modulo = get_mode_modulo_shift(mode);
1334 assert((unsigned)(long)b==b);
1335 sc_shrI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1336 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1340 * bitwise signed right shift
1342 ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b)
1344 char *temp_val = NULL;
1346 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1350 if (get_mode_modulo_shift(a->mode) != 0) {
1351 temp_val = (char*) alloca(sc_get_buffer_length());
1353 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1354 sc_mod(b->value, temp_val, temp_val);
1356 temp_val = (char*)b->value;
1358 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1359 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1362 ir_tarval *tarval_shrs_unsigned(ir_tarval *a, unsigned b)
1364 ir_mode *mode = a->mode;
1365 unsigned modulo = get_mode_modulo_shift(mode);
1368 assert((unsigned)(long)b==b);
1369 sc_shrsI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1370 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1374 * bitwise rotation to left
1376 ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b)
1378 char *temp_val = NULL;
1380 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1384 if (get_mode_modulo_shift(a->mode) != 0) {
1385 temp_val = (char*) alloca(sc_get_buffer_length());
1387 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1388 sc_mod(b->value, temp_val, temp_val);
1390 temp_val = (char*)b->value;
1392 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1393 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1397 * carry flag of the last operation
1399 int tarval_carry(void)
1401 if (carry_flag == -1)
1402 panic("Carry undefined for the last operation");
1409 int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
1411 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1415 const tarval_mode_info *mode_info;
1416 const char *prefix, *suffix;
1418 mode_info = (const tarval_mode_info*) tv->mode->tv_priv;
1420 mode_info = &default_info;
1421 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1422 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1424 switch (get_mode_sort(tv->mode)) {
1425 case irms_reference:
1426 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1428 case irms_int_number:
1429 switch (mode_info->mode_output) {
1432 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1436 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1443 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1446 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1448 case irms_float_number:
1449 switch (mode_info->mode_output) {
1451 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1454 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1459 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1462 case irms_internal_boolean:
1463 switch (mode_info->mode_output) {
1469 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1473 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1477 return snprintf(buf, len, "<TV_??""?>");
1482 * Output of tarvals to stdio.
1484 int tarval_printf(ir_tarval *tv)
1489 res = tarval_snprintf(buf, sizeof(buf), tv);
1490 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1495 char *get_tarval_bitpattern(ir_tarval *tv)
1498 int n = get_mode_size_bits(tv->mode);
1499 int bytes = (n + 7) / 8;
1500 char *res = XMALLOCN(char, n + 1);
1503 for (i = 0; i < bytes; i++) {
1504 byte = get_tarval_sub_bits(tv, i);
1505 for (j = 1; j < 256; j <<= 1)
1507 res[pos++] = j & byte ? '1' : '0';
1516 * access to the bitpattern
1518 unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs)
1520 switch (get_mode_arithmetic(tv->mode)) {
1521 case irma_twos_complement:
1522 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1524 case irma_x86_extended_float:
1525 return fc_sub_bits((const fp_value*) tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1527 panic("arithmetic mode not supported");
1532 * Specify the output options of one mode.
1534 * This functions stores the modinfo, so DO NOT DESTROY it.
1536 * Returns zero on success.
1538 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1542 mode->tv_priv = modeinfo;
1547 * Returns the output options of one mode.
1549 * This functions returns the mode info of a given mode.
1551 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1555 return (const tarval_mode_info*) mode->tv_priv;
1559 * Returns non-zero if a given (integer) tarval has only one single bit
1562 int tarval_is_single_bit(ir_tarval *tv)
1567 if (!tv || tv == tarval_bad) return 0;
1568 if (! mode_is_int(tv->mode)) return 0;
1570 l = get_mode_size_bytes(tv->mode);
1571 for (bits = 0, i = l - 1; i >= 0; --i) {
1572 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1574 /* check for more than one bit in these */
1586 * Return the number of set bits in a given (integer) tarval.
1588 int get_tarval_popcount(ir_tarval *tv)
1593 if (!tv || tv == tarval_bad) return -1;
1594 if (! mode_is_int(tv->mode)) return -1;
1596 l = get_mode_size_bytes(tv->mode);
1597 for (bits = 0, i = l - 1; i >= 0; --i) {
1598 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1600 bits += popcount(v);
1606 * Return the number of the lowest set bit in a given (integer) tarval.
1608 * @param tv the tarval
1610 * @return number of lowest set bit or -1 on error
1612 int get_tarval_lowest_bit(ir_tarval *tv)
1616 if (!tv || tv == tarval_bad) return -1;
1617 if (! mode_is_int(tv->mode)) return -1;
1619 l = get_mode_size_bytes(tv->mode);
1620 for (i = 0; i < l; ++i) {
1621 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1624 return ntz(v) + i * 8;
1630 * Returns non-zero if the mantissa of a floating point IEEE-754
1631 * tarval is zero (i.e. 1.0Exxx)
1633 int tarval_zero_mantissa(ir_tarval *tv)
1635 assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1636 || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1637 return fc_zero_mantissa((const fp_value*) tv->value);
1640 /* Returns the exponent of a floating point IEEE-754 tarval. */
1641 int tarval_get_exponent(ir_tarval *tv)
1643 assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1644 || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1645 return fc_get_exponent((const fp_value*) tv->value);
1649 * Check if the tarval can be converted to the given mode without
1652 int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode)
1654 const float_descriptor_t *desc = get_descriptor(mode);
1655 return fc_can_lossless_conv_to((const fp_value*) tv->value, desc);
1658 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1659 unsigned tarval_ieee754_get_exact(void)
1661 return fc_is_exact();
1664 /* check if its the a floating point NaN */
1665 int tarval_is_NaN(ir_tarval *tv)
1667 if (! mode_is_float(tv->mode))
1669 return fc_is_nan((const fp_value*) tv->value);
1672 /* check if its the a floating point +inf */
1673 int tarval_is_plus_inf(ir_tarval *tv)
1675 if (! mode_is_float(tv->mode))
1677 return fc_is_inf((const fp_value*) tv->value)
1678 && !fc_is_negative((const fp_value*) tv->value);
1681 /* check if its the a floating point -inf */
1682 int tarval_is_minus_inf(ir_tarval *tv)
1684 if (! mode_is_float(tv->mode))
1686 return fc_is_inf((const fp_value*) tv->value)
1687 && fc_is_negative((const fp_value*) tv->value);
1690 /* check if the tarval represents a finite value */
1691 int tarval_is_finite(ir_tarval *tv)
1693 if (mode_is_float(tv->mode))
1694 return !fc_is_nan((const fp_value*) tv->value)
1695 && !fc_is_inf((const fp_value*) tv->value);
1700 * Sets the overflow mode for integer operations.
1702 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)
1704 int_overflow_mode = ov_mode;
1707 /* Get the overflow mode for integer operations. */
1708 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void)
1710 return int_overflow_mode;
1713 /* Enable/Disable floating point constant folding. */
1714 void tarval_enable_fp_ops(int enable)
1719 int tarval_fp_ops_enabled(void)
1725 * default mode_info for output as HEX
1727 static const tarval_mode_info hex_output = {
1734 * Initialization of the tarval module: called before init_mode()
1736 void init_tarval_1(long null_value, int support_quad_precision)
1738 /* if these assertion fail, tarval_is_constant() will follow ... */
1739 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1740 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1742 _null_value = null_value;
1744 /* initialize the sets holding the tarvals with a comparison function and
1745 * an initial size, which is the expected number of constants */
1746 tarvals = new_set(cmp_tv, N_CONSTANTS);
1747 values = new_set(memcmp, N_CONSTANTS);
1748 /* calls init_strcalc() with needed size */
1749 init_fltcalc(support_quad_precision ? 112 : 64);
1753 * Initialization of the tarval module: called after init_mode()
1755 void init_tarval_2(void)
1757 tarval_bad->kind = k_tarval;
1758 tarval_bad->mode = mode_BAD;
1759 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1761 tarval_undefined->kind = k_tarval;
1762 tarval_undefined->mode = mode_ANY;
1763 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1765 tarval_b_true->kind = k_tarval;
1766 tarval_b_true->mode = mode_b;
1767 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1769 tarval_b_false->kind = k_tarval;
1770 tarval_b_false->mode = mode_b;
1771 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1773 tarval_unreachable->kind = k_tarval;
1774 tarval_unreachable->mode = mode_X;
1775 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1777 tarval_reachable->kind = k_tarval;
1778 tarval_reachable->mode = mode_X;
1779 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1782 * assign output modes that are compatible with the
1783 * old implementation: Hex output
1785 set_tarval_mode_output_option(mode_Bs, &hex_output);
1786 set_tarval_mode_output_option(mode_Bu, &hex_output);
1787 set_tarval_mode_output_option(mode_Hs, &hex_output);
1788 set_tarval_mode_output_option(mode_Hu, &hex_output);
1789 set_tarval_mode_output_option(mode_Is, &hex_output);
1790 set_tarval_mode_output_option(mode_Iu, &hex_output);
1791 set_tarval_mode_output_option(mode_Ls, &hex_output);
1792 set_tarval_mode_output_option(mode_Lu, &hex_output);
1793 set_tarval_mode_output_option(mode_P, &hex_output);
1796 /* free all memory occupied by tarval. */
1797 void finish_tarval(void)
1801 del_set(tarvals); tarvals = NULL;
1802 del_set(values); values = NULL;
1805 int (is_tarval)(const void *thing)
1807 return _is_tarval(thing);