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
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.
42 #include "bitfiddle.h"
52 #include "firm_common.h"
55 /** Size of hash tables. Should correspond to average number of distinct constant
57 #define N_CONSTANTS 2048
59 /* unused, float to int doesn't work yet */
60 typedef enum float_to_int_mode {
65 static float_to_int_mode current_float_to_int_mode = TRUNCATE;
67 /* set this to true if infinity should be clipped to +/- MAX_FLOAT */
68 #define SWITCH_NOINFINITY 0
69 /* set this to true if denormals should be clipped to zero */
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, FC_NORMAL };
105 /** IEEE-754 single precision */
106 static const ieee_descriptor_t single_desc = { 8, 23, 0, FC_NORMAL };
107 /** IEEE-754 double precision */
108 static const ieee_descriptor_t double_desc = { 11, 52, 0, FC_NORMAL };
109 /** Intel x87 extended precision */
110 static const ieee_descriptor_t extended_desc = { 15, 63, 1, FC_NORMAL };
112 /** IEEE-754r quad precision */
113 static const ieee_descriptor_t quad_desc = { 15, 112, 0, FC_NORMAL };
115 /****************************************************************************
117 ****************************************************************************/
119 static unsigned hash_val(const void *value, size_t length);
120 static unsigned 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);
132 __attribute__((unused))
134 void tarval_verify(ir_tarval *tv)
140 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
141 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
143 if (!FIND_TARVAL(tv)) fail_verify(tv);
144 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
148 /** Hash a tarval. */
149 static unsigned hash_tv(ir_tarval *tv)
151 return (unsigned)((PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length);
154 /** Hash a value. Treat it as a byte array. */
155 static unsigned hash_val(const void *value, size_t length)
160 /* scramble the byte - array */
161 for (i = 0; i < length; ++i) {
162 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
163 hash += (hash << 11) ^ (hash >> 17);
169 static int cmp_tv(const void *p1, const void *p2, size_t n)
171 const ir_tarval *tv1 = (const ir_tarval*) p1;
172 const ir_tarval *tv2 = (const ir_tarval*) p2;
175 assert(tv1->kind == k_tarval);
176 assert(tv2->kind == k_tarval);
177 if (tv1->mode < tv2->mode)
179 if (tv1->mode > tv2->mode)
181 if (tv1->length < tv2->length)
183 if (tv1->length > tv2->length)
185 if (tv1->value < tv2->value)
187 if (tv1->value > tv2->value)
193 /** finds tarval with value/mode or creates new tarval */
194 static ir_tarval *get_tarval(const void *value, size_t length, ir_mode *mode)
202 /* if there already is such a value, it is returned, else value
203 * is copied into the set */
204 char *temp = (char*) alloca(length);
205 memcpy(temp, value, length);
206 if (get_mode_arithmetic(mode) == irma_twos_complement) {
207 sign_extend(temp, mode);
209 tv.value = INSERT_VALUE(temp, length);
213 /* if there is such a tarval, it is returned, else tv is copied
215 return (ir_tarval *)INSERT_TARVAL(&tv);
221 static ir_tarval *get_tarval_overflow(const void *value, size_t length, ir_mode *mode)
225 switch (get_mode_sort(mode)) {
227 /* addresses always wrap around */
228 temp = (char*) alloca(sc_get_buffer_length());
229 memcpy(temp, value, sc_get_buffer_length());
230 sc_truncate(get_mode_size_bits(mode), temp);
231 /* the sc_ module expects that all bits are set ... */
232 sign_extend(temp, mode);
233 return get_tarval(temp, length, mode);
235 case irms_int_number:
236 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
237 switch (tarval_get_integer_overflow_mode()) {
238 case TV_OVERFLOW_SATURATE:
239 return get_mode_max(mode);
240 case TV_OVERFLOW_WRAP:
241 temp = (char*) alloca(sc_get_buffer_length());
242 memcpy(temp, value, sc_get_buffer_length());
243 sc_truncate(get_mode_size_bits(mode), temp);
244 /* the sc_ module expects that all bits are set ... */
245 sign_extend(temp, mode);
246 return get_tarval(temp, length, mode);
247 case TV_OVERFLOW_BAD:
250 return get_tarval(value, length, mode);
253 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
254 switch (tarval_get_integer_overflow_mode()) {
255 case TV_OVERFLOW_SATURATE:
256 return get_mode_min(mode);
257 case TV_OVERFLOW_WRAP: {
258 temp = (char*) alloca(sc_get_buffer_length());
259 memcpy(temp, value, sc_get_buffer_length());
260 sc_truncate(get_mode_size_bits(mode), temp);
261 return get_tarval(temp, length, mode);
263 case TV_OVERFLOW_BAD:
266 return get_tarval(value, length, mode);
271 case irms_float_number:
272 #if SWITCH_NOINFINITY
273 if (fc_is_inf((const fp_value*) value)) {
274 /* clip infinity to maximum value */
275 return fc_is_negative((const fp_value*) value) ? get_mode_min(mode) : get_mode_max(mode);
278 #if 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; */
321 panic("Unsupported mode in get_descriptor()");
325 ir_tarval *new_integer_tarval_from_str(const char *str, size_t len, char sign,
326 unsigned char base, ir_mode *mode)
331 buffer = alloca(sc_get_buffer_length());
333 ok = sc_val_from_str(sign, base, str, len, buffer);
337 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
340 static ir_tarval *new_tarval_from_str_int(const char *str, size_t len,
348 /* skip leading spaces */
349 while (len > 0 && str[0] == ' ') {
356 /* 1 sign character allowed */
361 } else if (str[0] == '+') {
366 /* a number starting with '0x' is hexadeciaml,
367 * a number starting with '0' (and at least 1 more char) is octal */
368 if (len >= 2 && str[0] == '0') {
369 if (str[1] == 'x' || str[1] == 'X') {
382 buffer = alloca(sc_get_buffer_length());
384 ok = sc_val_from_str(sign, base, str, len, buffer);
388 return get_tarval_overflow(buffer, sc_get_buffer_length(), mode);
392 * Constructors =============================================================
394 ir_tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
396 const ieee_descriptor_t *desc;
402 switch (get_mode_sort(mode)) {
403 case irms_control_flow:
406 panic("Unsupported tarval creation with mode %F", mode);
408 case irms_internal_boolean:
409 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
410 if (!strcasecmp(str, "true"))
411 return tarval_b_true;
412 else if (!strcasecmp(str, "false"))
413 return tarval_b_false;
415 /* XXX This is C semantics */
416 return atoi(str) ? tarval_b_true : tarval_b_false;
418 case irms_float_number:
419 desc = get_descriptor(mode);
420 fc_val_from_str(str, len, desc, NULL);
421 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
424 if (!strcasecmp(str, "null"))
425 return get_tarval_null(mode);
427 case irms_int_number:
428 return new_tarval_from_str_int(str, len, mode);
430 panic("Unsupported tarval creation with mode %F", mode);
434 * helper function, create a tarval from long
436 ir_tarval *new_tarval_from_long(long l, ir_mode *mode)
440 switch (get_mode_sort(mode)) {
441 case irms_internal_boolean:
442 /* XXX C semantics ! */
443 return l ? tarval_b_true : tarval_b_false ;
446 /* same as integer modes */
447 case irms_int_number:
448 sc_val_from_long(l, NULL);
449 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
451 case irms_float_number:
452 return new_tarval_from_double((long double)l, mode);
455 panic("unsupported mode sort");
459 /* returns non-zero if can be converted to long */
460 int tarval_is_long(ir_tarval *tv)
462 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
465 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
466 /* the value might be too big to fit in a long */
467 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
468 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
469 /* really doesn't fit */
476 /* this might overflow the machine's long, so use only with small values */
477 long get_tarval_long(ir_tarval* tv)
479 assert(tarval_is_long(tv) && "tarval too big to fit in long");
481 return sc_val_to_long(tv->value);
484 ir_tarval *new_tarval_from_long_double(long double d, ir_mode *mode)
486 const ieee_descriptor_t *desc;
488 assert(mode && (get_mode_sort(mode) == irms_float_number));
489 desc = get_descriptor(mode);
490 fc_val_from_ieee754(d, desc, NULL);
491 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
494 ir_tarval *new_tarval_from_double(double d, ir_mode *mode)
496 return new_tarval_from_long_double(d, mode);
499 /* returns non-zero if can be converted to double */
500 int tarval_is_double(ir_tarval *tv)
504 return (get_mode_sort(tv->mode) == irms_float_number);
507 long double get_tarval_long_double(ir_tarval *tv)
509 assert(tarval_is_double(tv));
511 return fc_val_to_ieee754((const fp_value*) tv->value);
514 double get_tarval_double(ir_tarval *tv)
516 return get_tarval_long_double(tv);
521 * Access routines for tarval fields ========================================
524 /* get the mode of the tarval */
525 ir_mode *(get_tarval_mode)(const ir_tarval *tv)
527 return _get_tarval_mode(tv);
531 * Special value query functions ============================================
533 * These functions calculate and return a tarval representing the requested
535 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
536 * functions, but these are stored on initialization of the irmode module and
537 * therefore the irmode functions should be preferred to the functions below.
540 ir_tarval *(get_tarval_bad)(void)
542 return _get_tarval_bad();
545 ir_tarval *(get_tarval_undefined)(void)
547 return _get_tarval_undefined();
550 ir_tarval *(get_tarval_b_false)(void)
552 return _get_tarval_b_false();
555 ir_tarval *(get_tarval_b_true)(void)
557 return _get_tarval_b_true();
560 ir_tarval *(get_tarval_reachable)(void)
562 return _get_tarval_reachable();
565 ir_tarval *(get_tarval_unreachable)(void)
567 return _get_tarval_unreachable();
570 ir_tarval *get_tarval_max(ir_mode *mode)
572 const ieee_descriptor_t *desc;
575 if (get_mode_n_vector_elems(mode) > 1) {
576 /* vector arithmetic not implemented yet */
580 switch (get_mode_sort(mode)) {
581 case irms_control_flow:
584 panic("mode %F does not support maximum value", mode);
586 case irms_internal_boolean:
587 return tarval_b_true;
589 case irms_float_number:
590 desc = get_descriptor(mode);
591 fc_get_max(desc, NULL);
592 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
595 case irms_int_number:
596 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
597 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
602 ir_tarval *get_tarval_min(ir_mode *mode)
604 const ieee_descriptor_t *desc;
607 if (get_mode_n_vector_elems(mode) > 1) {
608 /* vector arithmetic not implemented yet */
612 switch (get_mode_sort(mode)) {
613 case irms_control_flow:
616 panic("mode %F does not support minimum value", mode);
618 case irms_internal_boolean:
619 return tarval_b_false;
621 case irms_float_number:
622 desc = get_descriptor(mode);
623 fc_get_min(desc, NULL);
624 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
627 case irms_int_number:
628 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
629 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
634 /** The bit pattern for the pointer NULL */
635 static long _null_value = 0;
637 ir_tarval *get_tarval_null(ir_mode *mode)
641 if (get_mode_n_vector_elems(mode) > 1) {
642 /* vector arithmetic not implemented yet */
646 switch (get_mode_sort(mode)) {
647 case irms_control_flow:
650 panic("mode %F does not support null value", mode);
652 case irms_float_number:
653 return new_tarval_from_double(0.0, mode);
655 case irms_internal_boolean:
656 case irms_int_number:
657 return new_tarval_from_long(0l, mode);
660 return new_tarval_from_long(_null_value, mode);
665 ir_tarval *get_tarval_one(ir_mode *mode)
669 if (get_mode_n_vector_elems(mode) > 1)
670 panic("vector arithmetic not implemented yet");
672 switch (get_mode_sort(mode)) {
673 case irms_control_flow:
676 panic("mode %F does not support one value", mode);
678 case irms_internal_boolean:
679 return tarval_b_true;
681 case irms_float_number:
682 return new_tarval_from_double(1.0, mode);
685 case irms_int_number:
686 return new_tarval_from_long(1l, mode);
691 ir_tarval *get_tarval_all_one(ir_mode *mode)
695 if (get_mode_n_vector_elems(mode) > 1)
696 panic("vector arithmetic not implemented yet");
698 switch (get_mode_sort(mode)) {
699 case irms_control_flow:
702 panic("mode %F does not support all-one value", mode);
704 case irms_int_number:
705 case irms_internal_boolean:
707 return tarval_not(get_mode_null(mode));
710 case irms_float_number:
711 return new_tarval_from_double(1.0, mode);
716 int tarval_is_constant(ir_tarval *tv)
718 int num_res = sizeof(reserved_tv) / sizeof(reserved_tv[0]);
720 /* reserved tarvals are NOT constants. Note that although
721 tarval_b_true and tarval_b_false are reserved, they are constants of course. */
722 return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
725 ir_tarval *get_tarval_minus_one(ir_mode *mode)
729 if (get_mode_n_vector_elems(mode) > 1)
730 panic("vector arithmetic not implemented yet");
732 switch (get_mode_sort(mode)) {
733 case irms_control_flow:
736 case irms_internal_boolean:
737 panic("mode %F does not support minus one value", mode);
742 case irms_float_number:
743 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
745 case irms_int_number:
746 return new_tarval_from_long(-1l, mode);
751 ir_tarval *get_tarval_nan(ir_mode *mode)
753 const ieee_descriptor_t *desc;
756 if (get_mode_n_vector_elems(mode) > 1)
757 panic("vector arithmetic not implemented yet");
759 if (get_mode_sort(mode) == irms_float_number) {
760 desc = get_descriptor(mode);
761 fc_get_qnan(desc, NULL);
762 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
764 panic("mode %F does not support NaN value", mode);
767 ir_tarval *get_tarval_plus_inf(ir_mode *mode)
770 if (get_mode_n_vector_elems(mode) > 1)
771 panic("vector arithmetic not implemented yet");
773 if (get_mode_sort(mode) == irms_float_number) {
774 const ieee_descriptor_t *desc = get_descriptor(mode);
775 fc_get_plusinf(desc, NULL);
776 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
778 panic("mode %F does not support +inf value", mode);
781 ir_tarval *get_tarval_minus_inf(ir_mode *mode)
785 if (get_mode_n_vector_elems(mode) > 1)
786 panic("vector arithmetic not implemented yet");
788 if (get_mode_sort(mode) == irms_float_number) {
789 const ieee_descriptor_t *desc = get_descriptor(mode);
790 fc_get_minusinf(desc, NULL);
791 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
793 panic("mode %F does not support -inf value", mode);
797 * Arithmetic operations on tarvals ========================================
801 * test if negative number, 1 means 'yes'
803 int tarval_is_negative(ir_tarval *a)
805 if (get_mode_n_vector_elems(a->mode) > 1)
806 panic("vector arithmetic not implemented yet");
808 switch (get_mode_sort(a->mode)) {
809 case irms_int_number:
810 if (!mode_is_signed(a->mode)) return 0;
812 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
814 case irms_float_number:
815 return fc_is_negative((const fp_value*) a->value);
818 panic("mode %F does not support negation value", a->mode);
823 * test if null, 1 means 'yes'
825 int tarval_is_null(ir_tarval *a)
829 a == get_mode_null(get_tarval_mode(a));
833 * test if one, 1 means 'yes'
835 int tarval_is_one(ir_tarval *a)
839 a == get_mode_one(get_tarval_mode(a));
842 int tarval_is_all_one(ir_tarval *tv)
846 tv == get_mode_all_one(get_tarval_mode(tv));
850 * test if one, 1 means 'yes'
852 int tarval_is_minus_one(ir_tarval *a)
856 a == get_mode_minus_one(get_tarval_mode(a));
862 ir_relation tarval_cmp(ir_tarval *a, ir_tarval *b)
866 if (a == tarval_bad || b == tarval_bad) {
867 panic("Comparison with tarval_bad");
870 if (a == tarval_undefined || b == tarval_undefined)
871 return ir_relation_false;
873 if (a->mode != b->mode)
874 return ir_relation_false;
876 if (get_mode_n_vector_elems(a->mode) > 1) {
877 /* vector arithmetic not implemented yet */
878 panic("cmp not implemented for vector modes");
881 /* Here the two tarvals are unequal and of the same mode */
882 switch (get_mode_sort(a->mode)) {
883 case irms_control_flow:
887 return ir_relation_equal;
888 return ir_relation_false;
890 case irms_float_number:
892 * BEWARE: we cannot compare a == b here, because
893 * a NaN is always Unordered to any other value, even to itself!
895 switch (fc_comp((const fp_value*) a->value, (const fp_value*) b->value)) {
896 case -1: return ir_relation_less;
897 case 0: return ir_relation_equal;
898 case 1: return ir_relation_greater;
899 case 2: return ir_relation_unordered;
900 default: return ir_relation_false;
903 case irms_int_number:
905 return ir_relation_equal;
906 return sc_comp(a->value, b->value) == 1 ? ir_relation_greater : ir_relation_less;
908 case irms_internal_boolean:
910 return ir_relation_equal;
911 return a == tarval_b_true ? ir_relation_greater : ir_relation_less;
913 return ir_relation_false;
917 * convert to other mode
919 ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *dst_mode)
922 fp_value *res = NULL;
923 const ieee_descriptor_t *desc;
931 if (src->mode == dst_mode)
934 if (get_mode_n_vector_elems(src->mode) > 1) {
935 /* vector arithmetic not implemented yet */
939 switch (get_mode_sort(src->mode)) {
940 case irms_control_flow:
945 /* cast float to something */
946 case irms_float_number:
947 switch (get_mode_sort(dst_mode)) {
948 case irms_float_number:
949 desc = get_descriptor(dst_mode);
950 fc_cast((const fp_value*) src->value, desc, NULL);
951 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
953 case irms_int_number:
954 switch (current_float_to_int_mode) {
956 res = fc_int((const fp_value*) src->value, NULL);
959 res = fc_rnd((const fp_value*) src->value, NULL);
962 buffer = (char*) alloca(sc_get_buffer_length());
963 if (! fc_flt2int(res, buffer, dst_mode))
965 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
970 /* the rest can't be converted */
973 /* cast int/characters to something */
974 case irms_int_number:
975 switch (get_mode_sort(dst_mode)) {
978 case irms_int_number:
979 buffer = (char*) alloca(sc_get_buffer_length());
980 memcpy(buffer, src->value, sc_get_buffer_length());
981 return get_tarval_overflow(buffer, src->length, dst_mode);
983 case irms_internal_boolean:
984 /* XXX C semantics */
985 if (src == get_mode_null(src->mode)) return tarval_b_false;
986 else return tarval_b_true;
988 case irms_float_number:
989 /* XXX floating point unit does not understand internal integer
990 * representation, convert to string first, then create float from
992 buffer = (char*) alloca(100);
993 /* decimal string representation because hexadecimal output is
994 * interpreted unsigned by fc_val_from_str, so this is a HACK */
995 len = snprintf(buffer, 100, "%s",
996 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
997 buffer[100 - 1] = '\0';
998 desc = get_descriptor(dst_mode);
999 fc_val_from_str(buffer, len, desc, NULL);
1000 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
1007 case irms_internal_boolean:
1008 /* beware: this is C semantic for the INTERNAL boolean mode */
1009 if (get_mode_sort(dst_mode) == irms_int_number)
1010 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
1013 case irms_reference:
1014 if (get_mode_sort(dst_mode) == irms_int_number) {
1015 buffer = (char*) alloca(sc_get_buffer_length());
1016 memcpy(buffer, src->value, sc_get_buffer_length());
1017 sign_extend(buffer, src->mode);
1018 return get_tarval_overflow(buffer, src->length, dst_mode);
1029 ir_tarval *tarval_not(ir_tarval *a)
1035 /* works for vector mode without changes */
1037 switch (get_mode_sort(a->mode)) {
1038 case irms_reference:
1039 case irms_int_number:
1040 buffer = (char*) alloca(sc_get_buffer_length());
1041 sc_not(a->value, buffer);
1042 return get_tarval(buffer, a->length, a->mode);
1044 case irms_internal_boolean:
1045 if (a == tarval_b_true)
1046 return tarval_b_false;
1047 if (a == tarval_b_false)
1048 return tarval_b_true;
1052 panic("bitwise negation is only allowed for integer and boolean");
1057 * arithmetic negation
1059 ir_tarval *tarval_neg(ir_tarval *a)
1063 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1067 /* note: negation is allowed even for unsigned modes. */
1069 if (get_mode_n_vector_elems(a->mode) > 1) {
1070 /* vector arithmetic not implemented yet */
1074 switch (get_mode_sort(a->mode)) {
1075 case irms_int_number:
1076 buffer = (char*) alloca(sc_get_buffer_length());
1077 sc_neg(a->value, buffer);
1078 return get_tarval_overflow(buffer, a->length, a->mode);
1080 case irms_float_number:
1081 /* it should be safe to enable this even if other arithmetic is disabled */
1083 return tarval_bad;*/
1085 fc_neg((const fp_value*) a->value, NULL);
1086 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1096 ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b)
1102 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1103 /* vector arithmetic not implemented yet */
1107 if (mode_is_reference(a->mode) && a->mode != b->mode) {
1108 b = tarval_convert_to(b, a->mode);
1109 } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
1110 a = tarval_convert_to(a, b->mode);
1113 assert(a->mode == b->mode);
1115 switch (get_mode_sort(a->mode)) {
1116 case irms_reference:
1117 case irms_int_number:
1118 /* modes of a,b are equal, so result has mode of a as this might be the character */
1119 buffer = (char*) alloca(sc_get_buffer_length());
1120 sc_add(a->value, b->value, buffer);
1121 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1122 return get_tarval_overflow(buffer, a->length, a->mode);
1124 case irms_float_number:
1128 fc_add((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1129 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1139 ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode)
1145 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1146 /* vector arithmetic not implemented yet */
1150 if (dst_mode != NULL) {
1151 if (a->mode != dst_mode)
1152 a = tarval_convert_to(a, dst_mode);
1153 if (b->mode != dst_mode)
1154 b = tarval_convert_to(b, dst_mode);
1156 assert(a->mode == b->mode);
1158 switch (get_mode_sort(a->mode)) {
1159 case irms_reference:
1160 case irms_int_number:
1161 /* modes of a,b are equal, so result has mode of a as this might be the character */
1162 buffer = (char*) alloca(sc_get_buffer_length());
1163 sc_sub(a->value, b->value, buffer);
1164 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1165 return get_tarval_overflow(buffer, a->length, a->mode);
1167 case irms_float_number:
1171 fc_sub((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1172 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1182 ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b)
1186 assert(a->mode == b->mode);
1190 if (get_mode_n_vector_elems(a->mode) > 1) {
1191 /* vector arithmetic not implemented yet */
1195 switch (get_mode_sort(a->mode)) {
1196 case irms_int_number:
1197 /* modes of a,b are equal */
1198 buffer = (char*) alloca(sc_get_buffer_length());
1199 sc_mul(a->value, b->value, buffer);
1200 return get_tarval_overflow(buffer, a->length, a->mode);
1202 case irms_float_number:
1206 fc_mul((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1207 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1216 * overflow is impossible, but look out for division by zero
1218 ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b)
1220 ir_mode *mode = a->mode;
1221 assert(mode == b->mode);
1225 if (get_mode_n_vector_elems(mode) > 1) {
1226 /* vector arithmetic not implemented yet */
1230 if (mode_is_int(mode)) {
1232 if (b == get_mode_null(mode))
1235 /* modes of a,b are equal */
1236 sc_div(a->value, b->value, NULL);
1237 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1239 assert(mode_is_float(mode));
1240 fc_div((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1241 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), mode);
1247 * overflow is impossible, but look out for division by zero
1249 ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b)
1251 assert((a->mode == b->mode) && mode_is_int(a->mode));
1255 if (get_mode_n_vector_elems(a->mode) > 1) {
1256 /* vector arithmetic not implemented yet */
1261 if (b == get_mode_null(b->mode)) return tarval_bad;
1262 /* modes of a,b are equal */
1263 sc_mod(a->value, b->value, NULL);
1264 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1268 * integer division AND remainder
1269 * overflow is impossible, but look out for division by zero
1271 ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod)
1273 int len = sc_get_buffer_length();
1274 char *div_res = (char*) alloca(len);
1275 char *mod_res = (char*) alloca(len);
1277 assert((a->mode == b->mode) && mode_is_int(a->mode));
1281 if (get_mode_n_vector_elems(a->mode) > 1) {
1282 /* vector arithmetic not implemented yet */
1288 if (b == get_mode_null(b->mode)) return tarval_bad;
1289 /* modes of a,b are equal */
1290 sc_divmod(a->value, b->value, div_res, mod_res);
1291 *mod = get_tarval(mod_res, len, a->mode);
1292 return get_tarval(div_res, len, a->mode);
1298 ir_tarval *tarval_abs(ir_tarval *a)
1303 assert(mode_is_num(a->mode));
1305 if (get_mode_n_vector_elems(a->mode) > 1) {
1306 /* vector arithmetic not implemented yet */
1310 switch (get_mode_sort(a->mode)) {
1311 case irms_int_number:
1312 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1313 buffer = (char*) alloca(sc_get_buffer_length());
1314 sc_neg(a->value, buffer);
1315 return get_tarval_overflow(buffer, a->length, a->mode);
1319 case irms_float_number:
1320 /* it should be safe to enable this even if other arithmetic is disabled */
1322 return tarval_bad;*/
1324 if (fc_comp((const fp_value*) a->value,
1325 (const fp_value*) get_mode_null(a->mode)->value) == -1) {
1326 fc_neg((const fp_value*) a->value, NULL);
1327 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1340 ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b)
1342 assert(a->mode == b->mode);
1344 /* works even for vector modes */
1347 switch (get_mode_sort(a->mode)) {
1348 case irms_internal_boolean:
1349 return (a == tarval_b_false) ? a : b;
1351 case irms_int_number:
1352 sc_and(a->value, b->value, NULL);
1353 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1356 panic("operation not defined on mode");
1360 ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b)
1362 assert(a->mode == b->mode);
1364 /* works even for vector modes */
1367 switch (get_mode_sort(a->mode)) {
1368 case irms_internal_boolean:
1369 return a == tarval_b_true && b == tarval_b_false ? tarval_b_true : tarval_b_false;
1371 case irms_int_number:
1372 sc_andnot(a->value, b->value, NULL);
1373 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1376 panic("operation not defined on mode");
1383 ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b)
1385 assert(a->mode == b->mode);
1387 /* works even for vector modes */
1390 switch (get_mode_sort(a->mode)) {
1391 case irms_internal_boolean:
1392 return (a == tarval_b_true) ? a : b;
1394 case irms_int_number:
1395 sc_or(a->value, b->value, NULL);
1396 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1399 panic("operation not defined on mode");
1404 * bitwise exclusive or (xor)
1406 ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b)
1408 assert((a->mode == b->mode));
1410 /* works even for vector modes */
1413 switch (get_mode_sort(a->mode)) {
1414 case irms_internal_boolean:
1415 return (a == b)? tarval_b_false : tarval_b_true;
1417 case irms_int_number:
1418 sc_xor(a->value, b->value, NULL);
1419 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1422 panic("operation not defined on mode");
1427 * bitwise left shift
1429 ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b)
1431 char *temp_val = NULL;
1433 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1437 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1438 /* vector arithmetic not implemented yet */
1442 if (get_mode_modulo_shift(a->mode) != 0) {
1443 temp_val = (char*) alloca(sc_get_buffer_length());
1445 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1446 sc_mod(b->value, temp_val, temp_val);
1448 temp_val = (char*)b->value;
1450 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1451 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1455 * bitwise unsigned right shift
1457 ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b)
1459 char *temp_val = NULL;
1461 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1465 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1466 /* vector arithmetic not implemented yet */
1470 if (get_mode_modulo_shift(a->mode) != 0) {
1471 temp_val = (char*) alloca(sc_get_buffer_length());
1473 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1474 sc_mod(b->value, temp_val, temp_val);
1476 temp_val = (char*)b->value;
1478 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1479 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1483 * bitwise signed right shift
1485 ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b)
1487 char *temp_val = NULL;
1489 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1493 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1494 /* vector arithmetic not implemented yet */
1498 if (get_mode_modulo_shift(a->mode) != 0) {
1499 temp_val = (char*) alloca(sc_get_buffer_length());
1501 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1502 sc_mod(b->value, temp_val, temp_val);
1504 temp_val = (char*)b->value;
1506 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1507 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1511 * bitwise rotation to left
1513 ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b)
1515 char *temp_val = NULL;
1517 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1521 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1522 /* vector arithmetic not implemented yet */
1526 if (get_mode_modulo_shift(a->mode) != 0) {
1527 temp_val = (char*) alloca(sc_get_buffer_length());
1529 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1530 sc_mod(b->value, temp_val, temp_val);
1532 temp_val = (char*)b->value;
1534 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1535 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1539 * carry flag of the last operation
1541 int tarval_carry(void)
1543 if (carry_flag == -1)
1544 panic("Carry undefined for the last operation");
1551 int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
1553 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1557 const tarval_mode_info *mode_info;
1558 const char *prefix, *suffix;
1560 mode_info = (const tarval_mode_info*) tv->mode->tv_priv;
1562 mode_info = &default_info;
1563 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1564 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1566 switch (get_mode_sort(tv->mode)) {
1567 case irms_reference:
1568 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1570 case irms_int_number:
1571 switch (mode_info->mode_output) {
1574 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1578 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1585 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1588 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1590 case irms_float_number:
1591 switch (mode_info->mode_output) {
1593 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1596 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1601 return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1604 case irms_internal_boolean:
1605 switch (mode_info->mode_output) {
1611 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1615 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1618 case irms_control_flow:
1620 case irms_auxiliary:
1621 if (tv == tarval_bad)
1622 return snprintf(buf, len, "<TV_BAD>");
1623 if (tv == tarval_undefined)
1624 return snprintf(buf, len, "<TV_UNDEF>");
1625 if (tv == tarval_unreachable)
1626 return snprintf(buf, len, "<TV_UNREACHABLE>");
1627 if (tv == tarval_reachable)
1628 return snprintf(buf, len, "<TV_REACHABLE>");
1629 return snprintf(buf, len, "<TV_??""?>");
1636 * Output of tarvals to stdio.
1638 int tarval_printf(ir_tarval *tv)
1643 res = tarval_snprintf(buf, sizeof(buf), tv);
1644 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1649 char *get_tarval_bitpattern(ir_tarval *tv)
1652 int n = get_mode_size_bits(tv->mode);
1653 int bytes = (n + 7) / 8;
1654 char *res = XMALLOCN(char, n + 1);
1657 for (i = 0; i < bytes; i++) {
1658 byte = get_tarval_sub_bits(tv, i);
1659 for (j = 1; j < 256; j <<= 1)
1661 res[pos++] = j & byte ? '1' : '0';
1670 * access to the bitpattern
1672 unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs)
1674 switch (get_mode_arithmetic(tv->mode)) {
1675 case irma_twos_complement:
1676 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1678 return fc_sub_bits((const fp_value*) tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1680 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1685 * Specify the output options of one mode.
1687 * This functions stores the modinfo, so DO NOT DESTROY it.
1689 * Returns zero on success.
1691 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1695 mode->tv_priv = modeinfo;
1700 * Returns the output options of one mode.
1702 * This functions returns the mode info of a given mode.
1704 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1708 return (const tarval_mode_info*) mode->tv_priv;
1712 * Returns non-zero if a given (integer) tarval has only one single bit
1715 int tarval_is_single_bit(ir_tarval *tv)
1720 if (!tv || tv == tarval_bad) return 0;
1721 if (! mode_is_int(tv->mode)) return 0;
1723 l = get_mode_size_bytes(tv->mode);
1724 for (bits = 0, i = l - 1; i >= 0; --i) {
1725 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1727 /* check for more than one bit in these */
1739 * Return the number of set bits in a given (integer) tarval.
1741 int get_tarval_popcount(ir_tarval *tv)
1746 if (!tv || tv == tarval_bad) return -1;
1747 if (! mode_is_int(tv->mode)) return -1;
1749 l = get_mode_size_bytes(tv->mode);
1750 for (bits = 0, i = l - 1; i >= 0; --i) {
1751 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1753 bits += popcount(v);
1759 * Return the number of the lowest set bit in a given (integer) tarval.
1761 * @param tv the tarval
1763 * @return number of lowest set bit or -1 on error
1765 int get_tarval_lowest_bit(ir_tarval *tv)
1769 if (!tv || tv == tarval_bad) return -1;
1770 if (! mode_is_int(tv->mode)) return -1;
1772 l = get_mode_size_bytes(tv->mode);
1773 for (i = 0; i < l; ++i) {
1774 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1777 return ntz(v) + i * 8;
1783 * Returns non-zero if the mantissa of a floating point IEEE-754
1784 * tarval is zero (i.e. 1.0Exxx)
1786 int tarval_ieee754_zero_mantissa(ir_tarval *tv)
1788 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1789 return fc_zero_mantissa((const fp_value*) tv->value);
1792 /* Returns the exponent of a floating point IEEE-754 tarval. */
1793 int tarval_ieee754_get_exponent(ir_tarval *tv)
1795 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1796 return fc_get_exponent((const fp_value*) tv->value);
1800 * Check if the tarval can be converted to the given mode without
1803 int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode)
1805 const ieee_descriptor_t *desc = get_descriptor(mode);
1806 return fc_can_lossless_conv_to((const fp_value*) tv->value, desc);
1809 /* Set the immediate precision for IEEE-754 results. */
1810 unsigned tarval_ieee754_set_immediate_precision(unsigned bits)
1812 return fc_set_immediate_precision(bits);
1815 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1816 unsigned tarval_ieee754_get_exact(void)
1818 return fc_is_exact();
1821 /* Return the size of the mantissa in bits (including possible
1822 implicit bits) for the given mode. */
1823 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode)
1825 const ieee_descriptor_t *desc;
1827 assert(get_mode_arithmetic(mode) == irma_ieee754);
1828 desc = get_descriptor(mode);
1830 return desc->mantissa_size + desc->explicit_one;
1833 /* check if its the a floating point NaN */
1834 int tarval_is_NaN(ir_tarval *tv)
1836 if (! mode_is_float(tv->mode))
1838 return fc_is_nan((const fp_value*) tv->value);
1841 /* check if its the a floating point +inf */
1842 int tarval_is_plus_inf(ir_tarval *tv)
1844 if (! mode_is_float(tv->mode))
1846 return fc_is_inf((const fp_value*) tv->value)
1847 && !fc_is_negative((const fp_value*) tv->value);
1850 /* check if its the a floating point -inf */
1851 int tarval_is_minus_inf(ir_tarval *tv)
1853 if (! mode_is_float(tv->mode))
1855 return fc_is_inf((const fp_value*) tv->value)
1856 && fc_is_negative((const fp_value*) tv->value);
1859 /* check if the tarval represents a finite value */
1860 int tarval_is_finite(ir_tarval *tv)
1862 if (mode_is_float(tv->mode))
1863 return !fc_is_nan((const fp_value*) tv->value)
1864 && !fc_is_inf((const fp_value*) tv->value);
1869 * Sets the overflow mode for integer operations.
1871 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)
1873 int_overflow_mode = ov_mode;
1876 /* Get the overflow mode for integer operations. */
1877 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void)
1879 return int_overflow_mode;
1882 /* Enable/Disable floating point constant folding. */
1883 void tarval_enable_fp_ops(int enable)
1888 int tarval_fp_ops_enabled(void)
1894 * default mode_info for output as HEX
1896 static const tarval_mode_info hex_output = {
1903 * Initialization of the tarval module: called before init_mode()
1905 void init_tarval_1(long null_value, int support_quad_precision)
1907 /* if these assertion fail, tarval_is_constant() will follow ... */
1908 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1909 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1911 _null_value = null_value;
1913 /* initialize the sets holding the tarvals with a comparison function and
1914 * an initial size, which is the expected number of constants */
1915 tarvals = new_set(cmp_tv, N_CONSTANTS);
1916 values = new_set(memcmp, N_CONSTANTS);
1917 /* calls init_strcalc() with needed size */
1918 init_fltcalc(support_quad_precision ? 112 : 64);
1922 * Initialization of the tarval module: called after init_mode()
1924 void init_tarval_2(void)
1926 tarval_bad->kind = k_tarval;
1927 tarval_bad->mode = mode_BAD;
1928 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1930 tarval_undefined->kind = k_tarval;
1931 tarval_undefined->mode = mode_ANY;
1932 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1934 tarval_b_true->kind = k_tarval;
1935 tarval_b_true->mode = mode_b;
1936 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1938 tarval_b_false->kind = k_tarval;
1939 tarval_b_false->mode = mode_b;
1940 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1942 tarval_unreachable->kind = k_tarval;
1943 tarval_unreachable->mode = mode_X;
1944 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1946 tarval_reachable->kind = k_tarval;
1947 tarval_reachable->mode = mode_X;
1948 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1951 * assign output modes that are compatible with the
1952 * old implementation: Hex output
1954 set_tarval_mode_output_option(mode_Bs, &hex_output);
1955 set_tarval_mode_output_option(mode_Bu, &hex_output);
1956 set_tarval_mode_output_option(mode_Hs, &hex_output);
1957 set_tarval_mode_output_option(mode_Hu, &hex_output);
1958 set_tarval_mode_output_option(mode_Is, &hex_output);
1959 set_tarval_mode_output_option(mode_Iu, &hex_output);
1960 set_tarval_mode_output_option(mode_Ls, &hex_output);
1961 set_tarval_mode_output_option(mode_Lu, &hex_output);
1962 set_tarval_mode_output_option(mode_P, &hex_output);
1965 /* free all memory occupied by tarval. */
1966 void finish_tarval(void)
1970 del_set(tarvals); tarvals = NULL;
1971 del_set(values); values = NULL;
1974 int (is_tarval)(const void *thing)
1976 return _is_tarval(thing);