2 * Copyright (C) 1995-2008 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
29 * Values are stored in a format depending upon chosen arithmetic
30 * module. Default uses strcalc and fltcalc.
31 * This implementation assumes:
32 * - target has IEEE-754 floating-point arithmetic.
44 #include "bitfiddle.h"
54 #include "firm_common.h"
57 /** Size of hash tables. Should correspond to average number of distinct constant
59 #define N_CONSTANTS 2048
61 /* unused, float to int doesn't work yet */
62 typedef enum float_to_int_mode {
67 static float_to_int_mode current_float_to_int_mode = TRUNCATE;
69 #define SWITCH_NOINFINITY 0
70 #define SWITCH_NODENORMALS 0
72 /****************************************************************************
73 * local definitions and macros
74 ****************************************************************************/
76 # define TARVAL_VERIFY(a) tarval_verify((a))
78 # define TARVAL_VERIFY(a) ((void)0)
81 #define INSERT_TARVAL(tv) ((ir_tarval*)set_insert(tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
82 #define FIND_TARVAL(tv) ((ir_tarval*)set_find(tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
84 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
85 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
87 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
89 /** A set containing all existing tarvals. */
90 static struct set *tarvals = NULL;
91 /** A set containing all existing values. */
92 static struct set *values = NULL;
94 /** The carry flag for SOME operations. -1 means UNDEFINED here */
95 static int carry_flag = -1;
97 /** The integer overflow mode. */
98 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
100 /** if this is set non-zero, the constant folding for floating point is OFF */
101 static int no_float = 0;
103 /** IEEE-754r half precision */
104 static const ieee_descriptor_t half_desc = { 5, 10, 0, NORMAL };
105 /** IEEE-754 single precision */
106 static const ieee_descriptor_t single_desc = { 8, 23, 0, NORMAL };
107 /** IEEE-754 double precision */
108 static const ieee_descriptor_t double_desc = { 11, 52, 0, NORMAL };
109 /** Intel x87 extended precision */
110 static const ieee_descriptor_t extended_desc = { 15, 63, 1, NORMAL };
112 /** IEEE-754r quad precision */
113 static const ieee_descriptor_t quad_desc = { 15, 112, 0, NORMAL };
115 /****************************************************************************
117 ****************************************************************************/
119 static int hash_val(const void *value, unsigned int length);
120 static int hash_tv(ir_tarval *tv);
121 static void _fail_verify(ir_tarval *tv, const char* file, int line)
123 /* print a memory image of the tarval and throw an assertion */
125 panic("%s:%d: Invalid tarval: mode: %F\n value: [%p]", file, line, tv->mode, tv->value);
127 panic("%s:%d: Invalid tarval (null)", file, line);
130 inline static void tarval_verify(ir_tarval *tv) __attribute__ ((unused));
133 inline static void tarval_verify(ir_tarval *tv)
139 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
140 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
142 if (!FIND_TARVAL(tv)) fail_verify(tv);
143 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
147 /** Hash a tarval. */
148 static int hash_tv(ir_tarval *tv)
150 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
153 /** Hash a value. Treat it as a byte array. */
154 static int hash_val(const void *value, unsigned int length)
157 unsigned int hash = 0;
159 /* scramble the byte - array */
160 for (i = 0; i < length; ++i) {
161 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
162 hash += (hash << 11) ^ (hash >> 17);
168 static int cmp_tv(const void *p1, const void *p2, size_t n)
170 const ir_tarval *tv1 = (const ir_tarval*) p1;
171 const ir_tarval *tv2 = (const ir_tarval*) p2;
174 assert(tv1->kind == k_tarval);
175 assert(tv2->kind == k_tarval);
176 if (tv1->mode < tv2->mode)
178 if (tv1->mode > tv2->mode)
180 if (tv1->length < tv2->length)
182 if (tv1->length > tv2->length)
184 if (tv1->value < tv2->value)
186 if (tv1->value > tv2->value)
192 /** finds tarval with value/mode or creates new tarval */
193 static ir_tarval *get_tarval(const void *value, int length, ir_mode *mode)
201 /* if there already is such a value, it is returned, else value
202 * is copied into the set */
203 char *temp = (char*) alloca(length);
204 memcpy(temp, value, length);
205 if (get_mode_arithmetic(mode) == irma_twos_complement) {
206 sign_extend(temp, mode);
208 tv.value = INSERT_VALUE(temp, length);
212 /* if there is such a tarval, it is returned, else tv is copied
214 return (ir_tarval *)INSERT_TARVAL(&tv);
220 static ir_tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
224 switch (get_mode_sort(mode)) {
226 /* addresses always wrap around */
227 temp = (char*) alloca(sc_get_buffer_length());
228 memcpy(temp, value, sc_get_buffer_length());
229 sc_truncate(get_mode_size_bits(mode), temp);
230 /* the sc_ module expects that all bits are set ... */
231 sign_extend(temp, mode);
232 return get_tarval(temp, length, mode);
234 case irms_int_number:
235 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
236 switch (tarval_get_integer_overflow_mode()) {
237 case TV_OVERFLOW_SATURATE:
238 return get_mode_max(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 /* the sc_ module expects that all bits are set ... */
244 sign_extend(temp, mode);
245 return get_tarval(temp, length, mode);
246 case TV_OVERFLOW_BAD:
249 return get_tarval(value, length, mode);
252 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
253 switch (tarval_get_integer_overflow_mode()) {
254 case TV_OVERFLOW_SATURATE:
255 return get_mode_min(mode);
256 case TV_OVERFLOW_WRAP: {
257 char *temp = (char*) alloca(sc_get_buffer_length());
258 memcpy(temp, value, sc_get_buffer_length());
259 sc_truncate(get_mode_size_bits(mode), temp);
260 return get_tarval(temp, length, mode);
262 case TV_OVERFLOW_BAD:
265 return get_tarval(value, length, mode);
270 case irms_float_number:
271 #ifdef SWITCH_NOINFINITY
272 if (fc_is_inf((const fp_value*) value)) {
273 /* clip infinity to maximum value */
274 return fc_is_negative((const fp_value*) value) ? get_mode_min(mode) : get_mode_max(mode);
278 #ifdef SWITCH_NODENORMALS
279 if (fc_is_subnormal((const fp_value*) value)) {
280 /* clip denormals to zero */
281 return get_mode_null(mode);
289 return get_tarval(value, length, mode);
293 * public variables declared in tv.h
295 static ir_tarval reserved_tv[6];
297 ir_tarval *tarval_b_false = &reserved_tv[0];
298 ir_tarval *tarval_b_true = &reserved_tv[1];
299 ir_tarval *tarval_bad = &reserved_tv[2];
300 ir_tarval *tarval_undefined = &reserved_tv[3];
301 ir_tarval *tarval_reachable = &reserved_tv[4];
302 ir_tarval *tarval_unreachable = &reserved_tv[5];
305 * get the float descriptor for given mode.
307 static const ieee_descriptor_t *get_descriptor(const ir_mode *mode)
309 switch (get_mode_size_bits(mode)) {
310 case 16: return &half_desc;
311 case 32: return &single_desc;
312 case 64: return &double_desc;
315 case 128: return &extended_desc; /* FIXME: HACK for x86 where we have
316 sizeof(long double)==16 with 10 byte
318 /* case 128: return &quad_desc; */
320 panic("Unsupported mode in get_descriptor()");
324 ir_tarval *new_integer_tarval_from_str(const char *str, size_t len, char sign,
325 unsigned char base, ir_mode *mode)
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);
339 static ir_tarval *new_tarval_from_str_int(const char *str, size_t len,
347 /* skip leading spaces */
348 while (len > 0 && str[0] == ' ') {
355 /* 1 sign character allowed */
360 } else if (str[0] == '+') {
365 /* a number starting with '0x' is hexadeciaml,
366 * a number starting with '0' (and at least 1 more char) is octal */
367 if (len >= 2 && str[0] == '0') {
368 if (str[1] == 'x' || str[1] == 'X') {
381 buffer = alloca(sc_get_buffer_length());
383 ok = sc_val_from_str(sign, base, str, len, buffer);
387 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
391 * Constructors =============================================================
393 ir_tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
395 const ieee_descriptor_t *desc;
401 switch (get_mode_sort(mode)) {
402 case irms_control_flow:
405 panic("Unsupported tarval creation with mode %F", mode);
407 case irms_internal_boolean:
408 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
409 if (!strcasecmp(str, "true"))
410 return tarval_b_true;
411 else if (!strcasecmp(str, "false"))
412 return tarval_b_false;
414 /* XXX This is C semantics */
415 return atoi(str) ? tarval_b_true : tarval_b_false;
417 case irms_float_number:
418 desc = get_descriptor(mode);
419 fc_val_from_str(str, len, desc, NULL);
420 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
423 if (!strcasecmp(str, "null"))
424 return get_tarval_null(mode);
426 case irms_int_number:
427 return new_tarval_from_str_int(str, len, mode);
429 panic("Unsupported tarval creation with mode %F", mode);
433 * helper function, create a tarval from long
435 ir_tarval *new_tarval_from_long(long l, ir_mode *mode)
439 switch (get_mode_sort(mode)) {
440 case irms_internal_boolean:
441 /* XXX C semantics ! */
442 return l ? tarval_b_true : tarval_b_false ;
445 /* same as integer modes */
446 case irms_int_number:
447 sc_val_from_long(l, NULL);
448 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
450 case irms_float_number:
451 return new_tarval_from_double((long double)l, mode);
454 panic("unsupported mode sort");
458 /* returns non-zero if can be converted to long */
459 int tarval_is_long(ir_tarval *tv)
461 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
464 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
465 /* the value might be too big to fit in a long */
466 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
467 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
468 /* really doesn't fit */
475 /* this might overflow the machine's long, so use only with small values */
476 long get_tarval_long(ir_tarval* tv)
478 assert(tarval_is_long(tv) && "tarval too big to fit in long");
480 return sc_val_to_long(tv->value);
483 ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode)
485 const ieee_descriptor_t *desc;
487 assert(mode && (get_mode_sort(mode) == irms_float_number));
488 desc = get_descriptor(mode);
489 fc_val_from_ieee754(d, desc, NULL);
490 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
493 ir_tarval *new_tarval_from_double(double d, ir_mode *mode)
495 return new_tarval_from_long_double(d, mode);
498 /* returns non-zero if can be converted to double */
499 int tarval_is_double(ir_tarval *tv)
503 return (get_mode_sort(tv->mode) == irms_float_number);
506 long double get_tarval_long_double(ir_tarval *tv)
508 assert(tarval_is_double(tv));
510 return fc_val_to_ieee754((const fp_value*) tv->value);
513 double get_tarval_double(ir_tarval *tv)
515 return get_tarval_long_double(tv);
520 * Access routines for tarval fields ========================================
523 /* get the mode of the tarval */
524 ir_mode *(get_tarval_mode)(const ir_tarval *tv)
526 return _get_tarval_mode(tv);
530 * Special value query functions ============================================
532 * These functions calculate and return a tarval representing the requested
534 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
535 * functions, but these are stored on initialization of the irmode module and
536 * therefore the irmode functions should be preferred to the functions below.
539 ir_tarval *(get_tarval_bad)(void)
541 return _get_tarval_bad();
544 ir_tarval *(get_tarval_undefined)(void)
546 return _get_tarval_undefined();
549 ir_tarval *(get_tarval_b_false)(void)
551 return _get_tarval_b_false();
554 ir_tarval *(get_tarval_b_true)(void)
556 return _get_tarval_b_true();
559 ir_tarval *(get_tarval_reachable)(void)
561 return _get_tarval_reachable();
564 ir_tarval *(get_tarval_unreachable)(void)
566 return _get_tarval_unreachable();
569 ir_tarval *get_tarval_max(ir_mode *mode)
571 const ieee_descriptor_t *desc;
574 if (get_mode_n_vector_elems(mode) > 1) {
575 /* vector arithmetic not implemented yet */
579 switch (get_mode_sort(mode)) {
580 case irms_control_flow:
583 panic("mode %F does not support maximum value", mode);
585 case irms_internal_boolean:
586 return tarval_b_true;
588 case irms_float_number:
589 desc = get_descriptor(mode);
590 fc_get_max(desc, NULL);
591 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
594 case irms_int_number:
595 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
596 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
601 ir_tarval *get_tarval_min(ir_mode *mode)
603 const ieee_descriptor_t *desc;
606 if (get_mode_n_vector_elems(mode) > 1) {
607 /* vector arithmetic not implemented yet */
611 switch (get_mode_sort(mode)) {
612 case irms_control_flow:
615 panic("mode %F does not support minimum value", mode);
617 case irms_internal_boolean:
618 return tarval_b_false;
620 case irms_float_number:
621 desc = get_descriptor(mode);
622 fc_get_min(desc, NULL);
623 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
626 case irms_int_number:
627 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
628 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
633 /** The bit pattern for the pointer NULL */
634 static long _null_value = 0;
636 ir_tarval *get_tarval_null(ir_mode *mode)
640 if (get_mode_n_vector_elems(mode) > 1) {
641 /* vector arithmetic not implemented yet */
645 switch (get_mode_sort(mode)) {
646 case irms_control_flow:
649 panic("mode %F does not support null value", mode);
651 case irms_float_number:
652 return new_tarval_from_double(0.0, mode);
654 case irms_internal_boolean:
655 case irms_int_number:
656 return new_tarval_from_long(0l, mode);
659 return new_tarval_from_long(_null_value, mode);
664 ir_tarval *get_tarval_one(ir_mode *mode)
668 if (get_mode_n_vector_elems(mode) > 1)
669 panic("vector arithmetic not implemented yet");
671 switch (get_mode_sort(mode)) {
672 case irms_control_flow:
675 panic("mode %F does not support one value", mode);
677 case irms_internal_boolean:
678 return tarval_b_true;
680 case irms_float_number:
681 return new_tarval_from_double(1.0, mode);
684 case irms_int_number:
685 return new_tarval_from_long(1l, mode);
690 ir_tarval *get_tarval_all_one(ir_mode *mode)
694 if (get_mode_n_vector_elems(mode) > 1)
695 panic("vector arithmetic not implemented yet");
697 switch (get_mode_sort(mode)) {
698 case irms_control_flow:
701 panic("mode %F does not support all-one value", mode);
703 case irms_int_number:
704 case irms_internal_boolean:
706 return tarval_not(get_mode_null(mode));
709 case irms_float_number:
710 return new_tarval_from_double(1.0, mode);
715 int tarval_is_constant(ir_tarval *tv)
717 int num_res = sizeof(reserved_tv) / sizeof(reserved_tv[0]);
719 /* reserved tarvals are NOT constants. Note that although
720 tarval_b_true and tarval_b_false are reserved, they are constants of course. */
721 return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
724 ir_tarval *get_tarval_minus_one(ir_mode *mode)
728 if (get_mode_n_vector_elems(mode) > 1)
729 panic("vector arithmetic not implemented yet");
731 switch (get_mode_sort(mode)) {
732 case irms_control_flow:
735 case irms_internal_boolean:
736 panic("mode %F does not support minus one value", mode);
741 case irms_float_number:
742 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
744 case irms_int_number:
745 return new_tarval_from_long(-1l, mode);
750 ir_tarval *get_tarval_nan(ir_mode *mode)
752 const ieee_descriptor_t *desc;
755 if (get_mode_n_vector_elems(mode) > 1)
756 panic("vector arithmetic not implemented yet");
758 if (get_mode_sort(mode) == irms_float_number) {
759 desc = get_descriptor(mode);
760 fc_get_qnan(desc, NULL);
761 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
763 panic("mode %F does not support NaN value", mode);
766 ir_tarval *get_tarval_plus_inf(ir_mode *mode)
769 if (get_mode_n_vector_elems(mode) > 1)
770 panic("vector arithmetic not implemented yet");
772 if (get_mode_sort(mode) == irms_float_number) {
773 const ieee_descriptor_t *desc = get_descriptor(mode);
774 fc_get_plusinf(desc, NULL);
775 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
777 panic("mode %F does not support +inf value", mode);
780 ir_tarval *get_tarval_minus_inf(ir_mode *mode)
784 if (get_mode_n_vector_elems(mode) > 1)
785 panic("vector arithmetic not implemented yet");
787 if (get_mode_sort(mode) == irms_float_number) {
788 const ieee_descriptor_t *desc = get_descriptor(mode);
789 fc_get_minusinf(desc, NULL);
790 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
792 panic("mode %F does not support -inf value", mode);
796 * Arithmetic operations on tarvals ========================================
800 * test if negative number, 1 means 'yes'
802 int tarval_is_negative(ir_tarval *a)
804 if (get_mode_n_vector_elems(a->mode) > 1)
805 panic("vector arithmetic not implemented yet");
807 switch (get_mode_sort(a->mode)) {
808 case irms_int_number:
809 if (!mode_is_signed(a->mode)) return 0;
811 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
813 case irms_float_number:
814 return fc_is_negative((const fp_value*) a->value);
817 panic("mode %F does not support negation value", a->mode);
822 * test if null, 1 means 'yes'
824 int tarval_is_null(ir_tarval *a)
828 a == get_mode_null(get_tarval_mode(a));
832 * test if one, 1 means 'yes'
834 int tarval_is_one(ir_tarval *a)
838 a == get_mode_one(get_tarval_mode(a));
841 int tarval_is_all_one(ir_tarval *tv)
845 tv == get_mode_all_one(get_tarval_mode(tv));
849 * test if one, 1 means 'yes'
851 int tarval_is_minus_one(ir_tarval *a)
855 a == get_mode_minus_one(get_tarval_mode(a));
861 pn_Cmp tarval_cmp(ir_tarval *a, ir_tarval *b)
865 if (a == tarval_bad || b == tarval_bad) {
866 panic("Comparison with tarval_bad");
869 if (a == tarval_undefined || b == tarval_undefined)
872 if (a->mode != b->mode)
875 if (get_mode_n_vector_elems(a->mode) > 1) {
876 /* vector arithmetic not implemented yet */
877 panic("cmp not implemented for vector modes");
880 /* Here the two tarvals are unequal and of the same mode */
881 switch (get_mode_sort(a->mode)) {
882 case irms_control_flow:
889 case irms_float_number:
890 /* it should be safe to enable this even if other arithmetic is disabled */
892 return pn_Cmp_False;*/
894 * BEWARE: we cannot compare a == b here, because
895 * a NaN is always Unordered to any other value, even to itself!
897 switch (fc_comp((const fp_value*) a->value, (const fp_value*) b->value)) {
898 case -1: return pn_Cmp_Lt;
899 case 0: return pn_Cmp_Eq;
900 case 1: return pn_Cmp_Gt;
901 case 2: return pn_Cmp_Uo;
902 default: return pn_Cmp_False;
905 case irms_int_number:
908 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
910 case irms_internal_boolean:
913 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
919 * convert to other mode
921 ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *dst_mode)
924 fp_value *res = NULL;
925 const ieee_descriptor_t *desc;
933 if (src->mode == dst_mode)
936 if (get_mode_n_vector_elems(src->mode) > 1) {
937 /* vector arithmetic not implemented yet */
941 switch (get_mode_sort(src->mode)) {
942 case irms_control_flow:
947 /* cast float to something */
948 case irms_float_number:
949 switch (get_mode_sort(dst_mode)) {
950 case irms_float_number:
951 desc = get_descriptor(dst_mode);
952 fc_cast((const fp_value*) src->value, desc, NULL);
953 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
955 case irms_int_number:
956 switch (current_float_to_int_mode) {
958 res = fc_int((const fp_value*) src->value, NULL);
961 res = fc_rnd((const fp_value*) src->value, NULL);
964 buffer = (char*) alloca(sc_get_buffer_length());
965 if (! fc_flt2int(res, buffer, dst_mode))
967 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
970 /* the rest can't be converted */
975 /* cast int/characters to something */
976 case irms_int_number:
977 switch (get_mode_sort(dst_mode)) {
980 case irms_int_number:
981 buffer = (char*) alloca(sc_get_buffer_length());
982 memcpy(buffer, src->value, sc_get_buffer_length());
983 sign_extend(buffer, dst_mode);
984 return get_tarval_overflow(buffer, src->length, dst_mode);
986 case irms_internal_boolean:
987 /* XXX C semantics */
988 if (src == get_mode_null(src->mode)) return tarval_b_false;
989 else return tarval_b_true;
991 case irms_float_number:
992 /* XXX floating point unit does not understand internal integer
993 * representation, convert to string first, then create float from
995 buffer = (char*) alloca(100);
996 /* decimal string representation because hexadecimal output is
997 * interpreted unsigned by fc_val_from_str, so this is a HACK */
998 len = snprintf(buffer, 100, "%s",
999 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
1000 buffer[100 - 1] = '\0';
1001 desc = get_descriptor(dst_mode);
1002 fc_val_from_str(buffer, len, desc, NULL);
1003 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
1010 case irms_internal_boolean:
1011 /* beware: this is C semantic for the INTERNAL boolean mode */
1012 if (get_mode_sort(dst_mode) == irms_int_number)
1013 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
1016 case irms_reference:
1017 if (get_mode_sort(dst_mode) == irms_int_number) {
1018 buffer = (char*) alloca(sc_get_buffer_length());
1019 memcpy(buffer, src->value, sc_get_buffer_length());
1020 sign_extend(buffer, src->mode);
1021 return get_tarval_overflow(buffer, src->length, dst_mode);
1032 ir_tarval *tarval_not(ir_tarval *a)
1038 /* works for vector mode without changes */
1040 switch (get_mode_sort(a->mode)) {
1041 case irms_reference:
1042 case irms_int_number:
1043 buffer = (char*) alloca(sc_get_buffer_length());
1044 sc_not(a->value, buffer);
1045 return get_tarval(buffer, a->length, a->mode);
1047 case irms_internal_boolean:
1048 if (a == tarval_b_true)
1049 return tarval_b_false;
1050 if (a == tarval_b_false)
1051 return tarval_b_true;
1055 panic("bitwise negation is only allowed for integer and boolean");
1060 * arithmetic negation
1062 ir_tarval *tarval_neg(ir_tarval *a)
1066 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1070 /* note: negation is allowed even for unsigned modes. */
1072 if (get_mode_n_vector_elems(a->mode) > 1) {
1073 /* vector arithmetic not implemented yet */
1077 switch (get_mode_sort(a->mode)) {
1078 case irms_int_number:
1079 buffer = (char*) alloca(sc_get_buffer_length());
1080 sc_neg(a->value, buffer);
1081 return get_tarval_overflow(buffer, a->length, a->mode);
1083 case irms_float_number:
1084 /* it should be safe to enable this even if other arithmetic is disabled */
1086 return tarval_bad;*/
1088 fc_neg((const fp_value*) a->value, NULL);
1089 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1099 ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b)
1105 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1106 /* vector arithmetic not implemented yet */
1110 if (mode_is_reference(a->mode) && a->mode != b->mode) {
1111 b = tarval_convert_to(b, a->mode);
1112 } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
1113 a = tarval_convert_to(a, b->mode);
1116 assert(a->mode == b->mode);
1118 switch (get_mode_sort(a->mode)) {
1119 case irms_reference:
1120 case irms_int_number:
1121 /* modes of a,b are equal, so result has mode of a as this might be the character */
1122 buffer = (char*) alloca(sc_get_buffer_length());
1123 sc_add(a->value, b->value, buffer);
1124 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1125 return get_tarval_overflow(buffer, a->length, a->mode);
1127 case irms_float_number:
1131 fc_add((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1132 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1142 ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode)
1148 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1149 /* vector arithmetic not implemented yet */
1153 if (dst_mode != NULL) {
1154 if (a->mode != dst_mode)
1155 a = tarval_convert_to(a, dst_mode);
1156 if (b->mode != dst_mode)
1157 b = tarval_convert_to(b, dst_mode);
1159 assert(a->mode == b->mode);
1161 switch (get_mode_sort(a->mode)) {
1162 case irms_reference:
1163 case irms_int_number:
1164 /* modes of a,b are equal, so result has mode of a as this might be the character */
1165 buffer = (char*) alloca(sc_get_buffer_length());
1166 sc_sub(a->value, b->value, buffer);
1167 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1168 return get_tarval_overflow(buffer, a->length, a->mode);
1170 case irms_float_number:
1174 fc_sub((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1175 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1185 ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b)
1189 assert(a->mode == b->mode);
1193 if (get_mode_n_vector_elems(a->mode) > 1) {
1194 /* vector arithmetic not implemented yet */
1198 switch (get_mode_sort(a->mode)) {
1199 case irms_int_number:
1200 /* modes of a,b are equal */
1201 buffer = (char*) alloca(sc_get_buffer_length());
1202 sc_mul(a->value, b->value, buffer);
1203 return get_tarval_overflow(buffer, a->length, a->mode);
1205 case irms_float_number:
1209 fc_mul((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1210 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1218 * floating point division
1220 ir_tarval *tarval_quo(ir_tarval *a, ir_tarval *b)
1222 assert((a->mode == b->mode) && mode_is_float(a->mode));
1229 if (get_mode_n_vector_elems(a->mode) > 1) {
1230 /* vector arithmetic not implemented yet */
1234 fc_div((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1235 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1240 * overflow is impossible, but look out for division by zero
1242 ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b)
1244 assert((a->mode == b->mode) && mode_is_int(a->mode));
1248 if (get_mode_n_vector_elems(a->mode) > 1) {
1249 /* vector arithmetic not implemented yet */
1254 if (b == get_mode_null(b->mode)) return tarval_bad;
1255 /* modes of a,b are equal */
1256 sc_div(a->value, b->value, NULL);
1257 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1262 * overflow is impossible, but look out for division by zero
1264 ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b)
1266 assert((a->mode == b->mode) && mode_is_int(a->mode));
1270 if (get_mode_n_vector_elems(a->mode) > 1) {
1271 /* vector arithmetic not implemented yet */
1276 if (b == get_mode_null(b->mode)) return tarval_bad;
1277 /* modes of a,b are equal */
1278 sc_mod(a->value, b->value, NULL);
1279 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1283 * integer division AND remainder
1284 * overflow is impossible, but look out for division by zero
1286 ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod)
1288 int len = sc_get_buffer_length();
1289 char *div_res = (char*) alloca(len);
1290 char *mod_res = (char*) alloca(len);
1292 assert((a->mode == b->mode) && mode_is_int(a->mode));
1296 if (get_mode_n_vector_elems(a->mode) > 1) {
1297 /* vector arithmetic not implemented yet */
1303 if (b == get_mode_null(b->mode)) return tarval_bad;
1304 /* modes of a,b are equal */
1305 sc_divmod(a->value, b->value, div_res, mod_res);
1306 *mod = get_tarval(mod_res, len, a->mode);
1307 return get_tarval(div_res, len, a->mode);
1313 ir_tarval *tarval_abs(ir_tarval *a)
1318 assert(mode_is_num(a->mode));
1320 if (get_mode_n_vector_elems(a->mode) > 1) {
1321 /* vector arithmetic not implemented yet */
1325 switch (get_mode_sort(a->mode)) {
1326 case irms_int_number:
1327 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1328 buffer = (char*) alloca(sc_get_buffer_length());
1329 sc_neg(a->value, buffer);
1330 return get_tarval_overflow(buffer, a->length, a->mode);
1334 case irms_float_number:
1335 /* it should be safe to enable this even if other arithmetic is disabled */
1337 return tarval_bad;*/
1339 if (fc_comp((const fp_value*) a->value,
1340 (const fp_value*) get_mode_null(a->mode)->value) == -1) {
1341 fc_neg((const fp_value*) a->value, NULL);
1342 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1355 ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b)
1357 assert(a->mode == b->mode);
1359 /* works even for vector modes */
1362 switch (get_mode_sort(a->mode)) {
1363 case irms_internal_boolean:
1364 return (a == tarval_b_false) ? a : b;
1366 case irms_int_number:
1367 sc_and(a->value, b->value, NULL);
1368 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1371 panic("operation not defined on mode");
1375 ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b)
1377 assert(a->mode == b->mode);
1379 /* works even for vector modes */
1382 switch (get_mode_sort(a->mode)) {
1383 case irms_internal_boolean:
1384 return a == tarval_b_true && b == tarval_b_false ? tarval_b_true : tarval_b_false;
1386 case irms_int_number:
1387 sc_andnot(a->value, b->value, NULL);
1388 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1391 panic("operation not defined on mode");
1398 ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b)
1400 assert(a->mode == b->mode);
1402 /* works even for vector modes */
1405 switch (get_mode_sort(a->mode)) {
1406 case irms_internal_boolean:
1407 return (a == tarval_b_true) ? a : b;
1409 case irms_int_number:
1410 sc_or(a->value, b->value, NULL);
1411 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1414 panic("operation not defined on mode");
1419 * bitwise exclusive or (xor)
1421 ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b)
1423 assert((a->mode == b->mode));
1425 /* works even for vector modes */
1428 switch (get_mode_sort(a->mode)) {
1429 case irms_internal_boolean:
1430 return (a == b)? tarval_b_false : tarval_b_true;
1432 case irms_int_number:
1433 sc_xor(a->value, b->value, NULL);
1434 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1437 panic("operation not defined on mode");
1442 * bitwise left shift
1444 ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b)
1446 char *temp_val = NULL;
1448 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1452 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1453 /* vector arithmetic not implemented yet */
1457 if (get_mode_modulo_shift(a->mode) != 0) {
1458 temp_val = (char*) alloca(sc_get_buffer_length());
1460 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1461 sc_mod(b->value, temp_val, temp_val);
1463 temp_val = (char*)b->value;
1465 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1466 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1470 * bitwise unsigned right shift
1472 ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b)
1474 char *temp_val = NULL;
1476 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1480 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1481 /* vector arithmetic not implemented yet */
1485 if (get_mode_modulo_shift(a->mode) != 0) {
1486 temp_val = (char*) alloca(sc_get_buffer_length());
1488 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1489 sc_mod(b->value, temp_val, temp_val);
1491 temp_val = (char*)b->value;
1493 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1494 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1498 * bitwise signed right shift
1500 ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b)
1502 char *temp_val = NULL;
1504 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1508 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1509 /* vector arithmetic not implemented yet */
1513 if (get_mode_modulo_shift(a->mode) != 0) {
1514 temp_val = (char*) alloca(sc_get_buffer_length());
1516 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1517 sc_mod(b->value, temp_val, temp_val);
1519 temp_val = (char*)b->value;
1521 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1522 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1526 * bitwise rotation to left
1528 ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b)
1530 char *temp_val = NULL;
1532 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1536 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1537 /* vector arithmetic not implemented yet */
1541 if (get_mode_modulo_shift(a->mode) != 0) {
1542 temp_val = (char*) alloca(sc_get_buffer_length());
1544 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1545 sc_mod(b->value, temp_val, temp_val);
1547 temp_val = (char*)b->value;
1549 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1550 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1554 * carry flag of the last operation
1556 int tarval_carry(void)
1558 if (carry_flag == -1)
1559 panic("Carry undefined for the last operation");
1566 int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
1568 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1572 const tarval_mode_info *mode_info;
1573 const char *prefix, *suffix;
1575 mode_info = (const tarval_mode_info*) tv->mode->tv_priv;
1577 mode_info = &default_info;
1578 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1579 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1581 switch (get_mode_sort(tv->mode)) {
1582 case irms_reference:
1583 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1585 case irms_int_number:
1586 switch (mode_info->mode_output) {
1589 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1593 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1600 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1603 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1605 case irms_float_number:
1606 switch (mode_info->mode_output) {
1608 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1611 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1616 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1620 case irms_internal_boolean:
1621 switch (mode_info->mode_output) {
1627 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1631 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1634 case irms_control_flow:
1636 case irms_auxiliary:
1637 if (tv == tarval_bad)
1638 return snprintf(buf, len, "<TV_BAD>");
1639 if (tv == tarval_undefined)
1640 return snprintf(buf, len, "<TV_UNDEF>");
1641 if (tv == tarval_unreachable)
1642 return snprintf(buf, len, "<TV_UNREACHABLE>");
1643 if (tv == tarval_reachable)
1644 return snprintf(buf, len, "<TV_REACHABLE>");
1645 return snprintf(buf, len, "<TV_??""?>");
1652 * Output of tarvals to stdio.
1654 int tarval_printf(ir_tarval *tv)
1659 res = tarval_snprintf(buf, sizeof(buf), tv);
1660 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1665 char *get_tarval_bitpattern(ir_tarval *tv)
1668 int n = get_mode_size_bits(tv->mode);
1669 int bytes = (n + 7) / 8;
1670 char *res = XMALLOCN(char, n + 1);
1673 for (i = 0; i < bytes; i++) {
1674 byte = get_tarval_sub_bits(tv, i);
1675 for (j = 1; j < 256; j <<= 1)
1677 res[pos++] = j & byte ? '1' : '0';
1686 * access to the bitpattern
1688 unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs)
1690 switch (get_mode_arithmetic(tv->mode)) {
1691 case irma_twos_complement:
1692 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1694 return fc_sub_bits((const fp_value*) tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1696 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1701 * Specify the output options of one mode.
1703 * This functions stores the modinfo, so DO NOT DESTROY it.
1705 * Returns zero on success.
1707 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1711 mode->tv_priv = modeinfo;
1716 * Returns the output options of one mode.
1718 * This functions returns the mode info of a given mode.
1720 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1724 return (const tarval_mode_info*) mode->tv_priv;
1728 * Returns non-zero if a given (integer) tarval has only one single bit
1731 int tarval_is_single_bit(ir_tarval *tv)
1736 if (!tv || tv == tarval_bad) return 0;
1737 if (! mode_is_int(tv->mode)) return 0;
1739 l = get_mode_size_bytes(tv->mode);
1740 for (bits = 0, i = l - 1; i >= 0; --i) {
1741 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1743 /* check for more than one bit in these */
1755 * Return the number of set bits in a given (integer) tarval.
1757 int get_tarval_popcount(ir_tarval *tv)
1762 if (!tv || tv == tarval_bad) return -1;
1763 if (! mode_is_int(tv->mode)) return -1;
1765 l = get_mode_size_bytes(tv->mode);
1766 for (bits = 0, i = l - 1; i >= 0; --i) {
1767 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1769 bits += popcount(v);
1775 * Return the number of the lowest set bit in a given (integer) tarval.
1777 * @param tv the tarval
1779 * @return number of lowest set bit or -1 on error
1781 int get_tarval_lowest_bit(ir_tarval *tv)
1785 if (!tv || tv == tarval_bad) return -1;
1786 if (! mode_is_int(tv->mode)) return -1;
1788 l = get_mode_size_bytes(tv->mode);
1789 for (i = 0; i < l; ++i) {
1790 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1793 return ntz(v) + i * 8;
1799 * Returns non-zero if the mantissa of a floating point IEEE-754
1800 * tarval is zero (i.e. 1.0Exxx)
1802 int tarval_ieee754_zero_mantissa(ir_tarval *tv)
1804 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1805 return fc_zero_mantissa((const fp_value*) tv->value);
1808 /* Returns the exponent of a floating point IEEE-754 tarval. */
1809 int tarval_ieee754_get_exponent(ir_tarval *tv)
1811 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1812 return fc_get_exponent((const fp_value*) tv->value);
1816 * Check if the tarval can be converted to the given mode without
1819 int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode)
1821 const ieee_descriptor_t *desc = get_descriptor(mode);
1822 return fc_can_lossless_conv_to((const fp_value*) tv->value, desc);
1825 /* Set the immediate precision for IEEE-754 results. */
1826 unsigned tarval_ieee754_set_immediate_precision(unsigned bits)
1828 return fc_set_immediate_precision(bits);
1831 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1832 unsigned tarval_ieee754_get_exact(void)
1834 return fc_is_exact();
1837 /* Return the size of the mantissa in bits (including possible
1838 implicit bits) for the given mode. */
1839 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode)
1841 const ieee_descriptor_t *desc;
1843 assert(get_mode_arithmetic(mode) == irma_ieee754);
1844 desc = get_descriptor(mode);
1846 return desc->mantissa_size + desc->explicit_one;
1849 /* check if its the a floating point NaN */
1850 int tarval_is_NaN(ir_tarval *tv)
1852 if (! mode_is_float(tv->mode))
1854 return fc_is_nan((const fp_value*) tv->value);
1857 /* check if its the a floating point +inf */
1858 int tarval_is_plus_inf(ir_tarval *tv)
1860 if (! mode_is_float(tv->mode))
1862 return fc_is_inf((const fp_value*) tv->value)
1863 && !fc_is_negative((const fp_value*) tv->value);
1866 /* check if its the a floating point -inf */
1867 int tarval_is_minus_inf(ir_tarval *tv)
1869 if (! mode_is_float(tv->mode))
1871 return fc_is_inf((const fp_value*) tv->value)
1872 && fc_is_negative((const fp_value*) tv->value);
1875 /* check if the tarval represents a finite value */
1876 int tarval_is_finite(ir_tarval *tv)
1878 if (mode_is_float(tv->mode))
1879 return !fc_is_nan((const fp_value*) tv->value)
1880 && !fc_is_inf((const fp_value*) tv->value);
1885 * Sets the overflow mode for integer operations.
1887 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)
1889 int_overflow_mode = ov_mode;
1892 /* Get the overflow mode for integer operations. */
1893 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void)
1895 return int_overflow_mode;
1898 /* Enable/Disable floating point constant folding. */
1899 void tarval_enable_fp_ops(int enable)
1904 int tarval_fp_ops_enabled(void)
1910 * default mode_info for output as HEX
1912 static const tarval_mode_info hex_output = {
1919 * Initialization of the tarval module: called before init_mode()
1921 void init_tarval_1(long null_value, int support_quad_precision)
1923 /* if these assertion fail, tarval_is_constant() will follow ... */
1924 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1925 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1927 _null_value = null_value;
1929 /* initialize the sets holding the tarvals with a comparison function and
1930 * an initial size, which is the expected number of constants */
1931 tarvals = new_set(cmp_tv, N_CONSTANTS);
1932 values = new_set(memcmp, N_CONSTANTS);
1933 /* calls init_strcalc() with needed size */
1934 init_fltcalc(support_quad_precision ? 112 : 64);
1938 * Initialization of the tarval module: called after init_mode()
1940 void init_tarval_2(void)
1942 tarval_bad->kind = k_tarval;
1943 tarval_bad->mode = mode_BAD;
1944 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1946 tarval_undefined->kind = k_tarval;
1947 tarval_undefined->mode = mode_ANY;
1948 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1950 tarval_b_true->kind = k_tarval;
1951 tarval_b_true->mode = mode_b;
1952 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1954 tarval_b_false->kind = k_tarval;
1955 tarval_b_false->mode = mode_b;
1956 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1958 tarval_unreachable->kind = k_tarval;
1959 tarval_unreachable->mode = mode_X;
1960 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1962 tarval_reachable->kind = k_tarval;
1963 tarval_reachable->mode = mode_X;
1964 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1967 * assign output modes that are compatible with the
1968 * old implementation: Hex output
1970 set_tarval_mode_output_option(mode_Bs, &hex_output);
1971 set_tarval_mode_output_option(mode_Bu, &hex_output);
1972 set_tarval_mode_output_option(mode_Hs, &hex_output);
1973 set_tarval_mode_output_option(mode_Hu, &hex_output);
1974 set_tarval_mode_output_option(mode_Is, &hex_output);
1975 set_tarval_mode_output_option(mode_Iu, &hex_output);
1976 set_tarval_mode_output_option(mode_Ls, &hex_output);
1977 set_tarval_mode_output_option(mode_Lu, &hex_output);
1978 set_tarval_mode_output_option(mode_P, &hex_output);
1981 /* free all memory occupied by tarval. */
1982 void finish_tarval(void)
1986 del_set(tarvals); tarvals = NULL;
1987 del_set(values); values = NULL;
1990 int (is_tarval)(const void *thing)
1992 return _is_tarval(thing);