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.
38 #include <assert.h> /* assertions */
39 #include <stdlib.h> /* atoi() */
41 # include <string.h> /* nice things for strings */
44 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
51 #include "set.h" /* to store tarvals in */
52 #include "entity_t.h" /* needed to store pointers to entities */
54 #include "irnode.h" /* defines boolean return values (pnc_number)*/
59 #include "firm_common.h"
62 /** Size of hash tables. Should correspond to average number of distinct constant
64 #define N_CONSTANTS 2048
66 /* get the integer overflow mode */
67 #define GET_OVERFLOW_MODE() int_overflow_mode
69 /* unused, float to int doesn't work yet */
70 enum float_to_int_mode {
75 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
77 #define SWITCH_NOINFINITY 0
78 #define SWITCH_NODENORMALS 0
80 /****************************************************************************
81 * local definitions and macros
82 ****************************************************************************/
84 # define TARVAL_VERIFY(a) tarval_verify((a))
86 # define TARVAL_VERIFY(a) ((void)0)
89 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
90 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
92 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
93 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
95 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
97 /****************************************************************************
99 ****************************************************************************/
100 static struct set *tarvals = NULL; /* container for tarval structs */
101 static struct set *values = NULL; /* container for values */
102 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
104 /** if this is set non-zero, the constant folding for floating point is OFF */
105 static int no_float = 0;
107 /****************************************************************************
109 ****************************************************************************/
111 static int hash_val(const void *value, unsigned int length);
112 static int hash_tv(tarval *tv);
113 static void _fail_verify(tarval *tv, const char* file, int line)
115 /* print a memory image of the tarval and throw an assertion */
117 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
119 printf("%s:%d: Invalid tarval (null)", file, line);
123 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
126 INLINE static void tarval_verify(tarval *tv)
132 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
133 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
135 if (!FIND_TARVAL(tv)) fail_verify(tv);
136 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
142 /** Hash a tarval. */
143 static int hash_tv(tarval *tv) {
144 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
147 /** Hash a value. Treat it as a byte array. */
148 static int hash_val(const void *value, unsigned int length) {
150 unsigned int hash = 0;
152 /* scramble the byte - array */
153 for (i = 0; i < length; ++i) {
154 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
155 hash += (hash << 11) ^ (hash >> 17);
161 static int cmp_tv(const void *p1, const void *p2, size_t n) {
162 const tarval *tv1 = p1;
163 const tarval *tv2 = p2;
166 assert(tv1->kind == k_tarval);
167 assert(tv2->kind == k_tarval);
168 if(tv1->mode < tv2->mode)
170 if(tv1->mode > tv2->mode)
172 if(tv1->length < tv2->length)
174 if(tv1->length > tv2->length)
176 if(tv1->value < tv2->value)
178 if(tv1->value > tv2->value)
184 /** finds tarval with value/mode or creates new tarval */
185 static tarval *get_tarval(const void *value, int length, ir_mode *mode) {
192 /* if there already is such a value, it is returned, else value
193 * is copied into the set */
194 char *temp = alloca(length);
195 memcpy(temp, value, length);
196 if (get_mode_arithmetic(mode) == irma_twos_complement) {
197 sign_extend(temp, mode);
199 tv.value = INSERT_VALUE(temp, length);
203 /* if there is such a tarval, it is returned, else tv is copied
205 return (tarval *)INSERT_TARVAL(&tv);
211 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
215 switch (get_mode_sort(mode)) {
217 /* addresses always wrap around */
218 temp = alloca(sc_get_buffer_length());
219 memcpy(temp, value, sc_get_buffer_length());
220 sc_truncate(get_mode_size_bits(mode), temp);
221 /* the sc_ module expects that all bits are set ... */
222 sign_extend(temp, mode);
223 return get_tarval(temp, length, mode);
225 case irms_int_number:
226 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
227 switch (GET_OVERFLOW_MODE()) {
228 case TV_OVERFLOW_SATURATE:
229 return get_mode_max(mode);
230 case TV_OVERFLOW_WRAP:
231 temp = alloca(sc_get_buffer_length());
232 memcpy(temp, value, sc_get_buffer_length());
233 sc_truncate(get_mode_size_bits(mode), temp);
234 /* the sc_ module expects that all bits are set ... */
235 sign_extend(temp, mode);
236 return get_tarval(temp, length, mode);
237 case TV_OVERFLOW_BAD:
240 return get_tarval(value, length, mode);
243 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
244 switch (GET_OVERFLOW_MODE()) {
245 case TV_OVERFLOW_SATURATE:
246 return get_mode_min(mode);
247 case TV_OVERFLOW_WRAP: {
248 char *temp = alloca(sc_get_buffer_length());
249 memcpy(temp, value, sc_get_buffer_length());
250 sc_truncate(get_mode_size_bits(mode), temp);
251 return get_tarval(temp, length, mode);
253 case TV_OVERFLOW_BAD:
256 return get_tarval(value, length, mode);
261 case irms_float_number:
262 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
263 /* clip infinity to maximum value */
264 return fc_is_negative(value) ? get_mode_min(mode) : get_mode_max(mode);
267 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
268 /* clip denormals to zero */
269 return get_mode_null(mode);
276 return get_tarval(value, length, mode);
280 * public variables declared in tv.h
282 static tarval reserved_tv[6];
284 tarval *tarval_b_false = &reserved_tv[0];
285 tarval *tarval_b_true = &reserved_tv[1];
286 tarval *tarval_bad = &reserved_tv[2];
287 tarval *tarval_undefined = &reserved_tv[3];
288 tarval *tarval_reachable = &reserved_tv[4];
289 tarval *tarval_unreachable = &reserved_tv[5];
292 * public functions declared in tv.h
296 * Constructors =============================================================
298 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
304 switch (get_mode_sort(mode)) {
305 case irms_control_flow:
311 case irms_internal_boolean:
312 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
313 if (strcasecmp(str, "true"))
314 return tarval_b_true;
315 else if (strcasecmp(str, "false"))
316 return tarval_b_true;
318 /* XXX This is C semantics */
319 return atoi(str) ? tarval_b_true : tarval_b_false;
321 case irms_float_number:
322 switch (get_mode_size_bits(mode)) {
324 fc_val_from_str(str, len, 8, 23, NULL);
327 fc_val_from_str(str, len, 11, 52, NULL);
331 fc_val_from_str(str, len, 15, 64, NULL);
334 panic("Unsupported mode in new_tarval_from_str()");
336 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
339 /* same as integer modes */
340 case irms_int_number:
341 sc_val_from_str(str, len, NULL, mode);
342 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
345 assert(0); /* can't be reached, can it? */
350 * helper function, create a tarval from long
352 tarval *new_tarval_from_long(long l, ir_mode *mode) {
355 switch (get_mode_sort(mode)) {
356 case irms_internal_boolean:
357 /* XXX C semantics ! */
358 return l ? tarval_b_true : tarval_b_false ;
361 /* same as integer modes */
362 case irms_int_number:
363 sc_val_from_long(l, NULL);
364 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
366 case irms_float_number:
367 return new_tarval_from_double((long double)l, mode);
370 assert(0 && "unsupported mode sort");
375 /* returns non-zero if can be converted to long */
376 int tarval_is_long(tarval *tv) {
377 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
380 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
381 /* the value might be too big to fit in a long */
382 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
383 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
384 /* really doesn't fit */
391 /* this might overflow the machine's long, so use only with small values */
392 long get_tarval_long(tarval* tv) {
393 assert(tarval_is_long(tv) && "tarval too big to fit in long");
395 return sc_val_to_long(tv->value);
398 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
399 assert(mode && (get_mode_sort(mode) == irms_float_number));
401 switch (get_mode_size_bits(mode)) {
403 fc_val_from_ieee754(d, 8, 23, NULL);
406 fc_val_from_ieee754(d, 11, 52, NULL);
410 fc_val_from_ieee754(d, 15, 64, NULL);
413 panic("Unsupported mode in new_tarval_from_double()");
415 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
418 /* returns non-zero if can be converted to double */
419 int tarval_is_double(tarval *tv) {
422 return (get_mode_sort(tv->mode) == irms_float_number);
425 long double get_tarval_double(tarval *tv) {
426 assert(tarval_is_double(tv));
428 return fc_val_to_ieee754(tv->value);
433 * Access routines for tarval fields ========================================
436 /* get the mode of the tarval */
437 ir_mode *(get_tarval_mode)(const tarval *tv) {
438 return _get_tarval_mode(tv);
442 * Special value query functions ============================================
444 * These functions calculate and return a tarval representing the requested
446 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
447 * functions, but these are stored on initialization of the irmode module and
448 * therefore the irmode functions should be preferred to the functions below.
451 tarval *(get_tarval_bad)(void) {
452 return _get_tarval_bad();
455 tarval *(get_tarval_undefined)(void) {
456 return _get_tarval_undefined();
459 tarval *(get_tarval_b_false)(void) {
460 return _get_tarval_b_false();
463 tarval *(get_tarval_b_true)(void) {
464 return _get_tarval_b_true();
467 tarval *(get_tarval_reachable)(void) {
468 return _get_tarval_reachable();
471 tarval *(get_tarval_unreachable)(void) {
472 return _get_tarval_unreachable();
475 tarval *get_tarval_max(ir_mode *mode) {
478 if (get_mode_n_vector_elems(mode) > 1) {
479 /* vector arithmetic not implemented yet */
483 switch(get_mode_sort(mode)) {
484 case irms_control_flow:
490 case irms_internal_boolean:
491 return tarval_b_true;
493 case irms_float_number:
494 switch(get_mode_size_bits(mode)) {
496 fc_get_max(8, 23, NULL);
499 fc_get_max(11, 52, NULL);
503 fc_get_max(15, 64, NULL);
506 panic("Unsupported mode in get_tarval_max()");
508 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
511 case irms_int_number:
512 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
513 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
518 tarval *get_tarval_min(ir_mode *mode) {
521 if (get_mode_n_vector_elems(mode) > 1) {
522 /* vector arithmetic not implemented yet */
526 switch(get_mode_sort(mode)) {
527 case irms_control_flow:
533 case irms_internal_boolean:
534 return tarval_b_false;
536 case irms_float_number:
537 switch(get_mode_size_bits(mode)) {
539 fc_get_min(8, 23, NULL);
542 fc_get_min(11, 52, NULL);
546 fc_get_min(15, 64, NULL);
549 panic("Unsupported mode in get_tarval_min()");
551 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
554 case irms_int_number:
555 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
556 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
561 /** The bit pattern for the pointer NULL */
562 static long _null_value = 0;
564 tarval *get_tarval_null(ir_mode *mode) {
567 if (get_mode_n_vector_elems(mode) > 1) {
568 /* vector arithmetic not implemented yet */
572 switch(get_mode_sort(mode)) {
573 case irms_control_flow:
579 case irms_float_number:
580 return new_tarval_from_double(0.0, mode);
582 case irms_internal_boolean:
583 case irms_int_number:
584 return new_tarval_from_long(0l, mode);
587 return new_tarval_from_long(_null_value, mode);
592 tarval *get_tarval_one(ir_mode *mode) {
595 if (get_mode_n_vector_elems(mode) > 1) {
596 /* vector arithmetic not implemented yet */
601 switch(get_mode_sort(mode)) {
602 case irms_control_flow:
608 case irms_internal_boolean:
609 return tarval_b_true;
611 case irms_float_number:
612 return new_tarval_from_double(1.0, mode);
615 case irms_int_number:
616 return new_tarval_from_long(1l, mode);
621 tarval *get_tarval_all_one(ir_mode *mode) {
624 if (get_mode_n_vector_elems(mode) > 1) {
625 /* vector arithmetic not implemented yet */
630 switch(get_mode_sort(mode)) {
631 case irms_control_flow:
637 case irms_int_number:
638 case irms_internal_boolean:
640 return tarval_not(get_mode_null(mode));
643 case irms_float_number:
644 return new_tarval_from_double(1.0, mode);
649 int tarval_is_constant(tarval *tv) {
650 int num_res = sizeof(reserved_tv) / sizeof(reserved_tv[0]);
652 /* reserved tarvals are NOT constants. Note that although
653 tarval_b_true and tarval_b_false are reserved, they are constants of course. */
654 return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
657 tarval *get_tarval_minus_one(ir_mode *mode) {
660 if (get_mode_n_vector_elems(mode) > 1) {
661 /* vector arithmetic not implemented yet */
665 switch(get_mode_sort(mode)) {
666 case irms_control_flow:
669 case irms_internal_boolean:
676 case irms_float_number:
677 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
679 case irms_int_number:
680 return new_tarval_from_long(-1l, mode);
685 tarval *get_tarval_nan(ir_mode *mode) {
688 if (get_mode_n_vector_elems(mode) > 1) {
689 /* vector arithmetic not implemented yet */
693 if (get_mode_sort(mode) == irms_float_number) {
694 switch(get_mode_size_bits(mode)) {
696 fc_get_qnan(8, 23, NULL);
699 fc_get_qnan(11, 52, NULL);
703 fc_get_qnan(15, 64, NULL);
706 panic("Unsupported mode in get_tarval_nan()");
708 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
710 assert(0 && "tarval is not floating point");
715 tarval *get_tarval_plus_inf(ir_mode *mode) {
718 if (get_mode_n_vector_elems(mode) > 1) {
719 /* vector arithmetic not implemented yet */
723 if (get_mode_sort(mode) == irms_float_number) {
724 switch(get_mode_size_bits(mode)) {
726 fc_get_plusinf(8, 23, NULL);
729 fc_get_plusinf(11, 52, NULL);
733 fc_get_plusinf(15, 64, NULL);
736 panic("Unsupported mode in get_tarval_plus_inf()");
738 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
740 assert(0 && "tarval is not floating point");
745 tarval *get_tarval_minus_inf(ir_mode *mode) {
748 if (get_mode_n_vector_elems(mode) > 1) {
749 /* vector arithmetic not implemented yet */
753 if (get_mode_sort(mode) == irms_float_number) {
754 switch(get_mode_size_bits(mode)) {
756 fc_get_minusinf(8, 23, NULL);
759 fc_get_minusinf(11, 52, NULL);
763 fc_get_minusinf(15, 64, NULL);
766 panic("Unsupported mode in get_tarval_minus_inf()");
768 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
770 assert(0 && "tarval is not floating point");
776 * Arithmethic operations on tarvals ========================================
780 * test if negative number, 1 means 'yes'
782 int tarval_is_negative(tarval *a) {
785 if (get_mode_n_vector_elems(a->mode) > 1) {
786 /* vector arithmetic not implemented yet */
787 assert(0 && "tarval_is_negative is not allowed for vector modes");
791 switch (get_mode_sort(a->mode)) {
792 case irms_int_number:
793 if (!mode_is_signed(a->mode)) return 0;
795 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
797 case irms_float_number:
798 return fc_is_negative(a->value);
801 assert(0 && "not implemented");
807 * test if null, 1 means 'yes'
809 int tarval_is_null(tarval *a) {
812 a == get_mode_null(get_tarval_mode(a));
816 * test if one, 1 means 'yes'
818 int tarval_is_one(tarval *a) {
821 a == get_mode_one(get_tarval_mode(a));
824 int tarval_is_all_one(tarval *tv) {
827 tv == get_mode_all_one(get_tarval_mode(tv));
831 * test if one, 1 means 'yes'
833 int tarval_is_minus_one(tarval *a) {
836 a == get_mode_minus_one(get_tarval_mode(a));
842 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
846 if (a == tarval_bad || b == tarval_bad) {
847 assert(0 && "Comparison with tarval_bad");
851 if (a == tarval_undefined || b == tarval_undefined)
854 if (a->mode != b->mode)
857 if (get_mode_n_vector_elems(a->mode) > 1) {
858 /* vector arithmetic not implemented yet */
859 assert(0 && "cmp not implemented for vector modes");
862 /* Here the two tarvals are unequal and of the same mode */
863 switch (get_mode_sort(a->mode)) {
864 case irms_control_flow:
871 case irms_float_number:
872 /* it should be safe to enable this even if other arithmetic is disabled */
874 return pn_Cmp_False;*/
876 * BEWARE: we cannot compare a == b here, because
877 * a NaN is always Unordered to any other value, even to itself!
879 switch (fc_comp(a->value, b->value)) {
880 case -1: return pn_Cmp_Lt;
881 case 0: return pn_Cmp_Eq;
882 case 1: return pn_Cmp_Gt;
883 case 2: return pn_Cmp_Uo;
884 default: return pn_Cmp_False;
887 case irms_int_number:
890 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
892 case irms_internal_boolean:
895 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
901 * convert to other mode
903 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
910 if (src->mode == dst_mode)
913 if (get_mode_n_vector_elems(src->mode) > 1) {
914 /* vector arithmetic not implemented yet */
918 switch (get_mode_sort(src->mode)) {
919 case irms_control_flow:
924 /* cast float to something */
925 case irms_float_number:
926 switch (get_mode_sort(dst_mode)) {
927 case irms_float_number:
928 switch (get_mode_size_bits(dst_mode)) {
930 fc_cast(src->value, 8, 23, NULL);
933 fc_cast(src->value, 11, 52, NULL);
937 fc_cast(src->value, 15, 64, NULL);
940 panic("Unsupported mode in tarval_convert_to()");
942 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
944 case irms_int_number:
945 switch (GET_FLOAT_TO_INT_MODE()) {
947 res = fc_int(src->value, NULL);
950 res = fc_rnd(src->value, NULL);
953 panic("Unsupported float to int conversion mode in tarval_convert_to()");
956 buffer = alloca(sc_get_buffer_length());
957 if (! fc_flt2int(res, buffer, dst_mode))
959 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
962 /* the rest can't be converted */
967 /* cast int/characters to something */
968 case irms_int_number:
969 switch (get_mode_sort(dst_mode)) {
972 case irms_int_number:
973 buffer = alloca(sc_get_buffer_length());
974 memcpy(buffer, src->value, sc_get_buffer_length());
975 sign_extend(buffer, dst_mode);
976 return get_tarval_overflow(buffer, src->length, dst_mode);
978 case irms_internal_boolean:
979 /* XXX C semantics */
980 if (src == get_mode_null(src->mode)) return tarval_b_false;
981 else return tarval_b_true;
983 case irms_float_number:
984 /* XXX floating point unit does not understand internal integer
985 * representation, convert to string first, then create float from
987 buffer = alloca(100);
988 /* decimal string representation because hexadecimal output is
989 * interpreted unsigned by fc_val_from_str, so this is a HACK */
990 snprintf(buffer, 100, "%s",
991 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
992 buffer[100 - 1] = '\0';
993 switch (get_mode_size_bits(dst_mode)) {
995 fc_val_from_str(buffer, 0, 8, 23, NULL);
998 fc_val_from_str(buffer, 0, 11, 52, NULL);
1002 fc_val_from_str(buffer, 0, 15, 64, NULL);
1005 panic("Unsupported mode in tarval_convert_to()");
1007 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
1014 case irms_internal_boolean:
1015 /* beware: this is C semantic for the INTERNAL boolean mode */
1016 if (get_mode_sort(dst_mode) == irms_int_number)
1017 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
1020 case irms_reference:
1021 if (get_mode_sort(dst_mode) == irms_int_number) {
1022 buffer = alloca(sc_get_buffer_length());
1023 memcpy(buffer, src->value, sc_get_buffer_length());
1024 sign_extend(buffer, src->mode);
1025 return get_tarval_overflow(buffer, src->length, dst_mode);
1036 tarval *tarval_not(tarval *a) {
1041 /* works for vector mode without changes */
1043 switch (get_mode_sort(a->mode)) {
1044 case irms_reference:
1045 case irms_int_number:
1046 buffer = alloca(sc_get_buffer_length());
1047 sc_not(a->value, buffer);
1048 return get_tarval(buffer, a->length, a->mode);
1050 case irms_internal_boolean:
1051 if (a == tarval_b_true)
1052 return tarval_b_false;
1053 if (a == tarval_b_false)
1054 return tarval_b_true;
1058 assert(0 && "bitwise negation is only allowed for integer and boolean");
1064 * arithmetic negation
1066 tarval *tarval_neg(tarval *a) {
1070 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1072 /* note: negation is allowed even for unsigned modes. */
1074 if (get_mode_n_vector_elems(a->mode) > 1) {
1075 /* vector arithmetic not implemented yet */
1079 switch (get_mode_sort(a->mode)) {
1080 case irms_int_number:
1081 buffer = alloca(sc_get_buffer_length());
1082 sc_neg(a->value, buffer);
1083 return get_tarval_overflow(buffer, a->length, a->mode);
1085 case irms_float_number:
1086 /* it should be safe to enable this even if other arithmetic is disabled */
1088 return tarval_bad;*/
1090 fc_neg(a->value, NULL);
1091 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1101 tarval *tarval_add(tarval *a, tarval *b) {
1104 ir_mode *dst_mode, *src_mode = NULL;
1109 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1110 /* vector arithmetic not implemented yet */
1114 if (mode_is_reference(a->mode)) {
1116 dst_mode = find_unsigned_mode(a->mode);
1118 if (dst_mode == NULL)
1121 a = tarval_convert_to(a, dst_mode);
1122 b = tarval_convert_to(b, dst_mode);
1124 if (mode_is_reference(b->mode)) {
1126 dst_mode = find_unsigned_mode(b->mode);
1131 a = tarval_convert_to(a, dst_mode);
1132 b = tarval_convert_to(b, dst_mode);
1135 assert(a->mode == b->mode);
1137 switch (get_mode_sort(a->mode)) {
1138 case irms_int_number:
1139 /* modes of a,b are equal, so result has mode of a as this might be the character */
1140 buffer = alloca(sc_get_buffer_length());
1141 sc_add(a->value, b->value, buffer);
1142 res = get_tarval_overflow(buffer, a->length, a->mode);
1145 case irms_float_number:
1149 fc_add(a->value, b->value, NULL);
1150 res = get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1156 if (src_mode != NULL)
1157 return tarval_convert_to(res, src_mode);
1164 tarval *tarval_sub(tarval *a, tarval *b) {
1167 ir_mode *dst_mode, *src_mode = NULL;
1172 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1173 /* vector arithmetic not implemented yet */
1176 if (mode_is_reference(a->mode)) {
1178 dst_mode = find_unsigned_mode(a->mode);
1180 if (dst_mode == NULL)
1183 a = tarval_convert_to(a, dst_mode);
1184 b = tarval_convert_to(b, dst_mode);
1186 if (mode_is_reference(b->mode)) {
1188 dst_mode = find_unsigned_mode(b->mode);
1193 a = tarval_convert_to(a, dst_mode);
1194 b = tarval_convert_to(b, dst_mode);
1197 assert(a->mode == b->mode);
1199 switch (get_mode_sort(a->mode)) {
1200 case irms_int_number:
1201 /* modes of a,b are equal, so result has mode of a as this might be the character */
1202 buffer = alloca(sc_get_buffer_length());
1203 sc_sub(a->value, b->value, buffer);
1204 res = get_tarval_overflow(buffer, a->length, a->mode);
1207 case irms_float_number:
1211 fc_sub(a->value, b->value, NULL);
1212 res = get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1218 if (src_mode != NULL)
1219 return tarval_convert_to(res, src_mode);
1226 tarval *tarval_mul(tarval *a, tarval *b) {
1231 assert(a->mode == b->mode);
1233 if (get_mode_n_vector_elems(a->mode) > 1) {
1234 /* vector arithmetic not implemented yet */
1238 switch (get_mode_sort(a->mode)) {
1239 case irms_int_number:
1240 /* modes of a,b are equal */
1241 buffer = alloca(sc_get_buffer_length());
1242 sc_mul(a->value, b->value, buffer);
1243 return get_tarval_overflow(buffer, a->length, a->mode);
1245 case irms_float_number:
1249 fc_mul(a->value, b->value, NULL);
1250 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1258 * floating point division
1260 tarval *tarval_quo(tarval *a, tarval *b) {
1263 assert((a->mode == b->mode) && mode_is_float(a->mode));
1268 if (get_mode_n_vector_elems(a->mode) > 1) {
1269 /* vector arithmetic not implemented yet */
1273 fc_div(a->value, b->value, NULL);
1274 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1279 * overflow is impossible, but look out for division by zero
1281 tarval *tarval_div(tarval *a, tarval *b) {
1284 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 */
1292 if (b == get_mode_null(b->mode)) return tarval_bad;
1293 /* modes of a,b are equal */
1294 sc_div(a->value, b->value, NULL);
1295 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1300 * overflow is impossible, but look out for division by zero
1302 tarval *tarval_mod(tarval *a, tarval *b) {
1305 assert((a->mode == b->mode) && mode_is_int(a->mode));
1307 if (get_mode_n_vector_elems(a->mode) > 1) {
1308 /* vector arithmetic not implemented yet */
1313 if (b == get_mode_null(b->mode)) return tarval_bad;
1314 /* modes of a,b are equal */
1315 sc_mod(a->value, b->value, NULL);
1316 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1320 * integer division AND remainder
1321 * overflow is impossible, but look out for division by zero
1323 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1324 int len = sc_get_buffer_length();
1325 char *div_res = alloca(len);
1326 char *mod_res = alloca(len);
1330 assert((a->mode == b->mode) && mode_is_int(a->mode));
1332 if (get_mode_n_vector_elems(a->mode) > 1) {
1333 /* vector arithmetic not implemented yet */
1339 if (b == get_mode_null(b->mode)) return tarval_bad;
1340 /* modes of a,b are equal */
1341 sc_divmod(a->value, b->value, div_res, mod_res);
1342 *mod = get_tarval(mod_res, len, a->mode);
1343 return get_tarval(div_res, len, a->mode);
1349 tarval *tarval_abs(tarval *a) {
1353 assert(mode_is_num(a->mode));
1355 if (get_mode_n_vector_elems(a->mode) > 1) {
1356 /* vector arithmetic not implemented yet */
1360 switch (get_mode_sort(a->mode)) {
1361 case irms_int_number:
1362 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1363 buffer = alloca(sc_get_buffer_length());
1364 sc_neg(a->value, buffer);
1365 return get_tarval_overflow(buffer, a->length, a->mode);
1369 case irms_float_number:
1370 /* it should be safe to enable this even if other arithmetic is disabled */
1372 return tarval_bad;*/
1374 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1375 fc_neg(a->value, NULL);
1376 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1389 tarval *tarval_and(tarval *a, tarval *b) {
1392 assert(a->mode == b->mode);
1394 /* works even for vector modes */
1396 switch(get_mode_sort(a->mode)) {
1397 case irms_internal_boolean:
1398 return (a == tarval_b_false) ? a : b;
1400 case irms_int_number:
1401 sc_and(a->value, b->value, NULL);
1402 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1405 assert(0 && "operation not defined on mode");
1413 tarval *tarval_or(tarval *a, tarval *b) {
1416 assert(a->mode == b->mode);
1418 /* works even for vector modes */
1420 switch (get_mode_sort(a->mode)) {
1421 case irms_internal_boolean:
1422 return (a == tarval_b_true) ? a : b;
1424 case irms_int_number:
1425 sc_or(a->value, b->value, NULL);
1426 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1429 assert(0 && "operation not defined on mode");
1435 * bitwise exclusive or (xor)
1437 tarval *tarval_eor(tarval *a, tarval *b) {
1440 assert((a->mode == b->mode));
1442 /* works even for vector modes */
1444 switch (get_mode_sort(a->mode)) {
1445 case irms_internal_boolean:
1446 return (a == b)? tarval_b_false : tarval_b_true;
1448 case irms_int_number:
1449 sc_xor(a->value, b->value, NULL);
1450 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1453 assert(0 && "operation not defined on mode");
1459 * bitwise left shift
1461 tarval *tarval_shl(tarval *a, tarval *b) {
1462 char *temp_val = NULL;
1466 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1468 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1469 /* vector arithmetic not implemented yet */
1473 if (get_mode_modulo_shift(a->mode) != 0) {
1474 temp_val = alloca(sc_get_buffer_length());
1476 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1477 sc_mod(b->value, temp_val, temp_val);
1479 temp_val = (char*)b->value;
1481 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1482 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1486 * bitwise unsigned right shift
1488 tarval *tarval_shr(tarval *a, tarval *b) {
1489 char *temp_val = NULL;
1493 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1495 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1496 /* vector arithmetic not implemented yet */
1500 if (get_mode_modulo_shift(a->mode) != 0) {
1501 temp_val = alloca(sc_get_buffer_length());
1503 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1504 sc_mod(b->value, temp_val, temp_val);
1506 temp_val = (char*)b->value;
1508 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1509 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1513 * bitwise signed right shift
1515 tarval *tarval_shrs(tarval *a, tarval *b) {
1516 char *temp_val = NULL;
1520 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1522 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1523 /* vector arithmetic not implemented yet */
1527 if (get_mode_modulo_shift(a->mode) != 0) {
1528 temp_val = alloca(sc_get_buffer_length());
1530 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1531 sc_mod(b->value, temp_val, temp_val);
1533 temp_val = (char*)b->value;
1535 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1536 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1540 * bitwise rotation to left
1542 tarval *tarval_rotl(tarval *a, tarval *b) {
1543 char *temp_val = NULL;
1547 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1549 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1550 /* vector arithmetic not implemented yet */
1554 if (get_mode_modulo_shift(a->mode) != 0) {
1555 temp_val = alloca(sc_get_buffer_length());
1557 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1558 sc_mod(b->value, temp_val, temp_val);
1560 temp_val = (char*)b->value;
1562 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1563 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1567 * carry flag of the last operation
1569 int tarval_carry(void) {
1570 panic("tarval_carry() requetsed: not implemented on all operations");
1571 return sc_had_carry();
1577 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1578 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1582 const tarval_mode_info *mode_info;
1583 const char *prefix, *suffix;
1585 mode_info = tv->mode->tv_priv;
1587 mode_info = &default_info;
1588 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1589 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1591 switch (get_mode_sort(tv->mode)) {
1592 case irms_reference:
1593 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1595 case irms_int_number:
1596 switch (mode_info->mode_output) {
1599 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1603 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1609 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1612 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1614 case irms_float_number:
1615 switch (mode_info->mode_output) {
1617 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1620 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1625 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1629 case irms_internal_boolean:
1630 switch (mode_info->mode_output) {
1636 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1640 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1643 case irms_control_flow:
1645 case irms_auxiliary:
1646 if (tv == tarval_bad)
1647 return snprintf(buf, len, "<TV_BAD>");
1648 if (tv == tarval_undefined)
1649 return snprintf(buf, len, "<TV_UNDEF>");
1650 if (tv == tarval_unreachable)
1651 return snprintf(buf, len, "<TV_UNREACHABLE>");
1652 if (tv == tarval_reachable)
1653 return snprintf(buf, len, "<TV_REACHABLE>");
1654 return snprintf(buf, len, "<TV_??""?>");
1661 * Output of tarvals to stdio.
1663 int tarval_printf(tarval *tv) {
1667 res = tarval_snprintf(buf, sizeof(buf), tv);
1668 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1673 char *get_tarval_bitpattern(tarval *tv) {
1675 int n = get_mode_size_bits(tv->mode);
1676 int bytes = (n + 7) / 8;
1677 char *res = xmalloc((n + 1) * sizeof(char));
1680 for(i = 0; i < bytes; i++) {
1681 byte = get_tarval_sub_bits(tv, i);
1682 for(j = 1; j < 256; j <<= 1)
1684 res[pos++] = j & byte ? '1' : '0';
1693 * access to the bitpattern
1695 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1696 switch (get_mode_arithmetic(tv->mode)) {
1697 case irma_twos_complement:
1698 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1700 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1702 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1707 * Specify the output options of one mode.
1709 * This functions stores the modinfo, so DO NOT DESTROY it.
1711 * Returns zero on success.
1713 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1716 mode->tv_priv = modeinfo;
1721 * Returns the output options of one mode.
1723 * This functions returns the mode info of a given mode.
1725 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1728 return mode->tv_priv;
1732 * Returns non-zero if a given (integer) tarval has only one single bit
1735 int tarval_is_single_bit(tarval *tv) {
1739 if (!tv || tv == tarval_bad) return 0;
1740 if (! mode_is_int(tv->mode)) return 0;
1742 l = get_mode_size_bytes(tv->mode);
1743 for (bits = 0, i = l - 1; i >= 0; --i) {
1744 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1746 /* check for more than one bit in these */
1758 * Returns non-zero if the mantissa of a floating point IEEE-754
1759 * tarval is zero (i.e. 1.0Exxx)
1761 int tarval_ieee754_zero_mantissa(tarval *tv) {
1762 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1763 return fc_zero_mantissa(tv->value);
1766 /* Returns the exponent of a floating point IEEE-754 tarval. */
1767 int tarval_ieee754_get_exponent(tarval *tv) {
1768 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1769 return fc_get_exponent(tv->value);
1773 * Check if the tarval can be converted to the given mode without
1776 int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode) {
1777 char exp_size, mant_size;
1778 switch (get_mode_size_bits(mode)) {
1780 exp_size = 8; mant_size = 23;
1783 exp_size = 11; mant_size = 52;
1787 exp_size = 15; mant_size = 64;
1790 panic("Unsupported mode in tarval_ieee754_can_conv_lossless()");
1793 return fc_can_lossless_conv_to(tv->value, exp_size, mant_size);
1796 /* Set the immediate precision for IEEE-754 results. */
1797 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1798 return fc_set_immediate_precision(bits);
1801 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1802 unsigned tarval_ieee754_get_exact(void) {
1803 return fc_is_exact();
1806 /* check if its the a floating point NaN */
1807 int tarval_is_NaN(tarval *tv) {
1808 if (! mode_is_float(tv->mode))
1810 return fc_is_nan(tv->value);
1813 /* check if its the a floating point +inf */
1814 int tarval_is_plus_inf(tarval *tv) {
1815 if (! mode_is_float(tv->mode))
1817 return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1820 /* check if its the a floating point -inf */
1821 int tarval_is_minus_inf(tarval *tv) {
1822 if (! mode_is_float(tv->mode))
1824 return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1827 /* check if the tarval represents a finite value */
1828 int tarval_is_finite(tarval *tv) {
1829 if (mode_is_float(tv->mode))
1830 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1835 * Sets the overflow mode for integer operations.
1837 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1838 int_overflow_mode = ov_mode;
1841 /* Get the overflow mode for integer operations. */
1842 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1843 return int_overflow_mode;
1846 /* Enable/Disable floating point constant folding. */
1847 int tarval_enable_fp_ops(int enable) {
1848 int old = !no_float;
1855 * default mode_info for output as HEX
1857 static const tarval_mode_info hex_output = {
1864 * Initialization of the tarval module: called before init_mode()
1866 void init_tarval_1(long null_value) {
1867 /* if these assertion fail, tarval_is_constant() will follow ... */
1868 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1869 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1871 _null_value = null_value;
1873 /* initialize the sets holding the tarvals with a comparison function and
1874 * an initial size, which is the expected number of constants */
1875 tarvals = new_set(cmp_tv, N_CONSTANTS);
1876 values = new_set(memcmp, N_CONSTANTS);
1877 /* init strcalc with precision of 68 to support floating point values with 64
1878 * bit mantissa (needs extra bits for rounding and overflow) */
1884 * Initialization of the tarval module: called after init_mode()
1886 void init_tarval_2(void) {
1887 tarval_bad->kind = k_tarval;
1888 tarval_bad->mode = mode_BAD;
1889 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1891 tarval_undefined->kind = k_tarval;
1892 tarval_undefined->mode = mode_ANY;
1893 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1895 tarval_b_true->kind = k_tarval;
1896 tarval_b_true->mode = mode_b;
1897 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1899 tarval_b_false->kind = k_tarval;
1900 tarval_b_false->mode = mode_b;
1901 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1903 tarval_unreachable->kind = k_tarval;
1904 tarval_unreachable->mode = mode_X;
1905 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1907 tarval_reachable->kind = k_tarval;
1908 tarval_reachable->mode = mode_X;
1909 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1912 * assign output modes that are compatible with the
1913 * old implementation: Hex output
1915 set_tarval_mode_output_option(mode_Bs, &hex_output);
1916 set_tarval_mode_output_option(mode_Bu, &hex_output);
1917 set_tarval_mode_output_option(mode_Hs, &hex_output);
1918 set_tarval_mode_output_option(mode_Hu, &hex_output);
1919 set_tarval_mode_output_option(mode_Is, &hex_output);
1920 set_tarval_mode_output_option(mode_Iu, &hex_output);
1921 set_tarval_mode_output_option(mode_Ls, &hex_output);
1922 set_tarval_mode_output_option(mode_Lu, &hex_output);
1923 set_tarval_mode_output_option(mode_P, &hex_output);
1926 /* free all memory occupied by tarval. */
1927 void finish_tarval(void) {
1930 del_set(tarvals); tarvals = NULL;
1931 del_set(values); values = NULL;
1934 int (is_tarval)(const void *thing) {
1935 return _is_tarval(thing);
1938 /****************************************************************************
1940 ****************************************************************************/