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_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 /* returns non-zero if can be converted to double */
494 int tarval_is_double(ir_tarval *tv)
498 return (get_mode_sort(tv->mode) == irms_float_number);
501 long double get_tarval_double(ir_tarval *tv)
503 assert(tarval_is_double(tv));
505 return fc_val_to_ieee754((const fp_value*) tv->value);
510 * Access routines for tarval fields ========================================
513 /* get the mode of the tarval */
514 ir_mode *(get_tarval_mode)(const ir_tarval *tv)
516 return _get_tarval_mode(tv);
520 * Special value query functions ============================================
522 * These functions calculate and return a tarval representing the requested
524 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
525 * functions, but these are stored on initialization of the irmode module and
526 * therefore the irmode functions should be preferred to the functions below.
529 ir_tarval *(get_tarval_bad)(void)
531 return _get_tarval_bad();
534 ir_tarval *(get_tarval_undefined)(void)
536 return _get_tarval_undefined();
539 ir_tarval *(get_tarval_b_false)(void)
541 return _get_tarval_b_false();
544 ir_tarval *(get_tarval_b_true)(void)
546 return _get_tarval_b_true();
549 ir_tarval *(get_tarval_reachable)(void)
551 return _get_tarval_reachable();
554 ir_tarval *(get_tarval_unreachable)(void)
556 return _get_tarval_unreachable();
559 ir_tarval *get_tarval_max(ir_mode *mode)
561 const ieee_descriptor_t *desc;
564 if (get_mode_n_vector_elems(mode) > 1) {
565 /* vector arithmetic not implemented yet */
569 switch (get_mode_sort(mode)) {
570 case irms_control_flow:
573 panic("mode %F does not support maximum value", mode);
575 case irms_internal_boolean:
576 return tarval_b_true;
578 case irms_float_number:
579 desc = get_descriptor(mode);
580 fc_get_max(desc, NULL);
581 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
584 case irms_int_number:
585 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
586 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
591 ir_tarval *get_tarval_min(ir_mode *mode)
593 const ieee_descriptor_t *desc;
596 if (get_mode_n_vector_elems(mode) > 1) {
597 /* vector arithmetic not implemented yet */
601 switch (get_mode_sort(mode)) {
602 case irms_control_flow:
605 panic("mode %F does not support minimum value", mode);
607 case irms_internal_boolean:
608 return tarval_b_false;
610 case irms_float_number:
611 desc = get_descriptor(mode);
612 fc_get_min(desc, NULL);
613 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
616 case irms_int_number:
617 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
618 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
623 /** The bit pattern for the pointer NULL */
624 static long _null_value = 0;
626 ir_tarval *get_tarval_null(ir_mode *mode)
630 if (get_mode_n_vector_elems(mode) > 1) {
631 /* vector arithmetic not implemented yet */
635 switch (get_mode_sort(mode)) {
636 case irms_control_flow:
639 panic("mode %F does not support null value", mode);
641 case irms_float_number:
642 return new_tarval_from_double(0.0, mode);
644 case irms_internal_boolean:
645 case irms_int_number:
646 return new_tarval_from_long(0l, mode);
649 return new_tarval_from_long(_null_value, mode);
654 ir_tarval *get_tarval_one(ir_mode *mode)
658 if (get_mode_n_vector_elems(mode) > 1)
659 panic("vector arithmetic not implemented yet");
661 switch (get_mode_sort(mode)) {
662 case irms_control_flow:
665 panic("mode %F does not support one value", mode);
667 case irms_internal_boolean:
668 return tarval_b_true;
670 case irms_float_number:
671 return new_tarval_from_double(1.0, mode);
674 case irms_int_number:
675 return new_tarval_from_long(1l, mode);
680 ir_tarval *get_tarval_all_one(ir_mode *mode)
684 if (get_mode_n_vector_elems(mode) > 1)
685 panic("vector arithmetic not implemented yet");
687 switch (get_mode_sort(mode)) {
688 case irms_control_flow:
691 panic("mode %F does not support all-one value", mode);
693 case irms_int_number:
694 case irms_internal_boolean:
696 return tarval_not(get_mode_null(mode));
699 case irms_float_number:
700 return new_tarval_from_double(1.0, mode);
705 int tarval_is_constant(ir_tarval *tv)
707 int num_res = sizeof(reserved_tv) / sizeof(reserved_tv[0]);
709 /* reserved tarvals are NOT constants. Note that although
710 tarval_b_true and tarval_b_false are reserved, they are constants of course. */
711 return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
714 ir_tarval *get_tarval_minus_one(ir_mode *mode)
718 if (get_mode_n_vector_elems(mode) > 1)
719 panic("vector arithmetic not implemented yet");
721 switch (get_mode_sort(mode)) {
722 case irms_control_flow:
725 case irms_internal_boolean:
726 panic("mode %F does not support minus one value", mode);
731 case irms_float_number:
732 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
734 case irms_int_number:
735 return new_tarval_from_long(-1l, mode);
740 ir_tarval *get_tarval_nan(ir_mode *mode)
742 const ieee_descriptor_t *desc;
745 if (get_mode_n_vector_elems(mode) > 1)
746 panic("vector arithmetic not implemented yet");
748 if (get_mode_sort(mode) == irms_float_number) {
749 desc = get_descriptor(mode);
750 fc_get_qnan(desc, NULL);
751 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
753 panic("mode %F does not support NaN value", mode);
756 ir_tarval *get_tarval_plus_inf(ir_mode *mode)
759 if (get_mode_n_vector_elems(mode) > 1)
760 panic("vector arithmetic not implemented yet");
762 if (get_mode_sort(mode) == irms_float_number) {
763 const ieee_descriptor_t *desc = get_descriptor(mode);
764 fc_get_plusinf(desc, NULL);
765 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
767 panic("mode %F does not support +inf value", mode);
770 ir_tarval *get_tarval_minus_inf(ir_mode *mode)
774 if (get_mode_n_vector_elems(mode) > 1)
775 panic("vector arithmetic not implemented yet");
777 if (get_mode_sort(mode) == irms_float_number) {
778 const ieee_descriptor_t *desc = get_descriptor(mode);
779 fc_get_minusinf(desc, NULL);
780 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
782 panic("mode %F does not support -inf value", mode);
786 * Arithmetic operations on tarvals ========================================
790 * test if negative number, 1 means 'yes'
792 int tarval_is_negative(ir_tarval *a)
794 if (get_mode_n_vector_elems(a->mode) > 1)
795 panic("vector arithmetic not implemented yet");
797 switch (get_mode_sort(a->mode)) {
798 case irms_int_number:
799 if (!mode_is_signed(a->mode)) return 0;
801 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
803 case irms_float_number:
804 return fc_is_negative((const fp_value*) a->value);
807 panic("mode %F does not support negation value", a->mode);
812 * test if null, 1 means 'yes'
814 int tarval_is_null(ir_tarval *a)
818 a == get_mode_null(get_tarval_mode(a));
822 * test if one, 1 means 'yes'
824 int tarval_is_one(ir_tarval *a)
828 a == get_mode_one(get_tarval_mode(a));
831 int tarval_is_all_one(ir_tarval *tv)
835 tv == get_mode_all_one(get_tarval_mode(tv));
839 * test if one, 1 means 'yes'
841 int tarval_is_minus_one(ir_tarval *a)
845 a == get_mode_minus_one(get_tarval_mode(a));
851 pn_Cmp tarval_cmp(ir_tarval *a, ir_tarval *b)
855 if (a == tarval_bad || b == tarval_bad) {
856 panic("Comparison with tarval_bad");
859 if (a == tarval_undefined || b == tarval_undefined)
862 if (a->mode != b->mode)
865 if (get_mode_n_vector_elems(a->mode) > 1) {
866 /* vector arithmetic not implemented yet */
867 panic("cmp not implemented for vector modes");
870 /* Here the two tarvals are unequal and of the same mode */
871 switch (get_mode_sort(a->mode)) {
872 case irms_control_flow:
879 case irms_float_number:
880 /* it should be safe to enable this even if other arithmetic is disabled */
882 return pn_Cmp_False;*/
884 * BEWARE: we cannot compare a == b here, because
885 * a NaN is always Unordered to any other value, even to itself!
887 switch (fc_comp((const fp_value*) a->value, (const fp_value*) b->value)) {
888 case -1: return pn_Cmp_Lt;
889 case 0: return pn_Cmp_Eq;
890 case 1: return pn_Cmp_Gt;
891 case 2: return pn_Cmp_Uo;
892 default: return pn_Cmp_False;
895 case irms_int_number:
898 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
900 case irms_internal_boolean:
903 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
909 * convert to other mode
911 ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *dst_mode)
914 fp_value *res = NULL;
915 const ieee_descriptor_t *desc;
923 if (src->mode == dst_mode)
926 if (get_mode_n_vector_elems(src->mode) > 1) {
927 /* vector arithmetic not implemented yet */
931 switch (get_mode_sort(src->mode)) {
932 case irms_control_flow:
937 /* cast float to something */
938 case irms_float_number:
939 switch (get_mode_sort(dst_mode)) {
940 case irms_float_number:
941 desc = get_descriptor(dst_mode);
942 fc_cast((const fp_value*) src->value, desc, NULL);
943 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
945 case irms_int_number:
946 switch (current_float_to_int_mode) {
948 res = fc_int((const fp_value*) src->value, NULL);
951 res = fc_rnd((const fp_value*) src->value, NULL);
954 buffer = (char*) alloca(sc_get_buffer_length());
955 if (! fc_flt2int(res, buffer, dst_mode))
957 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
960 /* the rest can't be converted */
965 /* cast int/characters to something */
966 case irms_int_number:
967 switch (get_mode_sort(dst_mode)) {
970 case irms_int_number:
971 buffer = (char*) alloca(sc_get_buffer_length());
972 memcpy(buffer, src->value, sc_get_buffer_length());
973 sign_extend(buffer, dst_mode);
974 return get_tarval_overflow(buffer, src->length, dst_mode);
976 case irms_internal_boolean:
977 /* XXX C semantics */
978 if (src == get_mode_null(src->mode)) return tarval_b_false;
979 else return tarval_b_true;
981 case irms_float_number:
982 /* XXX floating point unit does not understand internal integer
983 * representation, convert to string first, then create float from
985 buffer = (char*) alloca(100);
986 /* decimal string representation because hexadecimal output is
987 * interpreted unsigned by fc_val_from_str, so this is a HACK */
988 len = snprintf(buffer, 100, "%s",
989 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
990 buffer[100 - 1] = '\0';
991 desc = get_descriptor(dst_mode);
992 fc_val_from_str(buffer, len, desc, NULL);
993 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
1000 case irms_internal_boolean:
1001 /* beware: this is C semantic for the INTERNAL boolean mode */
1002 if (get_mode_sort(dst_mode) == irms_int_number)
1003 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
1006 case irms_reference:
1007 if (get_mode_sort(dst_mode) == irms_int_number) {
1008 buffer = (char*) alloca(sc_get_buffer_length());
1009 memcpy(buffer, src->value, sc_get_buffer_length());
1010 sign_extend(buffer, src->mode);
1011 return get_tarval_overflow(buffer, src->length, dst_mode);
1022 ir_tarval *tarval_not(ir_tarval *a)
1028 /* works for vector mode without changes */
1030 switch (get_mode_sort(a->mode)) {
1031 case irms_reference:
1032 case irms_int_number:
1033 buffer = (char*) alloca(sc_get_buffer_length());
1034 sc_not(a->value, buffer);
1035 return get_tarval(buffer, a->length, a->mode);
1037 case irms_internal_boolean:
1038 if (a == tarval_b_true)
1039 return tarval_b_false;
1040 if (a == tarval_b_false)
1041 return tarval_b_true;
1045 panic("bitwise negation is only allowed for integer and boolean");
1050 * arithmetic negation
1052 ir_tarval *tarval_neg(ir_tarval *a)
1056 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1060 /* note: negation is allowed even for unsigned modes. */
1062 if (get_mode_n_vector_elems(a->mode) > 1) {
1063 /* vector arithmetic not implemented yet */
1067 switch (get_mode_sort(a->mode)) {
1068 case irms_int_number:
1069 buffer = (char*) alloca(sc_get_buffer_length());
1070 sc_neg(a->value, buffer);
1071 return get_tarval_overflow(buffer, a->length, a->mode);
1073 case irms_float_number:
1074 /* it should be safe to enable this even if other arithmetic is disabled */
1076 return tarval_bad;*/
1078 fc_neg((const fp_value*) a->value, NULL);
1079 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1089 ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b)
1095 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1096 /* vector arithmetic not implemented yet */
1100 if (mode_is_reference(a->mode) && a->mode != b->mode) {
1101 b = tarval_convert_to(b, a->mode);
1102 } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
1103 a = tarval_convert_to(a, b->mode);
1106 assert(a->mode == b->mode);
1108 switch (get_mode_sort(a->mode)) {
1109 case irms_reference:
1110 case irms_int_number:
1111 /* modes of a,b are equal, so result has mode of a as this might be the character */
1112 buffer = (char*) alloca(sc_get_buffer_length());
1113 sc_add(a->value, b->value, buffer);
1114 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1115 return get_tarval_overflow(buffer, a->length, a->mode);
1117 case irms_float_number:
1121 fc_add((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1122 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1132 ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode)
1138 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1139 /* vector arithmetic not implemented yet */
1143 if (dst_mode != NULL) {
1144 if (a->mode != dst_mode)
1145 a = tarval_convert_to(a, dst_mode);
1146 if (b->mode != dst_mode)
1147 b = tarval_convert_to(b, dst_mode);
1149 assert(a->mode == b->mode);
1151 switch (get_mode_sort(a->mode)) {
1152 case irms_reference:
1153 case irms_int_number:
1154 /* modes of a,b are equal, so result has mode of a as this might be the character */
1155 buffer = (char*) alloca(sc_get_buffer_length());
1156 sc_sub(a->value, b->value, buffer);
1157 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1158 return get_tarval_overflow(buffer, a->length, a->mode);
1160 case irms_float_number:
1164 fc_sub((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1165 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1175 ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b)
1179 assert(a->mode == b->mode);
1183 if (get_mode_n_vector_elems(a->mode) > 1) {
1184 /* vector arithmetic not implemented yet */
1188 switch (get_mode_sort(a->mode)) {
1189 case irms_int_number:
1190 /* modes of a,b are equal */
1191 buffer = (char*) alloca(sc_get_buffer_length());
1192 sc_mul(a->value, b->value, buffer);
1193 return get_tarval_overflow(buffer, a->length, a->mode);
1195 case irms_float_number:
1199 fc_mul((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1200 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1208 * floating point division
1210 ir_tarval *tarval_quo(ir_tarval *a, ir_tarval *b)
1212 assert((a->mode == b->mode) && mode_is_float(a->mode));
1219 if (get_mode_n_vector_elems(a->mode) > 1) {
1220 /* vector arithmetic not implemented yet */
1224 fc_div((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1225 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1230 * overflow is impossible, but look out for division by zero
1232 ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b)
1234 assert((a->mode == b->mode) && mode_is_int(a->mode));
1238 if (get_mode_n_vector_elems(a->mode) > 1) {
1239 /* vector arithmetic not implemented yet */
1244 if (b == get_mode_null(b->mode)) return tarval_bad;
1245 /* modes of a,b are equal */
1246 sc_div(a->value, b->value, NULL);
1247 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1252 * overflow is impossible, but look out for division by zero
1254 ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b)
1256 assert((a->mode == b->mode) && mode_is_int(a->mode));
1260 if (get_mode_n_vector_elems(a->mode) > 1) {
1261 /* vector arithmetic not implemented yet */
1266 if (b == get_mode_null(b->mode)) return tarval_bad;
1267 /* modes of a,b are equal */
1268 sc_mod(a->value, b->value, NULL);
1269 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1273 * integer division AND remainder
1274 * overflow is impossible, but look out for division by zero
1276 ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod)
1278 int len = sc_get_buffer_length();
1279 char *div_res = (char*) alloca(len);
1280 char *mod_res = (char*) alloca(len);
1282 assert((a->mode == b->mode) && mode_is_int(a->mode));
1286 if (get_mode_n_vector_elems(a->mode) > 1) {
1287 /* vector arithmetic not implemented yet */
1293 if (b == get_mode_null(b->mode)) return tarval_bad;
1294 /* modes of a,b are equal */
1295 sc_divmod(a->value, b->value, div_res, mod_res);
1296 *mod = get_tarval(mod_res, len, a->mode);
1297 return get_tarval(div_res, len, a->mode);
1303 ir_tarval *tarval_abs(ir_tarval *a)
1308 assert(mode_is_num(a->mode));
1310 if (get_mode_n_vector_elems(a->mode) > 1) {
1311 /* vector arithmetic not implemented yet */
1315 switch (get_mode_sort(a->mode)) {
1316 case irms_int_number:
1317 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1318 buffer = (char*) alloca(sc_get_buffer_length());
1319 sc_neg(a->value, buffer);
1320 return get_tarval_overflow(buffer, a->length, a->mode);
1324 case irms_float_number:
1325 /* it should be safe to enable this even if other arithmetic is disabled */
1327 return tarval_bad;*/
1329 if (fc_comp((const fp_value*) a->value,
1330 (const fp_value*) get_mode_null(a->mode)->value) == -1) {
1331 fc_neg((const fp_value*) a->value, NULL);
1332 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1345 ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b)
1347 assert(a->mode == b->mode);
1349 /* works even for vector modes */
1352 switch (get_mode_sort(a->mode)) {
1353 case irms_internal_boolean:
1354 return (a == tarval_b_false) ? a : b;
1356 case irms_int_number:
1357 sc_and(a->value, b->value, NULL);
1358 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1361 panic("operation not defined on mode");
1365 ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b)
1367 assert(a->mode == b->mode);
1369 /* works even for vector modes */
1372 switch (get_mode_sort(a->mode)) {
1373 case irms_internal_boolean:
1374 return a == tarval_b_true && b == tarval_b_false ? tarval_b_true : tarval_b_false;
1376 case irms_int_number:
1377 sc_andnot(a->value, b->value, NULL);
1378 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1381 panic("operation not defined on mode");
1388 ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b)
1390 assert(a->mode == b->mode);
1392 /* works even for vector modes */
1395 switch (get_mode_sort(a->mode)) {
1396 case irms_internal_boolean:
1397 return (a == tarval_b_true) ? a : b;
1399 case irms_int_number:
1400 sc_or(a->value, b->value, NULL);
1401 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1404 panic("operation not defined on mode");
1409 * bitwise exclusive or (xor)
1411 ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b)
1413 assert((a->mode == b->mode));
1415 /* works even for vector modes */
1418 switch (get_mode_sort(a->mode)) {
1419 case irms_internal_boolean:
1420 return (a == b)? tarval_b_false : tarval_b_true;
1422 case irms_int_number:
1423 sc_xor(a->value, b->value, NULL);
1424 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1427 panic("operation not defined on mode");
1432 * bitwise left shift
1434 ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b)
1436 char *temp_val = NULL;
1438 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1442 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1443 /* vector arithmetic not implemented yet */
1447 if (get_mode_modulo_shift(a->mode) != 0) {
1448 temp_val = (char*) alloca(sc_get_buffer_length());
1450 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1451 sc_mod(b->value, temp_val, temp_val);
1453 temp_val = (char*)b->value;
1455 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1456 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1460 * bitwise unsigned right shift
1462 ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b)
1464 char *temp_val = NULL;
1466 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1470 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1471 /* vector arithmetic not implemented yet */
1475 if (get_mode_modulo_shift(a->mode) != 0) {
1476 temp_val = (char*) alloca(sc_get_buffer_length());
1478 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1479 sc_mod(b->value, temp_val, temp_val);
1481 temp_val = (char*)b->value;
1483 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1484 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1488 * bitwise signed right shift
1490 ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b)
1492 char *temp_val = NULL;
1494 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1498 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1499 /* vector arithmetic not implemented yet */
1503 if (get_mode_modulo_shift(a->mode) != 0) {
1504 temp_val = (char*) alloca(sc_get_buffer_length());
1506 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1507 sc_mod(b->value, temp_val, temp_val);
1509 temp_val = (char*)b->value;
1511 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1512 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1516 * bitwise rotation to left
1518 ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b)
1520 char *temp_val = NULL;
1522 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1526 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1527 /* vector arithmetic not implemented yet */
1531 if (get_mode_modulo_shift(a->mode) != 0) {
1532 temp_val = (char*) alloca(sc_get_buffer_length());
1534 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1535 sc_mod(b->value, temp_val, temp_val);
1537 temp_val = (char*)b->value;
1539 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1540 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1544 * carry flag of the last operation
1546 int tarval_carry(void)
1548 if (carry_flag == -1)
1549 panic("Carry undefined for the last operation");
1556 int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
1558 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1562 const tarval_mode_info *mode_info;
1563 const char *prefix, *suffix;
1565 mode_info = (const tarval_mode_info*) tv->mode->tv_priv;
1567 mode_info = &default_info;
1568 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1569 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1571 switch (get_mode_sort(tv->mode)) {
1572 case irms_reference:
1573 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1575 case irms_int_number:
1576 switch (mode_info->mode_output) {
1579 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1583 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1590 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1593 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1595 case irms_float_number:
1596 switch (mode_info->mode_output) {
1598 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1601 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1606 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1610 case irms_internal_boolean:
1611 switch (mode_info->mode_output) {
1617 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1621 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1624 case irms_control_flow:
1626 case irms_auxiliary:
1627 if (tv == tarval_bad)
1628 return snprintf(buf, len, "<TV_BAD>");
1629 if (tv == tarval_undefined)
1630 return snprintf(buf, len, "<TV_UNDEF>");
1631 if (tv == tarval_unreachable)
1632 return snprintf(buf, len, "<TV_UNREACHABLE>");
1633 if (tv == tarval_reachable)
1634 return snprintf(buf, len, "<TV_REACHABLE>");
1635 return snprintf(buf, len, "<TV_??""?>");
1642 * Output of tarvals to stdio.
1644 int tarval_printf(ir_tarval *tv)
1649 res = tarval_snprintf(buf, sizeof(buf), tv);
1650 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1655 char *get_tarval_bitpattern(ir_tarval *tv)
1658 int n = get_mode_size_bits(tv->mode);
1659 int bytes = (n + 7) / 8;
1660 char *res = XMALLOCN(char, n + 1);
1663 for (i = 0; i < bytes; i++) {
1664 byte = get_tarval_sub_bits(tv, i);
1665 for (j = 1; j < 256; j <<= 1)
1667 res[pos++] = j & byte ? '1' : '0';
1676 * access to the bitpattern
1678 unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs)
1680 switch (get_mode_arithmetic(tv->mode)) {
1681 case irma_twos_complement:
1682 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1684 return fc_sub_bits((const fp_value*) tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1686 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1691 * Specify the output options of one mode.
1693 * This functions stores the modinfo, so DO NOT DESTROY it.
1695 * Returns zero on success.
1697 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1701 mode->tv_priv = modeinfo;
1706 * Returns the output options of one mode.
1708 * This functions returns the mode info of a given mode.
1710 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1714 return (const tarval_mode_info*) mode->tv_priv;
1718 * Returns non-zero if a given (integer) tarval has only one single bit
1721 int tarval_is_single_bit(ir_tarval *tv)
1726 if (!tv || tv == tarval_bad) return 0;
1727 if (! mode_is_int(tv->mode)) return 0;
1729 l = get_mode_size_bytes(tv->mode);
1730 for (bits = 0, i = l - 1; i >= 0; --i) {
1731 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1733 /* check for more than one bit in these */
1745 * Return the number of set bits in a given (integer) tarval.
1747 int get_tarval_popcount(ir_tarval *tv)
1752 if (!tv || tv == tarval_bad) return -1;
1753 if (! mode_is_int(tv->mode)) return -1;
1755 l = get_mode_size_bytes(tv->mode);
1756 for (bits = 0, i = l - 1; i >= 0; --i) {
1757 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1759 bits += popcount(v);
1765 * Return the number of the lowest set bit in a given (integer) tarval.
1767 * @param tv the tarval
1769 * @return number of lowest set bit or -1 on error
1771 int get_tarval_lowest_bit(ir_tarval *tv)
1775 if (!tv || tv == tarval_bad) return -1;
1776 if (! mode_is_int(tv->mode)) return -1;
1778 l = get_mode_size_bytes(tv->mode);
1779 for (i = 0; i < l; ++i) {
1780 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1783 return ntz(v) + i * 8;
1789 * Returns non-zero if the mantissa of a floating point IEEE-754
1790 * tarval is zero (i.e. 1.0Exxx)
1792 int tarval_ieee754_zero_mantissa(ir_tarval *tv)
1794 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1795 return fc_zero_mantissa((const fp_value*) tv->value);
1798 /* Returns the exponent of a floating point IEEE-754 tarval. */
1799 int tarval_ieee754_get_exponent(ir_tarval *tv)
1801 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1802 return fc_get_exponent((const fp_value*) tv->value);
1806 * Check if the tarval can be converted to the given mode without
1809 int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode)
1811 const ieee_descriptor_t *desc = get_descriptor(mode);
1812 return fc_can_lossless_conv_to((const fp_value*) tv->value, desc);
1815 /* Set the immediate precision for IEEE-754 results. */
1816 unsigned tarval_ieee754_set_immediate_precision(unsigned bits)
1818 return fc_set_immediate_precision(bits);
1821 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1822 unsigned tarval_ieee754_get_exact(void)
1824 return fc_is_exact();
1827 /* Return the size of the mantissa in bits (including possible
1828 implicit bits) for the given mode. */
1829 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode)
1831 const ieee_descriptor_t *desc;
1833 assert(get_mode_arithmetic(mode) == irma_ieee754);
1834 desc = get_descriptor(mode);
1836 return desc->mantissa_size + desc->explicit_one;
1839 /* check if its the a floating point NaN */
1840 int tarval_is_NaN(ir_tarval *tv)
1842 if (! mode_is_float(tv->mode))
1844 return fc_is_nan((const fp_value*) tv->value);
1847 /* check if its the a floating point +inf */
1848 int tarval_is_plus_inf(ir_tarval *tv)
1850 if (! mode_is_float(tv->mode))
1852 return fc_is_inf((const fp_value*) tv->value)
1853 && !fc_is_negative((const fp_value*) tv->value);
1856 /* check if its the a floating point -inf */
1857 int tarval_is_minus_inf(ir_tarval *tv)
1859 if (! mode_is_float(tv->mode))
1861 return fc_is_inf((const fp_value*) tv->value)
1862 && fc_is_negative((const fp_value*) tv->value);
1865 /* check if the tarval represents a finite value */
1866 int tarval_is_finite(ir_tarval *tv)
1868 if (mode_is_float(tv->mode))
1869 return !fc_is_nan((const fp_value*) tv->value)
1870 && !fc_is_inf((const fp_value*) tv->value);
1875 * Sets the overflow mode for integer operations.
1877 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)
1879 int_overflow_mode = ov_mode;
1882 /* Get the overflow mode for integer operations. */
1883 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void)
1885 return int_overflow_mode;
1888 /* Enable/Disable floating point constant folding. */
1889 void tarval_enable_fp_ops(int enable)
1894 int tarval_fp_ops_enabled(void)
1900 * default mode_info for output as HEX
1902 static const tarval_mode_info hex_output = {
1909 * Initialization of the tarval module: called before init_mode()
1911 void init_tarval_1(long null_value, int support_quad_precision)
1913 /* if these assertion fail, tarval_is_constant() will follow ... */
1914 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1915 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1917 _null_value = null_value;
1919 /* initialize the sets holding the tarvals with a comparison function and
1920 * an initial size, which is the expected number of constants */
1921 tarvals = new_set(cmp_tv, N_CONSTANTS);
1922 values = new_set(memcmp, N_CONSTANTS);
1923 /* calls init_strcalc() with needed size */
1924 init_fltcalc(support_quad_precision ? 112 : 64);
1928 * Initialization of the tarval module: called after init_mode()
1930 void init_tarval_2(void)
1932 tarval_bad->kind = k_tarval;
1933 tarval_bad->mode = mode_BAD;
1934 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1936 tarval_undefined->kind = k_tarval;
1937 tarval_undefined->mode = mode_ANY;
1938 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1940 tarval_b_true->kind = k_tarval;
1941 tarval_b_true->mode = mode_b;
1942 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1944 tarval_b_false->kind = k_tarval;
1945 tarval_b_false->mode = mode_b;
1946 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1948 tarval_unreachable->kind = k_tarval;
1949 tarval_unreachable->mode = mode_X;
1950 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1952 tarval_reachable->kind = k_tarval;
1953 tarval_reachable->mode = mode_X;
1954 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1957 * assign output modes that are compatible with the
1958 * old implementation: Hex output
1960 set_tarval_mode_output_option(mode_Bs, &hex_output);
1961 set_tarval_mode_output_option(mode_Bu, &hex_output);
1962 set_tarval_mode_output_option(mode_Hs, &hex_output);
1963 set_tarval_mode_output_option(mode_Hu, &hex_output);
1964 set_tarval_mode_output_option(mode_Is, &hex_output);
1965 set_tarval_mode_output_option(mode_Iu, &hex_output);
1966 set_tarval_mode_output_option(mode_Ls, &hex_output);
1967 set_tarval_mode_output_option(mode_Lu, &hex_output);
1968 set_tarval_mode_output_option(mode_P, &hex_output);
1971 /* free all memory occupied by tarval. */
1972 void finish_tarval(void)
1976 del_set(tarvals); tarvals = NULL;
1977 del_set(values); values = NULL;
1980 int (is_tarval)(const void *thing)
1982 return _is_tarval(thing);