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 /****************************************************************************
67 * local definitions and macros
68 ****************************************************************************/
70 # define TARVAL_VERIFY(a) tarval_verify((a))
72 # define TARVAL_VERIFY(a) ((void)0)
75 #define INSERT_TARVAL(tv) (set_insert(ir_tarval, tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
76 #define FIND_TARVAL(tv) (set_find(ir_tarval, tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
78 #define INSERT_VALUE(val, size) (set_insert(char, values, (val), size, hash_val((val), size)))
79 #define FIND_VALUE(val, size) (set_find(char, values, (val), size, hash_val((val), size)))
81 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
83 /** A set containing all existing tarvals. */
84 static struct set *tarvals = NULL;
85 /** A set containing all existing values. */
86 static struct set *values = NULL;
88 /** The carry flag for SOME operations. -1 means UNDEFINED here */
89 static int carry_flag = -1;
91 /** The integer overflow mode. */
92 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
94 /** if this is set non-zero, the constant folding for floating point is OFF */
95 static int no_float = 0;
97 /****************************************************************************
99 ****************************************************************************/
101 static unsigned hash_val(const void *value, size_t length);
102 static unsigned hash_tv(ir_tarval *tv);
103 static void _fail_verify(ir_tarval *tv, const char* file, int line)
105 /* print a memory image of the tarval and throw an assertion */
107 panic("%s:%d: Invalid tarval: mode: %F\n value: [%p]", file, line, tv->mode, tv->value);
109 panic("%s:%d: Invalid tarval (null)", file, line);
114 __attribute__((unused))
116 void tarval_verify(ir_tarval *tv)
122 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
123 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
125 if (!FIND_TARVAL(tv)) fail_verify(tv);
126 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
130 /** Hash a tarval. */
131 static unsigned hash_tv(ir_tarval *tv)
133 return (unsigned)((PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length);
136 /** Hash a value. Treat it as a byte array. */
137 static unsigned hash_val(const void *value, size_t length)
142 /* scramble the byte - array */
143 for (i = 0; i < length; ++i) {
144 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
145 hash += (hash << 11) ^ (hash >> 17);
151 static int cmp_tv(const void *p1, const void *p2, size_t n)
153 const ir_tarval *tv1 = (const ir_tarval*) p1;
154 const ir_tarval *tv2 = (const ir_tarval*) p2;
157 assert(tv1->kind == k_tarval);
158 assert(tv2->kind == k_tarval);
159 if (tv1->mode < tv2->mode)
161 if (tv1->mode > tv2->mode)
163 if (tv1->length < tv2->length)
165 if (tv1->length > tv2->length)
167 if (tv1->value < tv2->value)
169 if (tv1->value > tv2->value)
175 /** finds tarval with value/mode or creates new tarval */
176 static ir_tarval *get_tarval(const void *value, size_t length, ir_mode *mode)
184 /* if there already is such a value, it is returned, else value
185 * is copied into the set */
186 char *temp = (char*) alloca(length);
187 memcpy(temp, value, length);
188 if (get_mode_arithmetic(mode) == irma_twos_complement) {
189 sign_extend(temp, mode);
191 tv.value = INSERT_VALUE(temp, length);
195 /* if there is such a tarval, it is returned, else tv is copied
197 return INSERT_TARVAL(&tv);
203 static ir_tarval *get_tarval_overflow(const void *value, size_t length, ir_mode *mode)
207 switch (get_mode_sort(mode)) {
209 /* addresses always wrap around */
210 temp = (char*) alloca(sc_get_buffer_length());
211 memcpy(temp, value, sc_get_buffer_length());
212 sc_truncate(get_mode_size_bits(mode), temp);
213 /* the sc_ module expects that all bits are set ... */
214 sign_extend(temp, mode);
215 return get_tarval(temp, length, mode);
217 case irms_int_number:
218 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
219 switch (tarval_get_integer_overflow_mode()) {
220 case TV_OVERFLOW_SATURATE:
221 return get_mode_max(mode);
222 case TV_OVERFLOW_WRAP:
223 temp = (char*) alloca(sc_get_buffer_length());
224 memcpy(temp, value, sc_get_buffer_length());
225 sc_truncate(get_mode_size_bits(mode), temp);
226 /* the sc_ module expects that all bits are set ... */
227 sign_extend(temp, mode);
228 return get_tarval(temp, length, mode);
229 case TV_OVERFLOW_BAD:
232 return get_tarval(value, length, mode);
235 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
236 switch (tarval_get_integer_overflow_mode()) {
237 case TV_OVERFLOW_SATURATE:
238 return get_mode_min(mode);
239 case TV_OVERFLOW_WRAP: {
240 temp = (char*) alloca(sc_get_buffer_length());
241 memcpy(temp, value, sc_get_buffer_length());
242 sc_truncate(get_mode_size_bits(mode), temp);
243 return get_tarval(temp, length, mode);
245 case TV_OVERFLOW_BAD:
248 return get_tarval(value, length, mode);
253 case irms_float_number:
259 return get_tarval(value, length, mode);
263 * public variables declared in tv.h
265 static ir_tarval reserved_tv[6];
267 ir_tarval *tarval_b_false = &reserved_tv[0];
268 ir_tarval *tarval_b_true = &reserved_tv[1];
269 ir_tarval *tarval_bad = &reserved_tv[2];
270 ir_tarval *tarval_undefined = &reserved_tv[3];
271 ir_tarval *tarval_reachable = &reserved_tv[4];
272 ir_tarval *tarval_unreachable = &reserved_tv[5];
275 * get the float descriptor for given mode.
277 static const float_descriptor_t *get_descriptor(const ir_mode *mode)
279 return &mode->float_desc;
282 ir_tarval *new_integer_tarval_from_str(const char *str, size_t len, char sign,
283 unsigned char base, ir_mode *mode)
288 buffer = alloca(sc_get_buffer_length());
290 ok = sc_val_from_str(sign, base, str, len, buffer);
294 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
297 static ir_tarval *new_tarval_from_str_int(const char *str, size_t len,
305 /* skip leading spaces */
306 while (len > 0 && str[0] == ' ') {
313 /* 1 sign character allowed */
318 } else if (str[0] == '+') {
323 /* a number starting with '0x' is hexadeciaml,
324 * a number starting with '0' (and at least 1 more char) is octal */
325 if (len >= 2 && str[0] == '0') {
326 if (str[1] == 'x' || str[1] == 'X') {
330 } else if (str[1] == 'b' || str[1] == 'B') {
343 buffer = alloca(sc_get_buffer_length());
345 ok = sc_val_from_str(sign, base, str, len, buffer);
349 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
353 * Constructors =============================================================
355 ir_tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
357 const float_descriptor_t *desc;
363 switch (get_mode_sort(mode)) {
364 case irms_internal_boolean:
365 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
366 if (!strcasecmp(str, "true"))
367 return tarval_b_true;
368 else if (!strcasecmp(str, "false"))
369 return tarval_b_false;
371 /* XXX This is C semantics */
372 return atoi(str) ? tarval_b_true : tarval_b_false;
374 case irms_float_number:
375 desc = get_descriptor(mode);
376 fc_val_from_str(str, len, desc, NULL);
377 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
380 if (!strcasecmp(str, "null"))
381 return get_tarval_null(mode);
383 case irms_int_number:
384 return new_tarval_from_str_int(str, len, mode);
386 panic("Unsupported tarval creation with mode %F", mode);
391 * helper function, create a tarval from long
393 ir_tarval *new_tarval_from_long(long l, ir_mode *mode)
397 switch (get_mode_sort(mode)) {
398 case irms_internal_boolean:
399 /* XXX C semantics ! */
400 return l ? tarval_b_true : tarval_b_false ;
403 /* same as integer modes */
404 case irms_int_number:
405 sc_val_from_long(l, NULL);
406 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
408 case irms_float_number:
409 return new_tarval_from_double((long double)l, mode);
412 panic("unsupported mode sort");
416 /* returns non-zero if can be converted to long */
417 int tarval_is_long(ir_tarval *tv)
419 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
422 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
423 /* the value might be too big to fit in a long */
424 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
425 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
426 /* really doesn't fit */
433 /* this might overflow the machine's long, so use only with small values */
434 long get_tarval_long(ir_tarval* tv)
436 assert(tarval_is_long(tv) && "tarval too big to fit in long");
438 return sc_val_to_long(tv->value);
441 ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode)
443 const float_descriptor_t *desc;
445 assert(mode && (get_mode_sort(mode) == irms_float_number));
446 desc = get_descriptor(mode);
447 fc_val_from_ieee754(d, desc, NULL);
448 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
451 ir_tarval *new_tarval_from_double(double d, ir_mode *mode)
453 return new_tarval_from_long_double(d, mode);
456 /* returns non-zero if can be converted to double */
457 int tarval_is_double(ir_tarval *tv)
461 return (get_mode_sort(tv->mode) == irms_float_number);
464 long double get_tarval_long_double(ir_tarval *tv)
466 assert(tarval_is_double(tv));
468 return fc_val_to_ieee754((const fp_value*) tv->value);
471 double get_tarval_double(ir_tarval *tv)
473 return get_tarval_long_double(tv);
478 * Access routines for tarval fields ========================================
481 /* get the mode of the tarval */
482 ir_mode *(get_tarval_mode)(const ir_tarval *tv)
484 return _get_tarval_mode(tv);
488 * Special value query functions ============================================
490 * These functions calculate and return a tarval representing the requested
492 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
493 * functions, but these are stored on initialization of the irmode module and
494 * therefore the irmode functions should be preferred to the functions below.
497 ir_tarval *(get_tarval_bad)(void)
499 return _get_tarval_bad();
502 ir_tarval *(get_tarval_undefined)(void)
504 return _get_tarval_undefined();
507 ir_tarval *(get_tarval_b_false)(void)
509 return _get_tarval_b_false();
512 ir_tarval *(get_tarval_b_true)(void)
514 return _get_tarval_b_true();
517 ir_tarval *(get_tarval_reachable)(void)
519 return _get_tarval_reachable();
522 ir_tarval *(get_tarval_unreachable)(void)
524 return _get_tarval_unreachable();
527 ir_tarval *get_tarval_max(ir_mode *mode)
529 const float_descriptor_t *desc;
531 switch (get_mode_sort(mode)) {
532 case irms_internal_boolean:
533 return tarval_b_true;
535 case irms_float_number:
536 desc = get_descriptor(mode);
537 fc_get_max(desc, NULL);
538 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
541 case irms_int_number:
542 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
543 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
545 panic("mode %F does not support maximum value", mode);
549 ir_tarval *get_tarval_min(ir_mode *mode)
551 const float_descriptor_t *desc;
553 switch (get_mode_sort(mode)) {
554 case irms_internal_boolean:
555 return tarval_b_false;
557 case irms_float_number:
558 desc = get_descriptor(mode);
559 fc_get_min(desc, NULL);
560 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
563 case irms_int_number:
564 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
565 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
567 panic("mode %F does not support minimum value", mode);
571 /** The bit pattern for the pointer NULL */
572 static long _null_value = 0;
574 ir_tarval *get_tarval_null(ir_mode *mode)
576 switch (get_mode_sort(mode)) {
577 case irms_float_number:
578 return new_tarval_from_double(0.0, mode);
580 case irms_internal_boolean:
581 case irms_int_number:
582 return new_tarval_from_long(0l, mode);
585 return new_tarval_from_long(_null_value, mode);
587 panic("mode %F does not support null value", mode);
591 ir_tarval *get_tarval_one(ir_mode *mode)
593 switch (get_mode_sort(mode)) {
594 case irms_internal_boolean:
595 return tarval_b_true;
597 case irms_float_number:
598 return new_tarval_from_double(1.0, mode);
601 case irms_int_number:
602 return new_tarval_from_long(1l, mode);
604 panic("mode %F does not support one value", mode);
608 ir_tarval *get_tarval_all_one(ir_mode *mode)
610 switch (get_mode_sort(mode)) {
611 case irms_int_number:
612 case irms_internal_boolean:
614 return tarval_not(get_mode_null(mode));
616 case irms_float_number:
617 return new_tarval_from_double(1.0, mode);
620 panic("mode %F does not support all-one value", mode);
624 int tarval_is_constant(ir_tarval *tv)
626 size_t const num_res = ARRAY_SIZE(reserved_tv);
628 /* reserved tarvals are NOT constants. Note that although
629 tarval_b_true and tarval_b_false are reserved, they are constants of course. */
630 return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
633 ir_tarval *get_tarval_minus_one(ir_mode *mode)
635 switch (get_mode_sort(mode)) {
639 case irms_float_number:
640 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
642 case irms_int_number:
643 return new_tarval_from_long(-1l, mode);
646 panic("mode %F does not support minus one value", mode);
650 ir_tarval *get_tarval_nan(ir_mode *mode)
652 const float_descriptor_t *desc;
654 if (get_mode_sort(mode) == irms_float_number) {
655 desc = get_descriptor(mode);
656 fc_get_qnan(desc, NULL);
657 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
659 panic("mode %F does not support NaN value", mode);
662 ir_tarval *get_tarval_plus_inf(ir_mode *mode)
664 if (get_mode_sort(mode) == irms_float_number) {
665 const float_descriptor_t *desc = get_descriptor(mode);
666 fc_get_plusinf(desc, NULL);
667 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
669 panic("mode %F does not support +inf value", mode);
672 ir_tarval *get_tarval_minus_inf(ir_mode *mode)
674 if (get_mode_sort(mode) == irms_float_number) {
675 const float_descriptor_t *desc = get_descriptor(mode);
676 fc_get_minusinf(desc, NULL);
677 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
679 panic("mode %F does not support -inf value", mode);
683 * Arithmetic operations on tarvals ========================================
687 * test if negative number, 1 means 'yes'
689 int tarval_is_negative(ir_tarval *a)
691 switch (get_mode_sort(a->mode)) {
692 case irms_int_number:
693 if (!mode_is_signed(a->mode)) return 0;
695 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
697 case irms_float_number:
698 return fc_is_negative((const fp_value*) a->value);
701 panic("mode %F does not support negation value", a->mode);
706 * test if null, 1 means 'yes'
708 int tarval_is_null(ir_tarval *a)
712 a == get_mode_null(get_tarval_mode(a));
716 * test if one, 1 means 'yes'
718 int tarval_is_one(ir_tarval *a)
722 a == get_mode_one(get_tarval_mode(a));
725 int tarval_is_all_one(ir_tarval *tv)
729 tv == get_mode_all_one(get_tarval_mode(tv));
733 * test if one, 1 means 'yes'
735 int tarval_is_minus_one(ir_tarval *a)
739 a == get_mode_minus_one(get_tarval_mode(a));
745 ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b)
749 if (a == tarval_bad || b == tarval_bad) {
750 panic("Comparison with tarval_bad");
753 if (a == tarval_undefined || b == tarval_undefined)
754 return ir_relation_false;
756 if (a->mode != b->mode)
757 return ir_relation_false;
759 /* Here the two tarvals are unequal and of the same mode */
760 switch (get_mode_sort(a->mode)) {
761 case irms_float_number:
763 * BEWARE: we cannot compare a == b here, because
764 * a NaN is always Unordered to any other value, even to itself!
766 switch (fc_comp((const fp_value*) a->value, (const fp_value*) b->value)) {
767 case -1: return ir_relation_less;
768 case 0: return ir_relation_equal;
769 case 1: return ir_relation_greater;
770 case 2: return ir_relation_unordered;
771 default: return ir_relation_false;
774 case irms_int_number:
776 return ir_relation_equal;
777 return sc_comp(a->value, b->value) == 1 ? ir_relation_greater : ir_relation_less;
779 case irms_internal_boolean:
781 return ir_relation_equal;
782 return a == tarval_b_true ? ir_relation_greater : ir_relation_less;
785 panic("can't compare values of mode %F", a->mode);
790 * convert to other mode
792 ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *dst_mode)
795 fp_value *res = NULL;
796 const float_descriptor_t *desc;
804 if (src->mode == dst_mode)
807 switch (get_mode_sort(src->mode)) {
808 /* cast float to something */
809 case irms_float_number:
810 switch (get_mode_sort(dst_mode)) {
811 case irms_float_number:
812 desc = get_descriptor(dst_mode);
813 fc_cast((const fp_value*) src->value, desc, NULL);
814 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
816 case irms_int_number:
817 switch (current_float_to_int_mode) {
819 res = fc_int((const fp_value*) src->value, NULL);
822 res = fc_rnd((const fp_value*) src->value, NULL);
825 buffer = (char*) alloca(sc_get_buffer_length());
826 if (! fc_flt2int(res, buffer, dst_mode))
828 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
833 /* the rest can't be converted */
836 /* cast int/characters to something */
837 case irms_int_number:
838 switch (get_mode_sort(dst_mode)) {
841 case irms_int_number:
842 buffer = (char*) alloca(sc_get_buffer_length());
843 memcpy(buffer, src->value, sc_get_buffer_length());
844 return get_tarval_overflow(buffer, src->length, dst_mode);
846 case irms_internal_boolean:
847 /* XXX C semantics */
848 if (src == get_mode_null(src->mode)) return tarval_b_false;
849 else return tarval_b_true;
851 case irms_float_number:
852 /* XXX floating point unit does not understand internal integer
853 * representation, convert to string first, then create float from
855 buffer = (char*) alloca(100);
856 /* decimal string representation because hexadecimal output is
857 * interpreted unsigned by fc_val_from_str, so this is a HACK */
858 len = snprintf(buffer, 100, "%s",
859 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
860 buffer[100 - 1] = '\0';
861 desc = get_descriptor(dst_mode);
862 fc_val_from_str(buffer, len, desc, NULL);
863 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
870 case irms_internal_boolean:
871 /* beware: this is C semantic for the INTERNAL boolean mode */
872 if (get_mode_sort(dst_mode) == irms_int_number)
873 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
877 if (get_mode_sort(dst_mode) == irms_int_number) {
878 buffer = (char*) alloca(sc_get_buffer_length());
879 memcpy(buffer, src->value, sc_get_buffer_length());
880 sign_extend(buffer, src->mode);
881 return get_tarval_overflow(buffer, src->length, dst_mode);
894 ir_tarval *tarval_not(ir_tarval *a)
900 /* works for vector mode without changes */
902 switch (get_mode_sort(a->mode)) {
904 case irms_int_number:
905 buffer = (char*) alloca(sc_get_buffer_length());
906 sc_not(a->value, buffer);
907 return get_tarval(buffer, a->length, a->mode);
909 case irms_internal_boolean:
910 if (a == tarval_b_true)
911 return tarval_b_false;
912 if (a == tarval_b_false)
913 return tarval_b_true;
917 panic("bitwise negation is only allowed for integer and boolean");
922 * arithmetic negation
924 ir_tarval *tarval_neg(ir_tarval *a)
928 assert(mode_is_num(a->mode)); /* negation only for numerical values */
932 /* note: negation is allowed even for unsigned modes. */
934 switch (get_mode_sort(a->mode)) {
935 case irms_int_number:
936 buffer = (char*) alloca(sc_get_buffer_length());
937 sc_neg(a->value, buffer);
938 return get_tarval_overflow(buffer, a->length, a->mode);
940 case irms_float_number:
941 /* it should be safe to enable this even if other arithmetic is disabled */
945 fc_neg((const fp_value*) a->value, NULL);
946 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
956 ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b)
962 if (mode_is_reference(a->mode) && a->mode != b->mode) {
963 b = tarval_convert_to(b, a->mode);
964 } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
965 a = tarval_convert_to(a, b->mode);
968 assert(a->mode == b->mode);
970 switch (get_mode_sort(a->mode)) {
972 case irms_int_number:
973 /* modes of a,b are equal, so result has mode of a as this might be the character */
974 buffer = (char*) alloca(sc_get_buffer_length());
975 sc_add(a->value, b->value, buffer);
976 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
977 return get_tarval_overflow(buffer, a->length, a->mode);
979 case irms_float_number:
983 fc_add((const fp_value*) a->value, (const fp_value*) b->value, NULL);
984 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
994 ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode)
1000 if (dst_mode != NULL) {
1001 if (a->mode != dst_mode)
1002 a = tarval_convert_to(a, dst_mode);
1003 if (b->mode != dst_mode)
1004 b = tarval_convert_to(b, dst_mode);
1006 assert(a->mode == b->mode);
1008 switch (get_mode_sort(a->mode)) {
1009 case irms_reference:
1010 case irms_int_number:
1011 /* modes of a,b are equal, so result has mode of a as this might be the character */
1012 buffer = (char*) alloca(sc_get_buffer_length());
1013 sc_sub(a->value, b->value, buffer);
1014 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1015 return get_tarval_overflow(buffer, a->length, a->mode);
1017 case irms_float_number:
1021 fc_sub((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1022 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1032 ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b)
1036 assert(a->mode == b->mode);
1040 switch (get_mode_sort(a->mode)) {
1041 case irms_int_number:
1042 /* modes of a,b are equal */
1043 buffer = (char*) alloca(sc_get_buffer_length());
1044 sc_mul(a->value, b->value, buffer);
1045 return get_tarval_overflow(buffer, a->length, a->mode);
1047 case irms_float_number:
1051 fc_mul((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1052 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1061 * overflow is impossible, but look out for division by zero
1063 ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b)
1065 ir_mode *mode = a->mode;
1066 assert(mode == b->mode);
1070 if (mode_is_int(mode)) {
1072 if (b == get_mode_null(mode))
1075 /* modes of a,b are equal */
1076 sc_div(a->value, b->value, NULL);
1077 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1079 assert(mode_is_float(mode));
1080 fc_div((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1081 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), mode);
1087 * overflow is impossible, but look out for division by zero
1089 ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b)
1091 assert((a->mode == b->mode) && mode_is_int(a->mode));
1096 if (b == get_mode_null(b->mode)) return tarval_bad;
1097 /* modes of a,b are equal */
1098 sc_mod(a->value, b->value, NULL);
1099 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1103 * integer division AND remainder
1104 * overflow is impossible, but look out for division by zero
1106 ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod)
1108 int len = sc_get_buffer_length();
1109 char *div_res = (char*) alloca(len);
1110 char *mod_res = (char*) alloca(len);
1112 assert((a->mode == b->mode) && mode_is_int(a->mode));
1117 if (b == get_mode_null(b->mode)) return tarval_bad;
1118 /* modes of a,b are equal */
1119 sc_divmod(a->value, b->value, div_res, mod_res);
1120 *mod = get_tarval(mod_res, len, a->mode);
1121 return get_tarval(div_res, len, a->mode);
1127 ir_tarval *tarval_abs(ir_tarval *a)
1132 assert(mode_is_num(a->mode));
1134 switch (get_mode_sort(a->mode)) {
1135 case irms_int_number:
1136 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1137 buffer = (char*) alloca(sc_get_buffer_length());
1138 sc_neg(a->value, buffer);
1139 return get_tarval_overflow(buffer, a->length, a->mode);
1143 case irms_float_number:
1144 /* it should be safe to enable this even if other arithmetic is disabled */
1146 return tarval_bad;*/
1148 if (fc_comp((const fp_value*) a->value,
1149 (const fp_value*) get_mode_null(a->mode)->value) == -1) {
1150 fc_neg((const fp_value*) a->value, NULL);
1151 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1164 ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b)
1166 assert(a->mode == b->mode);
1168 /* works even for vector modes */
1171 switch (get_mode_sort(a->mode)) {
1172 case irms_internal_boolean:
1173 return (a == tarval_b_false) ? a : b;
1175 case irms_reference:
1176 case irms_int_number:
1177 sc_and(a->value, b->value, NULL);
1178 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1181 panic("operation not defined on mode");
1185 ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b)
1187 assert(a->mode == b->mode);
1189 /* works even for vector modes */
1192 switch (get_mode_sort(a->mode)) {
1193 case irms_internal_boolean:
1194 return a == tarval_b_true && b == tarval_b_false ? tarval_b_true : tarval_b_false;
1196 case irms_reference:
1197 case irms_int_number:
1198 sc_andnot(a->value, b->value, NULL);
1199 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1202 panic("operation not defined on mode");
1209 ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b)
1211 assert(a->mode == b->mode);
1213 /* works even for vector modes */
1216 switch (get_mode_sort(a->mode)) {
1217 case irms_internal_boolean:
1218 return (a == tarval_b_true) ? a : b;
1220 case irms_reference:
1221 case irms_int_number:
1222 sc_or(a->value, b->value, NULL);
1223 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1226 panic("operation not defined on mode");
1231 * bitwise exclusive or (xor)
1233 ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b)
1235 assert((a->mode == b->mode));
1237 /* works even for vector modes */
1240 switch (get_mode_sort(a->mode)) {
1241 case irms_internal_boolean:
1242 return (a == b)? tarval_b_false : tarval_b_true;
1244 case irms_reference:
1245 case irms_int_number:
1246 sc_xor(a->value, b->value, NULL);
1247 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1250 panic("operation not defined on mode");
1255 * bitwise left shift
1257 ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b)
1259 char *temp_val = NULL;
1261 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1265 if (get_mode_modulo_shift(a->mode) != 0) {
1266 temp_val = (char*) alloca(sc_get_buffer_length());
1268 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1269 sc_mod(b->value, temp_val, temp_val);
1271 temp_val = (char*)b->value;
1273 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1274 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1277 ir_tarval *tarval_shl_unsigned(ir_tarval *a, unsigned b)
1279 ir_mode *mode = a->mode;
1280 unsigned modulo = get_mode_modulo_shift(mode);
1283 assert((unsigned)(long)b==b);
1284 sc_shlI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1285 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1289 * bitwise unsigned right shift
1291 ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b)
1293 char *temp_val = NULL;
1295 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1299 if (get_mode_modulo_shift(a->mode) != 0) {
1300 temp_val = (char*) alloca(sc_get_buffer_length());
1302 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1303 sc_mod(b->value, temp_val, temp_val);
1305 temp_val = (char*)b->value;
1307 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1308 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1311 ir_tarval *tarval_shr_unsigned(ir_tarval *a, unsigned b)
1313 ir_mode *mode = a->mode;
1314 unsigned modulo = get_mode_modulo_shift(mode);
1317 assert((unsigned)(long)b==b);
1318 sc_shrI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1319 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1323 * bitwise signed right shift
1325 ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b)
1327 char *temp_val = NULL;
1329 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1333 if (get_mode_modulo_shift(a->mode) != 0) {
1334 temp_val = (char*) alloca(sc_get_buffer_length());
1336 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1337 sc_mod(b->value, temp_val, temp_val);
1339 temp_val = (char*)b->value;
1341 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1342 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1345 ir_tarval *tarval_shrs_unsigned(ir_tarval *a, unsigned b)
1347 ir_mode *mode = a->mode;
1348 unsigned modulo = get_mode_modulo_shift(mode);
1351 assert((unsigned)(long)b==b);
1352 sc_shrsI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1353 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1357 * bitwise rotation to left
1359 ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b)
1361 char *temp_val = NULL;
1363 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1367 if (get_mode_modulo_shift(a->mode) != 0) {
1368 temp_val = (char*) alloca(sc_get_buffer_length());
1370 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1371 sc_mod(b->value, temp_val, temp_val);
1373 temp_val = (char*)b->value;
1375 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1376 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1380 * carry flag of the last operation
1382 int tarval_carry(void)
1384 if (carry_flag == -1)
1385 panic("Carry undefined for the last operation");
1392 int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
1394 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1398 const tarval_mode_info *mode_info;
1399 const char *prefix, *suffix;
1401 mode_info = (const tarval_mode_info*) tv->mode->tv_priv;
1403 mode_info = &default_info;
1404 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1405 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1407 switch (get_mode_sort(tv->mode)) {
1408 case irms_reference:
1409 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1411 case irms_int_number:
1412 switch (mode_info->mode_output) {
1415 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1419 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1426 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1429 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1431 case irms_float_number:
1432 switch (mode_info->mode_output) {
1434 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1437 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1442 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1445 case irms_internal_boolean:
1446 switch (mode_info->mode_output) {
1452 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1456 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1460 if (tv == tarval_bad)
1461 return snprintf(buf, len, "<TV_BAD>");
1462 else if (tv == tarval_undefined)
1463 return snprintf(buf, len, "<TV_UNDEFINED>");
1464 else if (tv == tarval_reachable)
1465 return snprintf(buf, len, "<TV_REACHABLE>");
1466 else if (tv == tarval_unreachable)
1467 return snprintf(buf, len, "<TV_UNREACHABLE>");
1469 return snprintf(buf, len, "<TV_??""?>");
1474 * Output of tarvals to stdio.
1476 int tarval_printf(ir_tarval *tv)
1481 res = tarval_snprintf(buf, sizeof(buf), tv);
1482 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1487 char *get_tarval_bitpattern(ir_tarval *tv)
1490 int n = get_mode_size_bits(tv->mode);
1491 int bytes = (n + 7) / 8;
1492 char *res = XMALLOCN(char, n + 1);
1495 for (i = 0; i < bytes; i++) {
1496 byte = get_tarval_sub_bits(tv, i);
1497 for (j = 1; j < 256; j <<= 1)
1499 res[pos++] = j & byte ? '1' : '0';
1508 * access to the bitpattern
1510 unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs)
1512 switch (get_mode_arithmetic(tv->mode)) {
1513 case irma_twos_complement:
1514 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1516 case irma_x86_extended_float:
1517 return fc_sub_bits((const fp_value*) tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1519 panic("arithmetic mode not supported");
1524 * Specify the output options of one mode.
1526 * This functions stores the modinfo, so DO NOT DESTROY it.
1528 * Returns zero on success.
1530 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1534 mode->tv_priv = modeinfo;
1539 * Returns the output options of one mode.
1541 * This functions returns the mode info of a given mode.
1543 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1547 return (const tarval_mode_info*) mode->tv_priv;
1551 * Returns non-zero if a given (integer) tarval has only one single bit
1554 int tarval_is_single_bit(ir_tarval *tv)
1559 if (!tv || tv == tarval_bad) return 0;
1560 if (! mode_is_int(tv->mode)) return 0;
1562 l = get_mode_size_bytes(tv->mode);
1563 for (bits = 0, i = l - 1; i >= 0; --i) {
1564 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1566 /* check for more than one bit in these */
1578 * Return the number of set bits in a given (integer) tarval.
1580 int get_tarval_popcount(ir_tarval *tv)
1585 if (!tv || tv == tarval_bad) return -1;
1586 if (! mode_is_int(tv->mode)) return -1;
1588 l = get_mode_size_bytes(tv->mode);
1589 for (bits = 0, i = l - 1; i >= 0; --i) {
1590 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1592 bits += popcount(v);
1598 * Return the number of the lowest set bit in a given (integer) tarval.
1600 * @param tv the tarval
1602 * @return number of lowest set bit or -1 on error
1604 int get_tarval_lowest_bit(ir_tarval *tv)
1608 if (!tv || tv == tarval_bad) return -1;
1609 if (! mode_is_int(tv->mode)) return -1;
1611 l = get_mode_size_bytes(tv->mode);
1612 for (i = 0; i < l; ++i) {
1613 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1616 return ntz(v) + i * 8;
1622 * Returns non-zero if the mantissa of a floating point IEEE-754
1623 * tarval is zero (i.e. 1.0Exxx)
1625 int tarval_zero_mantissa(ir_tarval *tv)
1627 assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1628 || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1629 return fc_zero_mantissa((const fp_value*) tv->value);
1632 /* Returns the exponent of a floating point IEEE-754 tarval. */
1633 int tarval_get_exponent(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_get_exponent((const fp_value*) tv->value);
1641 * Check if the tarval can be converted to the given mode without
1644 int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode)
1646 const float_descriptor_t *desc = get_descriptor(mode);
1647 return fc_can_lossless_conv_to((const fp_value*) tv->value, desc);
1650 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1651 unsigned tarval_ieee754_get_exact(void)
1653 return fc_is_exact();
1656 /* check if its the a floating point NaN */
1657 int tarval_is_NaN(ir_tarval *tv)
1659 if (! mode_is_float(tv->mode))
1661 return fc_is_nan((const fp_value*) tv->value);
1664 /* check if its the a floating point +inf */
1665 int tarval_is_plus_inf(ir_tarval *tv)
1667 if (! mode_is_float(tv->mode))
1669 return fc_is_inf((const fp_value*) tv->value)
1670 && !fc_is_negative((const fp_value*) tv->value);
1673 /* check if its the a floating point -inf */
1674 int tarval_is_minus_inf(ir_tarval *tv)
1676 if (! mode_is_float(tv->mode))
1678 return fc_is_inf((const fp_value*) tv->value)
1679 && fc_is_negative((const fp_value*) tv->value);
1682 /* check if the tarval represents a finite value */
1683 int tarval_is_finite(ir_tarval *tv)
1685 if (mode_is_float(tv->mode))
1686 return !fc_is_nan((const fp_value*) tv->value)
1687 && !fc_is_inf((const fp_value*) tv->value);
1692 * Sets the overflow mode for integer operations.
1694 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)
1696 int_overflow_mode = ov_mode;
1699 /* Get the overflow mode for integer operations. */
1700 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void)
1702 return int_overflow_mode;
1705 /* Enable/Disable floating point constant folding. */
1706 void tarval_enable_fp_ops(int enable)
1711 int tarval_fp_ops_enabled(void)
1717 * default mode_info for output as HEX
1719 static const tarval_mode_info hex_output = {
1726 * Initialization of the tarval module: called before init_mode()
1728 void init_tarval_1(long null_value, int support_quad_precision)
1730 /* if these assertion fail, tarval_is_constant() will follow ... */
1731 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1732 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1734 _null_value = null_value;
1736 /* initialize the sets holding the tarvals with a comparison function and
1737 * an initial size, which is the expected number of constants */
1738 tarvals = new_set(cmp_tv, N_CONSTANTS);
1739 values = new_set(memcmp, N_CONSTANTS);
1740 /* calls init_strcalc() with needed size */
1741 init_fltcalc(support_quad_precision ? 112 : 64);
1745 * Initialization of the tarval module: called after init_mode()
1747 void init_tarval_2(void)
1749 tarval_bad->kind = k_tarval;
1750 tarval_bad->mode = mode_BAD;
1751 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1753 tarval_undefined->kind = k_tarval;
1754 tarval_undefined->mode = mode_ANY;
1755 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1757 tarval_b_true->kind = k_tarval;
1758 tarval_b_true->mode = mode_b;
1759 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1761 tarval_b_false->kind = k_tarval;
1762 tarval_b_false->mode = mode_b;
1763 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1765 tarval_unreachable->kind = k_tarval;
1766 tarval_unreachable->mode = mode_X;
1767 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1769 tarval_reachable->kind = k_tarval;
1770 tarval_reachable->mode = mode_X;
1771 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1774 * assign output modes that are compatible with the
1775 * old implementation: Hex output
1777 set_tarval_mode_output_option(mode_Bs, &hex_output);
1778 set_tarval_mode_output_option(mode_Bu, &hex_output);
1779 set_tarval_mode_output_option(mode_Hs, &hex_output);
1780 set_tarval_mode_output_option(mode_Hu, &hex_output);
1781 set_tarval_mode_output_option(mode_Is, &hex_output);
1782 set_tarval_mode_output_option(mode_Iu, &hex_output);
1783 set_tarval_mode_output_option(mode_Ls, &hex_output);
1784 set_tarval_mode_output_option(mode_Lu, &hex_output);
1785 set_tarval_mode_output_option(mode_P, &hex_output);
1788 /* free all memory occupied by tarval. */
1789 void finish_tarval(void)
1793 del_set(tarvals); tarvals = NULL;
1794 del_set(values); values = NULL;
1797 int (is_tarval)(const void *thing)
1799 return _is_tarval(thing);