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 integer overflow mode. */
81 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
83 /****************************************************************************
85 ****************************************************************************/
87 static unsigned hash_val(const void *value, size_t length);
88 static unsigned hash_tv(ir_tarval *tv);
89 static void _fail_verify(ir_tarval *tv, const char* file, int line)
91 /* print a memory image of the tarval and throw an assertion */
93 panic("%s:%d: Invalid tarval: mode: %F\n value: [%p]", file, line, tv->mode, tv->value);
95 panic("%s:%d: Invalid tarval (null)", file, line);
100 __attribute__((unused))
102 void tarval_verify(ir_tarval *tv)
108 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
109 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
111 if (!FIND_TARVAL(tv)) fail_verify(tv);
112 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
116 /** Hash a tarval. */
117 static unsigned hash_tv(ir_tarval *tv)
119 return (unsigned)((PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length);
122 /** Hash a value. Treat it as a byte array. */
123 static unsigned hash_val(const void *value, size_t length)
128 /* scramble the byte - array */
129 for (i = 0; i < length; ++i) {
130 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
131 hash += (hash << 11) ^ (hash >> 17);
137 static int cmp_tv(const void *p1, const void *p2, size_t n)
139 const ir_tarval *tv1 = (const ir_tarval*) p1;
140 const ir_tarval *tv2 = (const ir_tarval*) p2;
143 assert(tv1->kind == k_tarval);
144 assert(tv2->kind == k_tarval);
145 if (tv1->mode < tv2->mode)
147 if (tv1->mode > tv2->mode)
149 if (tv1->length < tv2->length)
151 if (tv1->length > tv2->length)
153 if (tv1->value < tv2->value)
155 if (tv1->value > tv2->value)
161 /** finds tarval with value/mode or creates new tarval */
162 static ir_tarval *get_tarval(const void *value, size_t length, ir_mode *mode)
170 /* if there already is such a value, it is returned, else value
171 * is copied into the set */
172 char *temp = (char*) alloca(length);
173 memcpy(temp, value, length);
174 if (get_mode_arithmetic(mode) == irma_twos_complement) {
175 sign_extend(temp, mode);
177 tv.value = INSERT_VALUE(temp, length);
181 /* if there is such a tarval, it is returned, else tv is copied
183 return INSERT_TARVAL(&tv);
189 static ir_tarval *get_tarval_overflow(const void *value, size_t length, ir_mode *mode)
193 switch (get_mode_sort(mode)) {
195 /* addresses always wrap around */
196 temp = (char*) alloca(sc_get_buffer_length());
197 memcpy(temp, value, sc_get_buffer_length());
198 sc_truncate(get_mode_size_bits(mode), temp);
199 /* the sc_ module expects that all bits are set ... */
200 sign_extend(temp, mode);
201 return get_tarval(temp, length, mode);
203 case irms_int_number:
204 if (sc_comp(value, get_mode_max(mode)->value) == ir_relation_greater) {
205 switch (tarval_get_integer_overflow_mode()) {
206 case TV_OVERFLOW_SATURATE:
207 return get_mode_max(mode);
208 case TV_OVERFLOW_WRAP:
209 temp = (char*) alloca(sc_get_buffer_length());
210 memcpy(temp, value, sc_get_buffer_length());
211 sc_truncate(get_mode_size_bits(mode), temp);
212 /* the sc_ module expects that all bits are set ... */
213 sign_extend(temp, mode);
214 return get_tarval(temp, length, mode);
215 case TV_OVERFLOW_BAD:
218 return get_tarval(value, length, mode);
221 if (sc_comp(value, get_mode_min(mode)->value) == ir_relation_less) {
222 switch (tarval_get_integer_overflow_mode()) {
223 case TV_OVERFLOW_SATURATE:
224 return get_mode_min(mode);
225 case TV_OVERFLOW_WRAP: {
226 temp = (char*) alloca(sc_get_buffer_length());
227 memcpy(temp, value, sc_get_buffer_length());
228 sc_truncate(get_mode_size_bits(mode), temp);
229 return get_tarval(temp, length, mode);
231 case TV_OVERFLOW_BAD:
234 return get_tarval(value, length, mode);
239 case irms_float_number:
245 return get_tarval(value, length, mode);
248 static ir_tarval reserved_tv[2];
249 static ir_tarval nonconst_tvs[4];
251 ir_tarval *tarval_b_false = &reserved_tv[0];
252 ir_tarval *tarval_b_true = &reserved_tv[1];
253 ir_tarval *tarval_bad = &nonconst_tvs[0];
254 ir_tarval *tarval_undefined = &nonconst_tvs[1];
255 ir_tarval *tarval_reachable = &nonconst_tvs[2];
256 ir_tarval *tarval_unreachable = &nonconst_tvs[3];
259 * get the float descriptor for given mode.
261 static const float_descriptor_t *get_descriptor(const ir_mode *mode)
263 return &mode->float_desc;
266 ir_tarval *new_integer_tarval_from_str(const char *str, size_t len, char sign,
267 unsigned char base, ir_mode *mode)
272 buffer = alloca(sc_get_buffer_length());
274 ok = sc_val_from_str(sign, base, str, len, buffer);
278 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
281 static ir_tarval *new_tarval_from_str_int(const char *str, size_t len,
289 /* skip leading spaces */
290 while (len > 0 && str[0] == ' ') {
297 /* 1 sign character allowed */
302 } else if (str[0] == '+') {
307 /* a number starting with '0x' is hexadeciaml,
308 * a number starting with '0' (and at least 1 more char) is octal */
309 if (len >= 2 && str[0] == '0') {
310 if (str[1] == 'x' || str[1] == 'X') {
314 } else if (str[1] == 'b' || str[1] == 'B') {
327 buffer = alloca(sc_get_buffer_length());
329 ok = sc_val_from_str(sign, base, str, len, buffer);
333 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
336 ir_tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
338 const float_descriptor_t *desc;
344 switch (get_mode_sort(mode)) {
345 case irms_internal_boolean:
346 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
347 if (!strcasecmp(str, "true"))
348 return tarval_b_true;
349 else if (!strcasecmp(str, "false"))
350 return tarval_b_false;
352 /* XXX This is C semantics */
353 return atoi(str) ? tarval_b_true : tarval_b_false;
355 case irms_float_number:
356 desc = get_descriptor(mode);
357 fc_val_from_str(str, len, desc, NULL);
358 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
361 if (!strcasecmp(str, "null"))
362 return get_tarval_null(mode);
364 case irms_int_number:
365 return new_tarval_from_str_int(str, len, mode);
367 panic("Unsupported tarval creation with mode %F", mode);
371 ir_tarval *new_tarval_from_long(long l, ir_mode *mode)
375 switch (get_mode_sort(mode)) {
376 case irms_internal_boolean:
377 /* XXX C semantics ! */
378 return l ? tarval_b_true : tarval_b_false ;
381 /* same as integer modes */
382 case irms_int_number:
383 sc_val_from_long(l, NULL);
384 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
386 case irms_float_number:
387 return new_tarval_from_double((long double)l, mode);
390 panic("unsupported mode sort");
394 int tarval_is_long(ir_tarval *tv)
396 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
399 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
400 /* the value might be too big to fit in a long */
401 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
402 if (sc_comp(sc_get_buffer(), tv->value) == ir_relation_less) {
403 /* really doesn't fit */
410 long get_tarval_long(ir_tarval* tv)
412 assert(tarval_is_long(tv) && "tarval too big to fit in long");
414 return sc_val_to_long(tv->value);
417 ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode)
419 const float_descriptor_t *desc;
421 assert(mode && (get_mode_sort(mode) == irms_float_number));
422 desc = get_descriptor(mode);
423 fc_val_from_ieee754(d, desc, NULL);
424 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
427 ir_tarval *new_tarval_from_double(double d, ir_mode *mode)
429 return new_tarval_from_long_double(d, mode);
432 int tarval_is_double(ir_tarval *tv)
436 return (get_mode_sort(tv->mode) == irms_float_number);
439 long double get_tarval_long_double(ir_tarval *tv)
441 assert(tarval_is_double(tv));
443 return fc_val_to_ieee754((const fp_value*) tv->value);
446 double get_tarval_double(ir_tarval *tv)
448 return get_tarval_long_double(tv);
451 ir_mode *(get_tarval_mode)(const ir_tarval *tv)
453 return _get_tarval_mode(tv);
457 * Special value query functions ============================================
459 * These functions calculate and return a tarval representing the requested
461 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
462 * functions, but these are stored on initialization of the irmode module and
463 * therefore the irmode functions should be preferred to the functions below.
466 ir_tarval *(get_tarval_bad)(void)
468 return _get_tarval_bad();
471 ir_tarval *(get_tarval_undefined)(void)
473 return _get_tarval_undefined();
476 ir_tarval *(get_tarval_b_false)(void)
478 return _get_tarval_b_false();
481 ir_tarval *(get_tarval_b_true)(void)
483 return _get_tarval_b_true();
486 ir_tarval *(get_tarval_reachable)(void)
488 return _get_tarval_reachable();
491 ir_tarval *(get_tarval_unreachable)(void)
493 return _get_tarval_unreachable();
496 ir_tarval *get_tarval_max(ir_mode *mode)
498 const float_descriptor_t *desc;
500 switch (get_mode_sort(mode)) {
501 case irms_internal_boolean:
502 return tarval_b_true;
504 case irms_float_number:
505 desc = get_descriptor(mode);
506 fc_get_max(desc, NULL);
507 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
510 case irms_int_number:
511 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
512 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
514 panic("mode %F does not support maximum value", mode);
518 ir_tarval *get_tarval_min(ir_mode *mode)
520 const float_descriptor_t *desc;
522 switch (get_mode_sort(mode)) {
523 case irms_internal_boolean:
524 return tarval_b_false;
526 case irms_float_number:
527 desc = get_descriptor(mode);
528 fc_get_min(desc, NULL);
529 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
532 case irms_int_number:
533 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
534 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
536 panic("mode %F does not support minimum value", mode);
540 /** The bit pattern for the pointer NULL */
541 static long _null_value = 0;
543 ir_tarval *get_tarval_null(ir_mode *mode)
545 switch (get_mode_sort(mode)) {
546 case irms_float_number:
547 return new_tarval_from_double(0.0, mode);
549 case irms_internal_boolean:
550 case irms_int_number:
551 return new_tarval_from_long(0l, mode);
554 return new_tarval_from_long(_null_value, mode);
556 panic("mode %F does not support null value", mode);
560 ir_tarval *get_tarval_one(ir_mode *mode)
562 switch (get_mode_sort(mode)) {
563 case irms_internal_boolean:
564 return tarval_b_true;
566 case irms_float_number:
567 return new_tarval_from_double(1.0, mode);
570 case irms_int_number:
571 return new_tarval_from_long(1l, mode);
573 panic("mode %F does not support one value", mode);
577 ir_tarval *get_tarval_all_one(ir_mode *mode)
579 switch (get_mode_sort(mode)) {
580 case irms_int_number:
581 case irms_internal_boolean:
583 return tarval_not(get_mode_null(mode));
585 case irms_float_number:
586 return new_tarval_from_double(1.0, mode);
589 panic("mode %F does not support all-one value", mode);
593 int tarval_is_constant(ir_tarval *tv)
595 size_t const num_res = ARRAY_SIZE(nonconst_tvs);
596 return tv < &nonconst_tvs[0] || &nonconst_tvs[num_res] <= tv;
599 ir_tarval *get_tarval_minus_one(ir_mode *mode)
601 switch (get_mode_sort(mode)) {
605 case irms_float_number:
606 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
608 case irms_int_number:
609 return new_tarval_from_long(-1l, mode);
612 panic("mode %F does not support minus one value", mode);
616 ir_tarval *get_tarval_nan(ir_mode *mode)
618 const float_descriptor_t *desc;
620 if (get_mode_sort(mode) == irms_float_number) {
621 desc = get_descriptor(mode);
622 fc_get_qnan(desc, NULL);
623 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
625 panic("mode %F does not support NaN value", mode);
628 ir_tarval *get_tarval_plus_inf(ir_mode *mode)
630 if (get_mode_sort(mode) == irms_float_number) {
631 const float_descriptor_t *desc = get_descriptor(mode);
632 fc_get_plusinf(desc, NULL);
633 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
635 panic("mode %F does not support +inf value", mode);
638 ir_tarval *get_tarval_minus_inf(ir_mode *mode)
640 if (get_mode_sort(mode) == irms_float_number) {
641 const float_descriptor_t *desc = get_descriptor(mode);
642 fc_get_minusinf(desc, NULL);
643 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
645 panic("mode %F does not support -inf value", mode);
649 * Arithmetic operations on tarvals ========================================
652 int tarval_is_negative(ir_tarval *a)
654 switch (get_mode_sort(a->mode)) {
655 case irms_int_number:
656 if (!mode_is_signed(a->mode)) return 0;
658 return sc_comp(a->value, get_mode_null(a->mode)->value) == ir_relation_less ? 1 : 0;
660 case irms_float_number:
661 return fc_is_negative((const fp_value*) a->value);
664 panic("mode %F does not support negation value", a->mode);
668 int tarval_is_null(ir_tarval *a)
672 a == get_mode_null(get_tarval_mode(a));
675 int tarval_is_one(ir_tarval *a)
679 a == get_mode_one(get_tarval_mode(a));
682 int tarval_is_all_one(ir_tarval *tv)
686 tv == get_mode_all_one(get_tarval_mode(tv));
689 int tarval_is_minus_one(ir_tarval *a)
693 a == get_mode_minus_one(get_tarval_mode(a));
696 ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b)
698 if (a == tarval_bad || b == tarval_bad) {
699 panic("Comparison with tarval_bad");
702 if (a == tarval_undefined || b == tarval_undefined)
703 return ir_relation_false;
705 if (a->mode != b->mode)
706 return ir_relation_false;
708 /* Here the two tarvals are unequal and of the same mode */
709 switch (get_mode_sort(a->mode)) {
710 case irms_float_number:
712 * BEWARE: we cannot compare a == b here, because
713 * a NaN is always Unordered to any other value, even to itself!
715 return fc_comp((fp_value const*)a->value, (fp_value const*)b->value);
718 case irms_int_number:
720 return ir_relation_equal;
721 return sc_comp(a->value, b->value);
723 case irms_internal_boolean:
725 return ir_relation_equal;
726 return a == tarval_b_true ? ir_relation_greater : ir_relation_less;
729 panic("can't compare values of mode %F", a->mode);
733 ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *dst_mode)
736 fp_value *res = NULL;
737 const float_descriptor_t *desc;
743 if (src->mode == dst_mode)
746 switch (get_mode_sort(src->mode)) {
747 /* cast float to something */
748 case irms_float_number:
749 switch (get_mode_sort(dst_mode)) {
750 case irms_float_number:
751 desc = get_descriptor(dst_mode);
752 fc_cast((const fp_value*) src->value, desc, NULL);
753 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
755 case irms_int_number:
756 res = fc_int((const fp_value*) src->value, NULL);
757 buffer = (char*) alloca(sc_get_buffer_length());
758 if (! fc_flt2int(res, buffer, dst_mode))
760 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
765 /* the rest can't be converted */
768 /* cast int/characters to something */
769 case irms_int_number:
770 switch (get_mode_sort(dst_mode)) {
773 case irms_int_number:
774 buffer = (char*) alloca(sc_get_buffer_length());
775 memcpy(buffer, src->value, sc_get_buffer_length());
776 return get_tarval_overflow(buffer, src->length, dst_mode);
778 case irms_internal_boolean:
779 /* XXX C semantics */
780 if (src == get_mode_null(src->mode)) return tarval_b_false;
781 else return tarval_b_true;
783 case irms_float_number:
784 /* XXX floating point unit does not understand internal integer
785 * representation, convert to string first, then create float from
787 buffer = (char*) alloca(100);
788 /* decimal string representation because hexadecimal output is
789 * interpreted unsigned by fc_val_from_str, so this is a HACK */
790 len = snprintf(buffer, 100, "%s",
791 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
792 buffer[100 - 1] = '\0';
793 desc = get_descriptor(dst_mode);
794 fc_val_from_str(buffer, len, desc, NULL);
795 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
802 case irms_internal_boolean:
803 /* beware: this is C semantic for the INTERNAL boolean mode */
804 if (get_mode_sort(dst_mode) == irms_int_number)
805 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
809 if (get_mode_sort(dst_mode) == irms_int_number) {
810 buffer = (char*) alloca(sc_get_buffer_length());
811 memcpy(buffer, src->value, sc_get_buffer_length());
812 sign_extend(buffer, src->mode);
813 return get_tarval_overflow(buffer, src->length, dst_mode);
823 ir_tarval *tarval_not(ir_tarval *a)
827 switch (get_mode_sort(a->mode)) {
829 case irms_int_number:
830 buffer = (char*) alloca(sc_get_buffer_length());
831 sc_not(a->value, buffer);
832 return get_tarval(buffer, a->length, a->mode);
834 case irms_internal_boolean:
835 if (a == tarval_b_true)
836 return tarval_b_false;
837 if (a == tarval_b_false)
838 return tarval_b_true;
842 panic("bitwise negation is only allowed for integer and boolean");
846 ir_tarval *tarval_neg(ir_tarval *a)
850 assert(mode_is_num(a->mode)); /* negation only for numerical values */
852 /* note: negation is allowed even for unsigned modes. */
854 switch (get_mode_sort(a->mode)) {
855 case irms_int_number:
856 buffer = (char*) alloca(sc_get_buffer_length());
857 sc_neg(a->value, buffer);
858 return get_tarval_overflow(buffer, a->length, a->mode);
860 case irms_float_number:
861 fc_neg((const fp_value*) a->value, NULL);
862 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
869 ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b)
873 if (mode_is_reference(a->mode) && a->mode != b->mode) {
874 b = tarval_convert_to(b, a->mode);
875 } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
876 a = tarval_convert_to(a, b->mode);
879 assert(a->mode == b->mode);
881 switch (get_mode_sort(a->mode)) {
883 case irms_int_number:
884 /* modes of a,b are equal, so result has mode of a as this might be the character */
885 buffer = (char*) alloca(sc_get_buffer_length());
886 sc_add(a->value, b->value, buffer);
887 return get_tarval_overflow(buffer, a->length, a->mode);
889 case irms_float_number:
890 fc_add((const fp_value*) a->value, (const fp_value*) b->value, NULL);
891 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
898 ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode)
902 if (dst_mode != NULL) {
903 if (a->mode != dst_mode)
904 a = tarval_convert_to(a, dst_mode);
905 if (b->mode != dst_mode)
906 b = tarval_convert_to(b, dst_mode);
908 assert(a->mode == b->mode);
910 switch (get_mode_sort(a->mode)) {
912 case irms_int_number:
913 /* modes of a,b are equal, so result has mode of a as this might be the character */
914 buffer = (char*) alloca(sc_get_buffer_length());
915 sc_sub(a->value, b->value, buffer);
916 return get_tarval_overflow(buffer, a->length, a->mode);
918 case irms_float_number:
919 fc_sub((const fp_value*) a->value, (const fp_value*) b->value, NULL);
920 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
927 ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b)
931 assert(a->mode == b->mode);
933 switch (get_mode_sort(a->mode)) {
934 case irms_int_number:
935 /* modes of a,b are equal */
936 buffer = (char*) alloca(sc_get_buffer_length());
937 sc_mul(a->value, b->value, buffer);
938 return get_tarval_overflow(buffer, a->length, a->mode);
940 case irms_float_number:
941 fc_mul((const fp_value*) a->value, (const fp_value*) b->value, NULL);
942 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
949 ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b)
951 ir_mode *mode = a->mode;
952 assert(mode == b->mode);
954 if (mode_is_int(mode)) {
956 if (b == get_mode_null(mode))
959 /* modes of a,b are equal */
960 sc_div(a->value, b->value, NULL);
961 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
963 assert(mode_is_float(mode));
964 fc_div((const fp_value*) a->value, (const fp_value*) b->value, NULL);
965 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), mode);
969 ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b)
971 assert((a->mode == b->mode) && mode_is_int(a->mode));
974 if (b == get_mode_null(b->mode)) return tarval_bad;
975 /* modes of a,b are equal */
976 sc_mod(a->value, b->value, NULL);
977 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
980 ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod)
982 int len = sc_get_buffer_length();
983 char *div_res = (char*) alloca(len);
984 char *mod_res = (char*) alloca(len);
986 assert((a->mode == b->mode) && mode_is_int(a->mode));
989 if (b == get_mode_null(b->mode)) return tarval_bad;
990 /* modes of a,b are equal */
991 sc_divmod(a->value, b->value, div_res, mod_res);
992 *mod = get_tarval(mod_res, len, a->mode);
993 return get_tarval(div_res, len, a->mode);
996 ir_tarval *tarval_abs(ir_tarval *a)
1000 assert(mode_is_num(a->mode));
1002 switch (get_mode_sort(a->mode)) {
1003 case irms_int_number:
1004 if (sc_comp(a->value, get_mode_null(a->mode)->value) == ir_relation_less) {
1005 buffer = (char*) alloca(sc_get_buffer_length());
1006 sc_neg(a->value, buffer);
1007 return get_tarval_overflow(buffer, a->length, a->mode);
1011 case irms_float_number:
1012 if (fc_is_negative((fp_value const*)a)) {
1013 fc_neg((const fp_value*) a->value, NULL);
1014 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1024 ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b)
1026 assert(a->mode == b->mode);
1028 switch (get_mode_sort(a->mode)) {
1029 case irms_internal_boolean:
1030 return (a == tarval_b_false) ? a : b;
1032 case irms_reference:
1033 case irms_int_number:
1034 sc_and(a->value, b->value, NULL);
1035 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1038 panic("operation not defined on mode");
1042 ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b)
1044 assert(a->mode == b->mode);
1046 switch (get_mode_sort(a->mode)) {
1047 case irms_internal_boolean:
1048 return a == tarval_b_true && b == tarval_b_false ? tarval_b_true : tarval_b_false;
1050 case irms_reference:
1051 case irms_int_number:
1052 sc_andnot(a->value, b->value, NULL);
1053 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1056 panic("operation not defined on mode");
1060 ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b)
1062 assert(a->mode == b->mode);
1064 switch (get_mode_sort(a->mode)) {
1065 case irms_internal_boolean:
1066 return (a == tarval_b_true) ? a : b;
1068 case irms_reference:
1069 case irms_int_number:
1070 sc_or(a->value, b->value, NULL);
1071 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1074 panic("operation not defined on mode");
1078 ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b)
1080 assert((a->mode == b->mode));
1082 switch (get_mode_sort(a->mode)) {
1083 case irms_internal_boolean:
1084 return (a == b)? tarval_b_false : tarval_b_true;
1086 case irms_reference:
1087 case irms_int_number:
1088 sc_xor(a->value, b->value, NULL);
1089 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1092 panic("operation not defined on mode");
1096 ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b)
1098 char *temp_val = NULL;
1100 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1102 if (get_mode_modulo_shift(a->mode) != 0) {
1103 temp_val = (char*) alloca(sc_get_buffer_length());
1105 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1106 sc_mod(b->value, temp_val, temp_val);
1108 temp_val = (char*)b->value;
1110 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1111 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1114 ir_tarval *tarval_shl_unsigned(ir_tarval *a, unsigned b)
1116 ir_mode *mode = a->mode;
1117 unsigned modulo = get_mode_modulo_shift(mode);
1120 assert((unsigned)(long)b==b);
1121 sc_shlI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1122 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1125 ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b)
1127 char *temp_val = NULL;
1129 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1131 if (get_mode_modulo_shift(a->mode) != 0) {
1132 temp_val = (char*) alloca(sc_get_buffer_length());
1134 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1135 sc_mod(b->value, temp_val, temp_val);
1137 temp_val = (char*)b->value;
1139 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1140 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1143 ir_tarval *tarval_shr_unsigned(ir_tarval *a, unsigned b)
1145 ir_mode *mode = a->mode;
1146 unsigned modulo = get_mode_modulo_shift(mode);
1149 assert((unsigned)(long)b==b);
1150 sc_shrI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1151 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1154 ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b)
1156 char *temp_val = NULL;
1158 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1160 if (get_mode_modulo_shift(a->mode) != 0) {
1161 temp_val = (char*) alloca(sc_get_buffer_length());
1163 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1164 sc_mod(b->value, temp_val, temp_val);
1166 temp_val = (char*)b->value;
1168 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1169 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1172 ir_tarval *tarval_shrs_unsigned(ir_tarval *a, unsigned b)
1174 ir_mode *mode = a->mode;
1175 unsigned modulo = get_mode_modulo_shift(mode);
1178 assert((unsigned)(long)b==b);
1179 sc_shrsI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1180 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1183 ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b)
1185 char *temp_val = NULL;
1187 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1189 if (get_mode_modulo_shift(a->mode) != 0) {
1190 temp_val = (char*) alloca(sc_get_buffer_length());
1192 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1193 sc_mod(b->value, temp_val, temp_val);
1195 temp_val = (char*)b->value;
1197 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1198 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1201 int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
1203 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1207 const tarval_mode_info *mode_info;
1208 const char *prefix, *suffix;
1210 mode_info = (const tarval_mode_info*) tv->mode->tv_priv;
1212 mode_info = &default_info;
1213 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1214 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1216 switch (get_mode_sort(tv->mode)) {
1217 case irms_reference:
1218 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1220 case irms_int_number:
1221 switch (mode_info->mode_output) {
1224 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1228 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1235 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1238 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1240 case irms_float_number:
1241 switch (mode_info->mode_output) {
1243 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1246 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1251 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1254 case irms_internal_boolean:
1255 switch (mode_info->mode_output) {
1261 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1265 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1269 if (tv == tarval_bad)
1270 return snprintf(buf, len, "<TV_BAD>");
1271 else if (tv == tarval_undefined)
1272 return snprintf(buf, len, "<TV_UNDEFINED>");
1273 else if (tv == tarval_reachable)
1274 return snprintf(buf, len, "<TV_REACHABLE>");
1275 else if (tv == tarval_unreachable)
1276 return snprintf(buf, len, "<TV_UNREACHABLE>");
1278 return snprintf(buf, len, "<TV_??""?>");
1282 int tarval_printf(ir_tarval *tv)
1287 res = tarval_snprintf(buf, sizeof(buf), tv);
1288 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1293 char *get_tarval_bitpattern(ir_tarval *tv)
1296 int n = get_mode_size_bits(tv->mode);
1297 int bytes = (n + 7) / 8;
1298 char *res = XMALLOCN(char, n + 1);
1301 for (i = 0; i < bytes; i++) {
1302 byte = get_tarval_sub_bits(tv, i);
1303 for (j = 1; j < 256; j <<= 1)
1305 res[pos++] = j & byte ? '1' : '0';
1313 unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs)
1315 switch (get_mode_arithmetic(tv->mode)) {
1316 case irma_twos_complement:
1317 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1319 case irma_x86_extended_float:
1320 return fc_sub_bits((const fp_value*) tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1322 panic("arithmetic mode not supported");
1326 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1330 mode->tv_priv = modeinfo;
1334 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1338 return (const tarval_mode_info*) mode->tv_priv;
1341 int tarval_is_single_bit(ir_tarval *tv)
1346 if (!tv || tv == tarval_bad) return 0;
1347 if (! mode_is_int(tv->mode)) return 0;
1349 l = get_mode_size_bytes(tv->mode);
1350 for (bits = 0, i = l - 1; i >= 0; --i) {
1351 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1353 /* check for more than one bit in these */
1364 int get_tarval_popcount(ir_tarval *tv)
1369 if (!tv || tv == tarval_bad) return -1;
1370 if (! mode_is_int(tv->mode)) return -1;
1372 l = get_mode_size_bytes(tv->mode);
1373 for (bits = 0, i = l - 1; i >= 0; --i) {
1374 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1376 bits += popcount(v);
1381 int get_tarval_lowest_bit(ir_tarval *tv)
1385 if (!tv || tv == tarval_bad) return -1;
1386 if (! mode_is_int(tv->mode)) return -1;
1388 l = get_mode_size_bytes(tv->mode);
1389 for (i = 0; i < l; ++i) {
1390 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1393 return ntz(v) + i * 8;
1398 int tarval_zero_mantissa(ir_tarval *tv)
1400 assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1401 || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1402 return fc_zero_mantissa((const fp_value*) tv->value);
1405 int tarval_get_exponent(ir_tarval *tv)
1407 assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1408 || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1409 return fc_get_exponent((const fp_value*) tv->value);
1412 int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode)
1414 const float_descriptor_t *desc = get_descriptor(mode);
1415 return fc_can_lossless_conv_to((const fp_value*) tv->value, desc);
1418 unsigned tarval_ieee754_get_exact(void)
1420 return fc_is_exact();
1423 int tarval_is_NaN(ir_tarval *tv)
1425 if (! mode_is_float(tv->mode))
1427 return fc_is_nan((const fp_value*) tv->value);
1430 int tarval_is_plus_inf(ir_tarval *tv)
1432 if (! mode_is_float(tv->mode))
1434 return fc_is_inf((const fp_value*) tv->value)
1435 && !fc_is_negative((const fp_value*) tv->value);
1438 int tarval_is_minus_inf(ir_tarval *tv)
1440 if (! mode_is_float(tv->mode))
1442 return fc_is_inf((const fp_value*) tv->value)
1443 && fc_is_negative((const fp_value*) tv->value);
1446 int tarval_is_finite(ir_tarval *tv)
1448 if (mode_is_float(tv->mode))
1449 return !fc_is_nan((const fp_value*) tv->value)
1450 && !fc_is_inf((const fp_value*) tv->value);
1454 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)
1456 int_overflow_mode = ov_mode;
1459 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void)
1461 return int_overflow_mode;
1465 * default mode_info for output as HEX
1467 static const tarval_mode_info hex_output = {
1473 void init_tarval_1(long null_value, int support_quad_precision)
1475 _null_value = null_value;
1477 /* initialize the sets holding the tarvals with a comparison function and
1478 * an initial size, which is the expected number of constants */
1479 tarvals = new_set(cmp_tv, N_CONSTANTS);
1480 values = new_set(memcmp, N_CONSTANTS);
1481 /* calls init_strcalc() with needed size */
1482 init_fltcalc(support_quad_precision ? 112 : 64);
1485 void init_tarval_2(void)
1487 tarval_bad->kind = k_tarval;
1488 tarval_bad->mode = mode_BAD;
1490 tarval_undefined->kind = k_tarval;
1491 tarval_undefined->mode = mode_ANY;
1493 tarval_b_true->kind = k_tarval;
1494 tarval_b_true->mode = mode_b;
1496 tarval_b_false->kind = k_tarval;
1497 tarval_b_false->mode = mode_b;
1499 tarval_unreachable->kind = k_tarval;
1500 tarval_unreachable->mode = mode_X;
1502 tarval_reachable->kind = k_tarval;
1503 tarval_reachable->mode = mode_X;
1506 * assign output modes that are compatible with the
1507 * old implementation: Hex output
1509 set_tarval_mode_output_option(mode_Bs, &hex_output);
1510 set_tarval_mode_output_option(mode_Bu, &hex_output);
1511 set_tarval_mode_output_option(mode_Hs, &hex_output);
1512 set_tarval_mode_output_option(mode_Hu, &hex_output);
1513 set_tarval_mode_output_option(mode_Is, &hex_output);
1514 set_tarval_mode_output_option(mode_Iu, &hex_output);
1515 set_tarval_mode_output_option(mode_Ls, &hex_output);
1516 set_tarval_mode_output_option(mode_Lu, &hex_output);
1517 set_tarval_mode_output_option(mode_P, &hex_output);
1520 void finish_tarval(void)
1524 del_set(tarvals); tarvals = NULL;
1525 del_set(values); values = NULL;
1528 int (is_tarval)(const void *thing)
1530 return _is_tarval(thing);