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 /****************************************************************************
59 * local definitions and macros
60 ****************************************************************************/
62 # define TARVAL_VERIFY(a) tarval_verify((a))
64 # define TARVAL_VERIFY(a) ((void)0)
67 #define INSERT_TARVAL(tv) (set_insert(ir_tarval, tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
68 #define FIND_TARVAL(tv) (set_find(ir_tarval, tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
70 #define INSERT_VALUE(val, size) (set_insert(char, values, (val), size, hash_val((val), size)))
71 #define FIND_VALUE(val, size) (set_find(char, values, (val), size, hash_val((val), size)))
73 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
75 /** A set containing all existing tarvals. */
76 static struct set *tarvals = NULL;
77 /** A set containing all existing values. */
78 static struct set *values = NULL;
80 /** The carry flag for SOME operations. -1 means UNDEFINED here */
81 static int carry_flag = -1;
83 /** The integer overflow mode. */
84 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
86 /****************************************************************************
88 ****************************************************************************/
90 static unsigned hash_val(const void *value, size_t length);
91 static unsigned hash_tv(ir_tarval *tv);
92 static void _fail_verify(ir_tarval *tv, const char* file, int line)
94 /* print a memory image of the tarval and throw an assertion */
96 panic("%s:%d: Invalid tarval: mode: %F\n value: [%p]", file, line, tv->mode, tv->value);
98 panic("%s:%d: Invalid tarval (null)", file, line);
103 __attribute__((unused))
105 void tarval_verify(ir_tarval *tv)
111 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
112 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
114 if (!FIND_TARVAL(tv)) fail_verify(tv);
115 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
119 /** Hash a tarval. */
120 static unsigned hash_tv(ir_tarval *tv)
122 return (unsigned)((PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length);
125 /** Hash a value. Treat it as a byte array. */
126 static unsigned hash_val(const void *value, size_t length)
131 /* scramble the byte - array */
132 for (i = 0; i < length; ++i) {
133 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
134 hash += (hash << 11) ^ (hash >> 17);
140 static int cmp_tv(const void *p1, const void *p2, size_t n)
142 const ir_tarval *tv1 = (const ir_tarval*) p1;
143 const ir_tarval *tv2 = (const ir_tarval*) p2;
146 assert(tv1->kind == k_tarval);
147 assert(tv2->kind == k_tarval);
148 if (tv1->mode < tv2->mode)
150 if (tv1->mode > tv2->mode)
152 if (tv1->length < tv2->length)
154 if (tv1->length > tv2->length)
156 if (tv1->value < tv2->value)
158 if (tv1->value > tv2->value)
164 /** finds tarval with value/mode or creates new tarval */
165 static ir_tarval *get_tarval(const void *value, size_t length, ir_mode *mode)
173 /* if there already is such a value, it is returned, else value
174 * is copied into the set */
175 char *temp = (char*) alloca(length);
176 memcpy(temp, value, length);
177 if (get_mode_arithmetic(mode) == irma_twos_complement) {
178 sign_extend(temp, mode);
180 tv.value = INSERT_VALUE(temp, length);
184 /* if there is such a tarval, it is returned, else tv is copied
186 return INSERT_TARVAL(&tv);
192 static ir_tarval *get_tarval_overflow(const void *value, size_t length, ir_mode *mode)
196 switch (get_mode_sort(mode)) {
198 /* addresses always wrap around */
199 temp = (char*) alloca(sc_get_buffer_length());
200 memcpy(temp, value, sc_get_buffer_length());
201 sc_truncate(get_mode_size_bits(mode), temp);
202 /* the sc_ module expects that all bits are set ... */
203 sign_extend(temp, mode);
204 return get_tarval(temp, length, mode);
206 case irms_int_number:
207 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
208 switch (tarval_get_integer_overflow_mode()) {
209 case TV_OVERFLOW_SATURATE:
210 return get_mode_max(mode);
211 case TV_OVERFLOW_WRAP:
212 temp = (char*) alloca(sc_get_buffer_length());
213 memcpy(temp, value, sc_get_buffer_length());
214 sc_truncate(get_mode_size_bits(mode), temp);
215 /* the sc_ module expects that all bits are set ... */
216 sign_extend(temp, mode);
217 return get_tarval(temp, length, mode);
218 case TV_OVERFLOW_BAD:
221 return get_tarval(value, length, mode);
224 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
225 switch (tarval_get_integer_overflow_mode()) {
226 case TV_OVERFLOW_SATURATE:
227 return get_mode_min(mode);
228 case TV_OVERFLOW_WRAP: {
229 temp = (char*) alloca(sc_get_buffer_length());
230 memcpy(temp, value, sc_get_buffer_length());
231 sc_truncate(get_mode_size_bits(mode), temp);
232 return get_tarval(temp, length, mode);
234 case TV_OVERFLOW_BAD:
237 return get_tarval(value, length, mode);
242 case irms_float_number:
248 return get_tarval(value, length, mode);
251 static ir_tarval reserved_tv[2];
252 static ir_tarval nonconst_tvs[4];
254 ir_tarval *tarval_b_false = &reserved_tv[0];
255 ir_tarval *tarval_b_true = &reserved_tv[1];
256 ir_tarval *tarval_bad = &nonconst_tvs[0];
257 ir_tarval *tarval_undefined = &nonconst_tvs[1];
258 ir_tarval *tarval_reachable = &nonconst_tvs[2];
259 ir_tarval *tarval_unreachable = &nonconst_tvs[3];
262 * get the float descriptor for given mode.
264 static const float_descriptor_t *get_descriptor(const ir_mode *mode)
266 return &mode->float_desc;
269 ir_tarval *new_integer_tarval_from_str(const char *str, size_t len, char sign,
270 unsigned char base, ir_mode *mode)
275 buffer = alloca(sc_get_buffer_length());
277 ok = sc_val_from_str(sign, base, str, len, buffer);
281 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
284 static ir_tarval *new_tarval_from_str_int(const char *str, size_t len,
292 /* skip leading spaces */
293 while (len > 0 && str[0] == ' ') {
300 /* 1 sign character allowed */
305 } else if (str[0] == '+') {
310 /* a number starting with '0x' is hexadeciaml,
311 * a number starting with '0' (and at least 1 more char) is octal */
312 if (len >= 2 && str[0] == '0') {
313 if (str[1] == 'x' || str[1] == 'X') {
317 } else if (str[1] == 'b' || str[1] == 'B') {
330 buffer = alloca(sc_get_buffer_length());
332 ok = sc_val_from_str(sign, base, str, len, buffer);
336 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
340 * Constructors =============================================================
342 ir_tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
344 const float_descriptor_t *desc;
350 switch (get_mode_sort(mode)) {
351 case irms_internal_boolean:
352 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
353 if (!strcasecmp(str, "true"))
354 return tarval_b_true;
355 else if (!strcasecmp(str, "false"))
356 return tarval_b_false;
358 /* XXX This is C semantics */
359 return atoi(str) ? tarval_b_true : tarval_b_false;
361 case irms_float_number:
362 desc = get_descriptor(mode);
363 fc_val_from_str(str, len, desc, NULL);
364 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
367 if (!strcasecmp(str, "null"))
368 return get_tarval_null(mode);
370 case irms_int_number:
371 return new_tarval_from_str_int(str, len, mode);
373 panic("Unsupported tarval creation with mode %F", mode);
378 * helper function, create a tarval from long
380 ir_tarval *new_tarval_from_long(long l, ir_mode *mode)
384 switch (get_mode_sort(mode)) {
385 case irms_internal_boolean:
386 /* XXX C semantics ! */
387 return l ? tarval_b_true : tarval_b_false ;
390 /* same as integer modes */
391 case irms_int_number:
392 sc_val_from_long(l, NULL);
393 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
395 case irms_float_number:
396 return new_tarval_from_double((long double)l, mode);
399 panic("unsupported mode sort");
403 /* returns non-zero if can be converted to long */
404 int tarval_is_long(ir_tarval *tv)
406 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
409 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
410 /* the value might be too big to fit in a long */
411 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
412 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
413 /* really doesn't fit */
420 /* this might overflow the machine's long, so use only with small values */
421 long get_tarval_long(ir_tarval* tv)
423 assert(tarval_is_long(tv) && "tarval too big to fit in long");
425 return sc_val_to_long(tv->value);
428 ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode)
430 const float_descriptor_t *desc;
432 assert(mode && (get_mode_sort(mode) == irms_float_number));
433 desc = get_descriptor(mode);
434 fc_val_from_ieee754(d, desc, NULL);
435 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
438 ir_tarval *new_tarval_from_double(double d, ir_mode *mode)
440 return new_tarval_from_long_double(d, mode);
443 /* returns non-zero if can be converted to double */
444 int tarval_is_double(ir_tarval *tv)
448 return (get_mode_sort(tv->mode) == irms_float_number);
451 long double get_tarval_long_double(ir_tarval *tv)
453 assert(tarval_is_double(tv));
455 return fc_val_to_ieee754((const fp_value*) tv->value);
458 double get_tarval_double(ir_tarval *tv)
460 return get_tarval_long_double(tv);
465 * Access routines for tarval fields ========================================
468 /* get the mode of the tarval */
469 ir_mode *(get_tarval_mode)(const ir_tarval *tv)
471 return _get_tarval_mode(tv);
475 * Special value query functions ============================================
477 * These functions calculate and return a tarval representing the requested
479 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
480 * functions, but these are stored on initialization of the irmode module and
481 * therefore the irmode functions should be preferred to the functions below.
484 ir_tarval *(get_tarval_bad)(void)
486 return _get_tarval_bad();
489 ir_tarval *(get_tarval_undefined)(void)
491 return _get_tarval_undefined();
494 ir_tarval *(get_tarval_b_false)(void)
496 return _get_tarval_b_false();
499 ir_tarval *(get_tarval_b_true)(void)
501 return _get_tarval_b_true();
504 ir_tarval *(get_tarval_reachable)(void)
506 return _get_tarval_reachable();
509 ir_tarval *(get_tarval_unreachable)(void)
511 return _get_tarval_unreachable();
514 ir_tarval *get_tarval_max(ir_mode *mode)
516 const float_descriptor_t *desc;
518 switch (get_mode_sort(mode)) {
519 case irms_internal_boolean:
520 return tarval_b_true;
522 case irms_float_number:
523 desc = get_descriptor(mode);
524 fc_get_max(desc, NULL);
525 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
528 case irms_int_number:
529 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
530 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
532 panic("mode %F does not support maximum value", mode);
536 ir_tarval *get_tarval_min(ir_mode *mode)
538 const float_descriptor_t *desc;
540 switch (get_mode_sort(mode)) {
541 case irms_internal_boolean:
542 return tarval_b_false;
544 case irms_float_number:
545 desc = get_descriptor(mode);
546 fc_get_min(desc, NULL);
547 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
550 case irms_int_number:
551 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
552 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
554 panic("mode %F does not support minimum value", mode);
558 /** The bit pattern for the pointer NULL */
559 static long _null_value = 0;
561 ir_tarval *get_tarval_null(ir_mode *mode)
563 switch (get_mode_sort(mode)) {
564 case irms_float_number:
565 return new_tarval_from_double(0.0, mode);
567 case irms_internal_boolean:
568 case irms_int_number:
569 return new_tarval_from_long(0l, mode);
572 return new_tarval_from_long(_null_value, mode);
574 panic("mode %F does not support null value", mode);
578 ir_tarval *get_tarval_one(ir_mode *mode)
580 switch (get_mode_sort(mode)) {
581 case irms_internal_boolean:
582 return tarval_b_true;
584 case irms_float_number:
585 return new_tarval_from_double(1.0, mode);
588 case irms_int_number:
589 return new_tarval_from_long(1l, mode);
591 panic("mode %F does not support one value", mode);
595 ir_tarval *get_tarval_all_one(ir_mode *mode)
597 switch (get_mode_sort(mode)) {
598 case irms_int_number:
599 case irms_internal_boolean:
601 return tarval_not(get_mode_null(mode));
603 case irms_float_number:
604 return new_tarval_from_double(1.0, mode);
607 panic("mode %F does not support all-one value", mode);
611 int tarval_is_constant(ir_tarval *tv)
613 size_t const num_res = ARRAY_SIZE(nonconst_tvs);
614 return tv < &nonconst_tvs[0] || &nonconst_tvs[num_res] <= tv;
617 ir_tarval *get_tarval_minus_one(ir_mode *mode)
619 switch (get_mode_sort(mode)) {
623 case irms_float_number:
624 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
626 case irms_int_number:
627 return new_tarval_from_long(-1l, mode);
630 panic("mode %F does not support minus one value", mode);
634 ir_tarval *get_tarval_nan(ir_mode *mode)
636 const float_descriptor_t *desc;
638 if (get_mode_sort(mode) == irms_float_number) {
639 desc = get_descriptor(mode);
640 fc_get_qnan(desc, NULL);
641 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
643 panic("mode %F does not support NaN value", mode);
646 ir_tarval *get_tarval_plus_inf(ir_mode *mode)
648 if (get_mode_sort(mode) == irms_float_number) {
649 const float_descriptor_t *desc = get_descriptor(mode);
650 fc_get_plusinf(desc, NULL);
651 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
653 panic("mode %F does not support +inf value", mode);
656 ir_tarval *get_tarval_minus_inf(ir_mode *mode)
658 if (get_mode_sort(mode) == irms_float_number) {
659 const float_descriptor_t *desc = get_descriptor(mode);
660 fc_get_minusinf(desc, NULL);
661 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
663 panic("mode %F does not support -inf value", mode);
667 * Arithmetic operations on tarvals ========================================
671 * test if negative number, 1 means 'yes'
673 int tarval_is_negative(ir_tarval *a)
675 switch (get_mode_sort(a->mode)) {
676 case irms_int_number:
677 if (!mode_is_signed(a->mode)) return 0;
679 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
681 case irms_float_number:
682 return fc_is_negative((const fp_value*) a->value);
685 panic("mode %F does not support negation value", a->mode);
690 * test if null, 1 means 'yes'
692 int tarval_is_null(ir_tarval *a)
696 a == get_mode_null(get_tarval_mode(a));
700 * test if one, 1 means 'yes'
702 int tarval_is_one(ir_tarval *a)
706 a == get_mode_one(get_tarval_mode(a));
709 int tarval_is_all_one(ir_tarval *tv)
713 tv == get_mode_all_one(get_tarval_mode(tv));
717 * test if one, 1 means 'yes'
719 int tarval_is_minus_one(ir_tarval *a)
723 a == get_mode_minus_one(get_tarval_mode(a));
729 ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b)
733 if (a == tarval_bad || b == tarval_bad) {
734 panic("Comparison with tarval_bad");
737 if (a == tarval_undefined || b == tarval_undefined)
738 return ir_relation_false;
740 if (a->mode != b->mode)
741 return ir_relation_false;
743 /* Here the two tarvals are unequal and of the same mode */
744 switch (get_mode_sort(a->mode)) {
745 case irms_float_number:
747 * BEWARE: we cannot compare a == b here, because
748 * a NaN is always Unordered to any other value, even to itself!
750 switch (fc_comp((const fp_value*) a->value, (const fp_value*) b->value)) {
751 case -1: return ir_relation_less;
752 case 0: return ir_relation_equal;
753 case 1: return ir_relation_greater;
754 case 2: return ir_relation_unordered;
755 default: return ir_relation_false;
758 case irms_int_number:
760 return ir_relation_equal;
761 return sc_comp(a->value, b->value) == 1 ? ir_relation_greater : ir_relation_less;
763 case irms_internal_boolean:
765 return ir_relation_equal;
766 return a == tarval_b_true ? ir_relation_greater : ir_relation_less;
769 panic("can't compare values of mode %F", a->mode);
774 * convert to other mode
776 ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *dst_mode)
779 fp_value *res = NULL;
780 const float_descriptor_t *desc;
788 if (src->mode == dst_mode)
791 switch (get_mode_sort(src->mode)) {
792 /* cast float to something */
793 case irms_float_number:
794 switch (get_mode_sort(dst_mode)) {
795 case irms_float_number:
796 desc = get_descriptor(dst_mode);
797 fc_cast((const fp_value*) src->value, desc, NULL);
798 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
800 case irms_int_number:
801 res = fc_int((const fp_value*) src->value, NULL);
802 buffer = (char*) alloca(sc_get_buffer_length());
803 if (! fc_flt2int(res, buffer, dst_mode))
805 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
810 /* the rest can't be converted */
813 /* cast int/characters to something */
814 case irms_int_number:
815 switch (get_mode_sort(dst_mode)) {
818 case irms_int_number:
819 buffer = (char*) alloca(sc_get_buffer_length());
820 memcpy(buffer, src->value, sc_get_buffer_length());
821 return get_tarval_overflow(buffer, src->length, dst_mode);
823 case irms_internal_boolean:
824 /* XXX C semantics */
825 if (src == get_mode_null(src->mode)) return tarval_b_false;
826 else return tarval_b_true;
828 case irms_float_number:
829 /* XXX floating point unit does not understand internal integer
830 * representation, convert to string first, then create float from
832 buffer = (char*) alloca(100);
833 /* decimal string representation because hexadecimal output is
834 * interpreted unsigned by fc_val_from_str, so this is a HACK */
835 len = snprintf(buffer, 100, "%s",
836 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
837 buffer[100 - 1] = '\0';
838 desc = get_descriptor(dst_mode);
839 fc_val_from_str(buffer, len, desc, NULL);
840 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
847 case irms_internal_boolean:
848 /* beware: this is C semantic for the INTERNAL boolean mode */
849 if (get_mode_sort(dst_mode) == irms_int_number)
850 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
854 if (get_mode_sort(dst_mode) == irms_int_number) {
855 buffer = (char*) alloca(sc_get_buffer_length());
856 memcpy(buffer, src->value, sc_get_buffer_length());
857 sign_extend(buffer, src->mode);
858 return get_tarval_overflow(buffer, src->length, dst_mode);
871 ir_tarval *tarval_not(ir_tarval *a)
877 /* works for vector mode without changes */
879 switch (get_mode_sort(a->mode)) {
881 case irms_int_number:
882 buffer = (char*) alloca(sc_get_buffer_length());
883 sc_not(a->value, buffer);
884 return get_tarval(buffer, a->length, a->mode);
886 case irms_internal_boolean:
887 if (a == tarval_b_true)
888 return tarval_b_false;
889 if (a == tarval_b_false)
890 return tarval_b_true;
894 panic("bitwise negation is only allowed for integer and boolean");
899 * arithmetic negation
901 ir_tarval *tarval_neg(ir_tarval *a)
905 assert(mode_is_num(a->mode)); /* negation only for numerical values */
909 /* note: negation is allowed even for unsigned modes. */
911 switch (get_mode_sort(a->mode)) {
912 case irms_int_number:
913 buffer = (char*) alloca(sc_get_buffer_length());
914 sc_neg(a->value, buffer);
915 return get_tarval_overflow(buffer, a->length, a->mode);
917 case irms_float_number:
918 fc_neg((const fp_value*) a->value, NULL);
919 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
929 ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b)
935 if (mode_is_reference(a->mode) && a->mode != b->mode) {
936 b = tarval_convert_to(b, a->mode);
937 } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
938 a = tarval_convert_to(a, b->mode);
941 assert(a->mode == b->mode);
943 switch (get_mode_sort(a->mode)) {
945 case irms_int_number:
946 /* modes of a,b are equal, so result has mode of a as this might be the character */
947 buffer = (char*) alloca(sc_get_buffer_length());
948 sc_add(a->value, b->value, buffer);
949 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
950 return get_tarval_overflow(buffer, a->length, a->mode);
952 case irms_float_number:
953 fc_add((const fp_value*) a->value, (const fp_value*) b->value, NULL);
954 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
964 ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode)
970 if (dst_mode != NULL) {
971 if (a->mode != dst_mode)
972 a = tarval_convert_to(a, dst_mode);
973 if (b->mode != dst_mode)
974 b = tarval_convert_to(b, dst_mode);
976 assert(a->mode == b->mode);
978 switch (get_mode_sort(a->mode)) {
980 case irms_int_number:
981 /* modes of a,b are equal, so result has mode of a as this might be the character */
982 buffer = (char*) alloca(sc_get_buffer_length());
983 sc_sub(a->value, b->value, buffer);
984 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
985 return get_tarval_overflow(buffer, a->length, a->mode);
987 case irms_float_number:
988 fc_sub((const fp_value*) a->value, (const fp_value*) b->value, NULL);
989 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
999 ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b)
1003 assert(a->mode == b->mode);
1007 switch (get_mode_sort(a->mode)) {
1008 case irms_int_number:
1009 /* modes of a,b are equal */
1010 buffer = (char*) alloca(sc_get_buffer_length());
1011 sc_mul(a->value, b->value, buffer);
1012 return get_tarval_overflow(buffer, a->length, a->mode);
1014 case irms_float_number:
1015 fc_mul((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1016 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1025 * overflow is impossible, but look out for division by zero
1027 ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b)
1029 ir_mode *mode = a->mode;
1030 assert(mode == b->mode);
1034 if (mode_is_int(mode)) {
1036 if (b == get_mode_null(mode))
1039 /* modes of a,b are equal */
1040 sc_div(a->value, b->value, NULL);
1041 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1043 assert(mode_is_float(mode));
1044 fc_div((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1045 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), mode);
1051 * overflow is impossible, but look out for division by zero
1053 ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b)
1055 assert((a->mode == b->mode) && mode_is_int(a->mode));
1060 if (b == get_mode_null(b->mode)) return tarval_bad;
1061 /* modes of a,b are equal */
1062 sc_mod(a->value, b->value, NULL);
1063 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1067 * integer division AND remainder
1068 * overflow is impossible, but look out for division by zero
1070 ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod)
1072 int len = sc_get_buffer_length();
1073 char *div_res = (char*) alloca(len);
1074 char *mod_res = (char*) alloca(len);
1076 assert((a->mode == b->mode) && mode_is_int(a->mode));
1081 if (b == get_mode_null(b->mode)) return tarval_bad;
1082 /* modes of a,b are equal */
1083 sc_divmod(a->value, b->value, div_res, mod_res);
1084 *mod = get_tarval(mod_res, len, a->mode);
1085 return get_tarval(div_res, len, a->mode);
1091 ir_tarval *tarval_abs(ir_tarval *a)
1096 assert(mode_is_num(a->mode));
1098 switch (get_mode_sort(a->mode)) {
1099 case irms_int_number:
1100 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1101 buffer = (char*) alloca(sc_get_buffer_length());
1102 sc_neg(a->value, buffer);
1103 return get_tarval_overflow(buffer, a->length, a->mode);
1107 case irms_float_number:
1108 if (fc_comp((const fp_value*) a->value,
1109 (const fp_value*) get_mode_null(a->mode)->value) == -1) {
1110 fc_neg((const fp_value*) a->value, NULL);
1111 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1124 ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b)
1126 assert(a->mode == b->mode);
1128 /* works even for vector modes */
1131 switch (get_mode_sort(a->mode)) {
1132 case irms_internal_boolean:
1133 return (a == tarval_b_false) ? a : b;
1135 case irms_reference:
1136 case irms_int_number:
1137 sc_and(a->value, b->value, NULL);
1138 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1141 panic("operation not defined on mode");
1145 ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b)
1147 assert(a->mode == b->mode);
1149 /* works even for vector modes */
1152 switch (get_mode_sort(a->mode)) {
1153 case irms_internal_boolean:
1154 return a == tarval_b_true && b == tarval_b_false ? tarval_b_true : tarval_b_false;
1156 case irms_reference:
1157 case irms_int_number:
1158 sc_andnot(a->value, b->value, NULL);
1159 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1162 panic("operation not defined on mode");
1169 ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b)
1171 assert(a->mode == b->mode);
1173 /* works even for vector modes */
1176 switch (get_mode_sort(a->mode)) {
1177 case irms_internal_boolean:
1178 return (a == tarval_b_true) ? a : b;
1180 case irms_reference:
1181 case irms_int_number:
1182 sc_or(a->value, b->value, NULL);
1183 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1186 panic("operation not defined on mode");
1191 * bitwise exclusive or (xor)
1193 ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b)
1195 assert((a->mode == b->mode));
1197 /* works even for vector modes */
1200 switch (get_mode_sort(a->mode)) {
1201 case irms_internal_boolean:
1202 return (a == b)? tarval_b_false : tarval_b_true;
1204 case irms_reference:
1205 case irms_int_number:
1206 sc_xor(a->value, b->value, NULL);
1207 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1210 panic("operation not defined on mode");
1215 * bitwise left shift
1217 ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b)
1219 char *temp_val = NULL;
1221 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1225 if (get_mode_modulo_shift(a->mode) != 0) {
1226 temp_val = (char*) alloca(sc_get_buffer_length());
1228 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1229 sc_mod(b->value, temp_val, temp_val);
1231 temp_val = (char*)b->value;
1233 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1234 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1237 ir_tarval *tarval_shl_unsigned(ir_tarval *a, unsigned b)
1239 ir_mode *mode = a->mode;
1240 unsigned modulo = get_mode_modulo_shift(mode);
1243 assert((unsigned)(long)b==b);
1244 sc_shlI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1245 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1249 * bitwise unsigned right shift
1251 ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b)
1253 char *temp_val = NULL;
1255 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1259 if (get_mode_modulo_shift(a->mode) != 0) {
1260 temp_val = (char*) alloca(sc_get_buffer_length());
1262 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1263 sc_mod(b->value, temp_val, temp_val);
1265 temp_val = (char*)b->value;
1267 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1268 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1271 ir_tarval *tarval_shr_unsigned(ir_tarval *a, unsigned b)
1273 ir_mode *mode = a->mode;
1274 unsigned modulo = get_mode_modulo_shift(mode);
1277 assert((unsigned)(long)b==b);
1278 sc_shrI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1279 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1283 * bitwise signed right shift
1285 ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b)
1287 char *temp_val = NULL;
1289 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1293 if (get_mode_modulo_shift(a->mode) != 0) {
1294 temp_val = (char*) alloca(sc_get_buffer_length());
1296 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1297 sc_mod(b->value, temp_val, temp_val);
1299 temp_val = (char*)b->value;
1301 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1302 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1305 ir_tarval *tarval_shrs_unsigned(ir_tarval *a, unsigned b)
1307 ir_mode *mode = a->mode;
1308 unsigned modulo = get_mode_modulo_shift(mode);
1311 assert((unsigned)(long)b==b);
1312 sc_shrsI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1313 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1317 * bitwise rotation to left
1319 ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b)
1321 char *temp_val = NULL;
1323 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1327 if (get_mode_modulo_shift(a->mode) != 0) {
1328 temp_val = (char*) alloca(sc_get_buffer_length());
1330 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1331 sc_mod(b->value, temp_val, temp_val);
1333 temp_val = (char*)b->value;
1335 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1336 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1340 * carry flag of the last operation
1342 int tarval_carry(void)
1344 if (carry_flag == -1)
1345 panic("Carry undefined for the last operation");
1352 int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
1354 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1358 const tarval_mode_info *mode_info;
1359 const char *prefix, *suffix;
1361 mode_info = (const tarval_mode_info*) tv->mode->tv_priv;
1363 mode_info = &default_info;
1364 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1365 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1367 switch (get_mode_sort(tv->mode)) {
1368 case irms_reference:
1369 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1371 case irms_int_number:
1372 switch (mode_info->mode_output) {
1375 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1379 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1386 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1389 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1391 case irms_float_number:
1392 switch (mode_info->mode_output) {
1394 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1397 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1402 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1405 case irms_internal_boolean:
1406 switch (mode_info->mode_output) {
1412 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1416 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1420 if (tv == tarval_bad)
1421 return snprintf(buf, len, "<TV_BAD>");
1422 else if (tv == tarval_undefined)
1423 return snprintf(buf, len, "<TV_UNDEFINED>");
1424 else if (tv == tarval_reachable)
1425 return snprintf(buf, len, "<TV_REACHABLE>");
1426 else if (tv == tarval_unreachable)
1427 return snprintf(buf, len, "<TV_UNREACHABLE>");
1429 return snprintf(buf, len, "<TV_??""?>");
1434 * Output of tarvals to stdio.
1436 int tarval_printf(ir_tarval *tv)
1441 res = tarval_snprintf(buf, sizeof(buf), tv);
1442 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1447 char *get_tarval_bitpattern(ir_tarval *tv)
1450 int n = get_mode_size_bits(tv->mode);
1451 int bytes = (n + 7) / 8;
1452 char *res = XMALLOCN(char, n + 1);
1455 for (i = 0; i < bytes; i++) {
1456 byte = get_tarval_sub_bits(tv, i);
1457 for (j = 1; j < 256; j <<= 1)
1459 res[pos++] = j & byte ? '1' : '0';
1468 * access to the bitpattern
1470 unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs)
1472 switch (get_mode_arithmetic(tv->mode)) {
1473 case irma_twos_complement:
1474 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1476 case irma_x86_extended_float:
1477 return fc_sub_bits((const fp_value*) tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1479 panic("arithmetic mode not supported");
1484 * Specify the output options of one mode.
1486 * This functions stores the modinfo, so DO NOT DESTROY it.
1488 * Returns zero on success.
1490 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1494 mode->tv_priv = modeinfo;
1499 * Returns the output options of one mode.
1501 * This functions returns the mode info of a given mode.
1503 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1507 return (const tarval_mode_info*) mode->tv_priv;
1511 * Returns non-zero if a given (integer) tarval has only one single bit
1514 int tarval_is_single_bit(ir_tarval *tv)
1519 if (!tv || tv == tarval_bad) return 0;
1520 if (! mode_is_int(tv->mode)) return 0;
1522 l = get_mode_size_bytes(tv->mode);
1523 for (bits = 0, i = l - 1; i >= 0; --i) {
1524 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1526 /* check for more than one bit in these */
1538 * Return the number of set bits in a given (integer) tarval.
1540 int get_tarval_popcount(ir_tarval *tv)
1545 if (!tv || tv == tarval_bad) return -1;
1546 if (! mode_is_int(tv->mode)) return -1;
1548 l = get_mode_size_bytes(tv->mode);
1549 for (bits = 0, i = l - 1; i >= 0; --i) {
1550 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1552 bits += popcount(v);
1558 * Return the number of the lowest set bit in a given (integer) tarval.
1560 * @param tv the tarval
1562 * @return number of lowest set bit or -1 on error
1564 int get_tarval_lowest_bit(ir_tarval *tv)
1568 if (!tv || tv == tarval_bad) return -1;
1569 if (! mode_is_int(tv->mode)) return -1;
1571 l = get_mode_size_bytes(tv->mode);
1572 for (i = 0; i < l; ++i) {
1573 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1576 return ntz(v) + i * 8;
1582 * Returns non-zero if the mantissa of a floating point IEEE-754
1583 * tarval is zero (i.e. 1.0Exxx)
1585 int tarval_zero_mantissa(ir_tarval *tv)
1587 assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1588 || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1589 return fc_zero_mantissa((const fp_value*) tv->value);
1592 /* Returns the exponent of a floating point IEEE-754 tarval. */
1593 int tarval_get_exponent(ir_tarval *tv)
1595 assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1596 || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1597 return fc_get_exponent((const fp_value*) tv->value);
1601 * Check if the tarval can be converted to the given mode without
1604 int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode)
1606 const float_descriptor_t *desc = get_descriptor(mode);
1607 return fc_can_lossless_conv_to((const fp_value*) tv->value, desc);
1610 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1611 unsigned tarval_ieee754_get_exact(void)
1613 return fc_is_exact();
1616 /* check if its the a floating point NaN */
1617 int tarval_is_NaN(ir_tarval *tv)
1619 if (! mode_is_float(tv->mode))
1621 return fc_is_nan((const fp_value*) tv->value);
1624 /* check if its the a floating point +inf */
1625 int tarval_is_plus_inf(ir_tarval *tv)
1627 if (! mode_is_float(tv->mode))
1629 return fc_is_inf((const fp_value*) tv->value)
1630 && !fc_is_negative((const fp_value*) tv->value);
1633 /* check if its the a floating point -inf */
1634 int tarval_is_minus_inf(ir_tarval *tv)
1636 if (! mode_is_float(tv->mode))
1638 return fc_is_inf((const fp_value*) tv->value)
1639 && fc_is_negative((const fp_value*) tv->value);
1642 /* check if the tarval represents a finite value */
1643 int tarval_is_finite(ir_tarval *tv)
1645 if (mode_is_float(tv->mode))
1646 return !fc_is_nan((const fp_value*) tv->value)
1647 && !fc_is_inf((const fp_value*) tv->value);
1652 * Sets the overflow mode for integer operations.
1654 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)
1656 int_overflow_mode = ov_mode;
1659 /* Get the overflow mode for integer operations. */
1660 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void)
1662 return int_overflow_mode;
1666 * default mode_info for output as HEX
1668 static const tarval_mode_info hex_output = {
1675 * Initialization of the tarval module: called before init_mode()
1677 void init_tarval_1(long null_value, int support_quad_precision)
1679 _null_value = null_value;
1681 /* initialize the sets holding the tarvals with a comparison function and
1682 * an initial size, which is the expected number of constants */
1683 tarvals = new_set(cmp_tv, N_CONSTANTS);
1684 values = new_set(memcmp, N_CONSTANTS);
1685 /* calls init_strcalc() with needed size */
1686 init_fltcalc(support_quad_precision ? 112 : 64);
1690 * Initialization of the tarval module: called after init_mode()
1692 void init_tarval_2(void)
1694 tarval_bad->kind = k_tarval;
1695 tarval_bad->mode = mode_BAD;
1697 tarval_undefined->kind = k_tarval;
1698 tarval_undefined->mode = mode_ANY;
1700 tarval_b_true->kind = k_tarval;
1701 tarval_b_true->mode = mode_b;
1703 tarval_b_false->kind = k_tarval;
1704 tarval_b_false->mode = mode_b;
1706 tarval_unreachable->kind = k_tarval;
1707 tarval_unreachable->mode = mode_X;
1709 tarval_reachable->kind = k_tarval;
1710 tarval_reachable->mode = mode_X;
1713 * assign output modes that are compatible with the
1714 * old implementation: Hex output
1716 set_tarval_mode_output_option(mode_Bs, &hex_output);
1717 set_tarval_mode_output_option(mode_Bu, &hex_output);
1718 set_tarval_mode_output_option(mode_Hs, &hex_output);
1719 set_tarval_mode_output_option(mode_Hu, &hex_output);
1720 set_tarval_mode_output_option(mode_Is, &hex_output);
1721 set_tarval_mode_output_option(mode_Iu, &hex_output);
1722 set_tarval_mode_output_option(mode_Ls, &hex_output);
1723 set_tarval_mode_output_option(mode_Lu, &hex_output);
1724 set_tarval_mode_output_option(mode_P, &hex_output);
1727 /* free all memory occupied by tarval. */
1728 void finish_tarval(void)
1732 del_set(tarvals); tarvals = NULL;
1733 del_set(values); values = NULL;
1736 int (is_tarval)(const void *thing)
1738 return _is_tarval(thing);