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[4];
284 tarval *tarval_bad = &reserved_tv[0];
285 tarval *tarval_undefined = &reserved_tv[1];
286 tarval *tarval_b_false = &reserved_tv[2];
287 tarval *tarval_b_true = &reserved_tv[3];
290 * public functions declared in tv.h
294 * Constructors =============================================================
296 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
302 switch (get_mode_sort(mode)) {
303 case irms_control_flow:
309 case irms_internal_boolean:
310 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
311 if (strcasecmp(str, "true")) return tarval_b_true;
312 else if (strcasecmp(str, "false")) return tarval_b_true;
314 /* XXX This is C semantics */
315 return atoi(str) ? tarval_b_true : tarval_b_false;
317 case irms_float_number:
318 switch (get_mode_size_bits(mode)) {
320 fc_val_from_str(str, len, 8, 23, NULL);
323 fc_val_from_str(str, len, 11, 52, NULL);
327 fc_val_from_str(str, len, 15, 64, NULL);
330 panic("Unsupported mode in new_tarval_from_str()");
332 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
335 /* same as integer modes */
336 case irms_int_number:
337 sc_val_from_str(str, len, NULL, mode);
338 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
341 assert(0); /* can't be reached, can it? */
346 * helper function, create a tarval from long
348 tarval *new_tarval_from_long(long l, ir_mode *mode) {
351 switch (get_mode_sort(mode)) {
352 case irms_internal_boolean:
353 /* XXX C semantics ! */
354 return l ? tarval_b_true : tarval_b_false ;
357 /* same as integer modes */
358 case irms_int_number:
359 sc_val_from_long(l, NULL);
360 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
362 case irms_float_number:
363 return new_tarval_from_double((long double)l, mode);
366 assert(0 && "unsupported mode sort");
371 /* returns non-zero if can be converted to long */
372 int tarval_is_long(tarval *tv) {
373 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
376 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
377 /* the value might be too big to fit in a long */
378 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
379 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
380 /* really doesn't fit */
387 /* this might overflow the machine's long, so use only with small values */
388 long get_tarval_long(tarval* tv) {
389 assert(tarval_is_long(tv) && "tarval too big to fit in long");
391 return sc_val_to_long(tv->value);
394 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
395 assert(mode && (get_mode_sort(mode) == irms_float_number));
397 switch (get_mode_size_bits(mode)) {
399 fc_val_from_ieee754(d, 8, 23, NULL);
402 fc_val_from_ieee754(d, 11, 52, NULL);
406 fc_val_from_ieee754(d, 15, 64, NULL);
409 panic("Unsupported mode in new_tarval_from_double()");
411 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
414 /* returns non-zero if can be converted to double */
415 int tarval_is_double(tarval *tv) {
418 return (get_mode_sort(tv->mode) == irms_float_number);
421 long double get_tarval_double(tarval *tv) {
422 assert(tarval_is_double(tv));
424 return fc_val_to_ieee754(tv->value);
429 * Access routines for tarval fields ========================================
432 /* get the mode of the tarval */
433 ir_mode *(get_tarval_mode)(const tarval *tv) {
434 return _get_tarval_mode(tv);
438 * Special value query functions ============================================
440 * These functions calculate and return a tarval representing the requested
442 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
443 * functions, but these are stored on initialization of the irmode module and
444 * therefore the irmode functions should be preferred to the functions below.
447 tarval *(get_tarval_bad)(void) {
448 return _get_tarval_bad();
451 tarval *(get_tarval_undefined)(void) {
452 return _get_tarval_undefined();
455 tarval *(get_tarval_b_false)(void) {
456 return _get_tarval_b_false();
459 tarval *(get_tarval_b_true)(void) {
460 return _get_tarval_b_true();
463 tarval *get_tarval_max(ir_mode *mode) {
466 if (get_mode_n_vector_elems(mode) > 1) {
467 /* vector arithmetic not implemented yet */
471 switch(get_mode_sort(mode)) {
472 case irms_control_flow:
478 case irms_internal_boolean:
479 return tarval_b_true;
481 case irms_float_number:
482 switch(get_mode_size_bits(mode)) {
484 fc_get_max(8, 23, NULL);
487 fc_get_max(11, 52, NULL);
491 fc_get_max(15, 64, NULL);
494 panic("Unsupported mode in get_tarval_max()");
496 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
499 case irms_int_number:
500 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
501 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
506 tarval *get_tarval_min(ir_mode *mode) {
509 if (get_mode_n_vector_elems(mode) > 1) {
510 /* vector arithmetic not implemented yet */
514 switch(get_mode_sort(mode)) {
515 case irms_control_flow:
521 case irms_internal_boolean:
522 return tarval_b_false;
524 case irms_float_number:
525 switch(get_mode_size_bits(mode)) {
527 fc_get_min(8, 23, NULL);
530 fc_get_min(11, 52, NULL);
534 fc_get_min(15, 64, NULL);
537 panic("Unsupported mode in get_tarval_min()");
539 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
542 case irms_int_number:
543 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
544 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
549 /** The bit pattern for the pointer NULL */
550 static long _null_value = 0;
552 tarval *get_tarval_null(ir_mode *mode) {
555 if (get_mode_n_vector_elems(mode) > 1) {
556 /* vector arithmetic not implemented yet */
560 switch(get_mode_sort(mode)) {
561 case irms_control_flow:
567 case irms_float_number:
568 return new_tarval_from_double(0.0, mode);
570 case irms_internal_boolean:
571 case irms_int_number:
572 return new_tarval_from_long(0l, mode);
575 return new_tarval_from_long(_null_value, mode);
580 tarval *get_tarval_one(ir_mode *mode) {
583 if (get_mode_n_vector_elems(mode) > 1) {
584 /* vector arithmetic not implemented yet */
589 switch(get_mode_sort(mode)) {
590 case irms_control_flow:
596 case irms_internal_boolean:
597 return tarval_b_true;
599 case irms_float_number:
600 return new_tarval_from_double(1.0, mode);
603 case irms_int_number:
604 return new_tarval_from_long(1l, mode);
609 tarval *get_tarval_all_one(ir_mode *mode) {
612 if (get_mode_n_vector_elems(mode) > 1) {
613 /* vector arithmetic not implemented yet */
618 switch(get_mode_sort(mode)) {
619 case irms_control_flow:
625 case irms_int_number:
626 case irms_internal_boolean:
628 return tarval_not(get_mode_null(mode));
631 case irms_float_number:
632 return new_tarval_from_double(1.0, mode);
637 tarval *get_tarval_minus_one(ir_mode *mode) {
640 if (get_mode_n_vector_elems(mode) > 1) {
641 /* vector arithmetic not implemented yet */
645 switch(get_mode_sort(mode)) {
646 case irms_control_flow:
649 case irms_internal_boolean:
656 case irms_float_number:
657 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
659 case irms_int_number:
660 return new_tarval_from_long(-1l, mode);
665 tarval *get_tarval_nan(ir_mode *mode) {
668 if (get_mode_n_vector_elems(mode) > 1) {
669 /* vector arithmetic not implemented yet */
673 if (get_mode_sort(mode) == irms_float_number) {
674 switch(get_mode_size_bits(mode)) {
676 fc_get_qnan(8, 23, NULL);
679 fc_get_qnan(11, 52, NULL);
683 fc_get_qnan(15, 64, NULL);
686 panic("Unsupported mode in get_tarval_nan()");
688 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
690 assert(0 && "tarval is not floating point");
695 tarval *get_tarval_plus_inf(ir_mode *mode) {
698 if (get_mode_n_vector_elems(mode) > 1) {
699 /* vector arithmetic not implemented yet */
703 if (get_mode_sort(mode) == irms_float_number) {
704 switch(get_mode_size_bits(mode)) {
706 fc_get_plusinf(8, 23, NULL);
709 fc_get_plusinf(11, 52, NULL);
713 fc_get_plusinf(15, 64, NULL);
716 panic("Unsupported mode in get_tarval_plus_inf()");
718 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
720 assert(0 && "tarval is not floating point");
725 tarval *get_tarval_minus_inf(ir_mode *mode) {
728 if (get_mode_n_vector_elems(mode) > 1) {
729 /* vector arithmetic not implemented yet */
733 if (get_mode_sort(mode) == irms_float_number) {
734 switch(get_mode_size_bits(mode)) {
736 fc_get_minusinf(8, 23, NULL);
739 fc_get_minusinf(11, 52, NULL);
743 fc_get_minusinf(15, 64, NULL);
746 panic("Unsupported mode in get_tarval_minus_inf()");
748 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
750 assert(0 && "tarval is not floating point");
756 * Arithmethic operations on tarvals ========================================
760 * test if negative number, 1 means 'yes'
762 int tarval_is_negative(tarval *a) {
765 if (get_mode_n_vector_elems(a->mode) > 1) {
766 /* vector arithmetic not implemented yet */
767 assert(0 && "tarval_is_negative is not allowed for vector modes");
771 switch (get_mode_sort(a->mode)) {
772 case irms_int_number:
773 if (!mode_is_signed(a->mode)) return 0;
775 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
777 case irms_float_number:
778 return fc_is_negative(a->value);
781 assert(0 && "not implemented");
787 * test if null, 1 means 'yes'
789 int tarval_is_null(tarval *a) {
792 a == get_mode_null(get_tarval_mode(a));
796 * test if one, 1 means 'yes'
798 int tarval_is_one(tarval *a) {
801 a == get_mode_one(get_tarval_mode(a));
804 int tarval_is_all_one(tarval *tv) {
807 tv == get_mode_all_one(get_tarval_mode(tv));
811 * test if one, 1 means 'yes'
813 int tarval_is_minus_one(tarval *a) {
816 a == get_mode_minus_one(get_tarval_mode(a));
822 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
826 if (a == tarval_bad || b == tarval_bad) {
827 assert(0 && "Comparison with tarval_bad");
831 if (a == tarval_undefined || b == tarval_undefined)
834 if (a->mode != b->mode)
837 if (get_mode_n_vector_elems(a->mode) > 1) {
838 /* vector arithmetic not implemented yet */
839 assert(0 && "cmp not implemented for vector modes");
842 /* Here the two tarvals are unequal and of the same mode */
843 switch (get_mode_sort(a->mode)) {
844 case irms_control_flow:
851 case irms_float_number:
852 /* it should be safe to enable this even if other arithmetic is disabled */
854 return pn_Cmp_False;*/
856 * BEWARE: we cannot compare a == b here, because
857 * a NaN is always Unordered to any other value, even to itself!
859 switch (fc_comp(a->value, b->value)) {
860 case -1: return pn_Cmp_Lt;
861 case 0: return pn_Cmp_Eq;
862 case 1: return pn_Cmp_Gt;
863 case 2: return pn_Cmp_Uo;
864 default: return pn_Cmp_False;
867 case irms_int_number:
870 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
872 case irms_internal_boolean:
875 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
881 * convert to other mode
883 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
890 if (src->mode == dst_mode) return src;
892 if (get_mode_n_vector_elems(src->mode) > 1) {
893 /* vector arithmetic not implemented yet */
897 switch (get_mode_sort(src->mode)) {
898 case irms_control_flow:
903 /* cast float to something */
904 case irms_float_number:
905 switch (get_mode_sort(dst_mode)) {
906 case irms_float_number:
907 switch (get_mode_size_bits(dst_mode)) {
909 fc_cast(src->value, 8, 23, NULL);
912 fc_cast(src->value, 11, 52, NULL);
916 fc_cast(src->value, 15, 64, NULL);
919 panic("Unsupported mode in tarval_convert_to()");
921 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
923 case irms_int_number:
924 switch (GET_FLOAT_TO_INT_MODE()) {
926 res = fc_int(src->value, NULL);
929 res = fc_rnd(src->value, NULL);
932 panic("Unsupported float to int conversion mode in tarval_convert_to()");
935 buffer = alloca(sc_get_buffer_length());
936 if (! fc_flt2int(res, buffer, dst_mode))
938 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
941 /* the rest can't be converted */
946 /* cast int/characters to something */
947 case irms_int_number:
948 switch (get_mode_sort(dst_mode)) {
951 case irms_int_number:
952 buffer = alloca(sc_get_buffer_length());
953 memcpy(buffer, src->value, sc_get_buffer_length());
954 sign_extend(buffer, dst_mode);
955 return get_tarval_overflow(buffer, src->length, dst_mode);
957 case irms_internal_boolean:
958 /* XXX C semantics */
959 if (src == get_mode_null(src->mode)) return tarval_b_false;
960 else return tarval_b_true;
962 case irms_float_number:
963 /* XXX floating point unit does not understand internal integer
964 * representation, convert to string first, then create float from
966 buffer = alloca(100);
967 /* decimal string representation because hexadecimal output is
968 * interpreted unsigned by fc_val_from_str, so this is a HACK */
969 snprintf(buffer, 100, "%s",
970 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
971 buffer[100 - 1] = '\0';
972 switch (get_mode_size_bits(dst_mode)) {
974 fc_val_from_str(buffer, 0, 8, 23, NULL);
977 fc_val_from_str(buffer, 0, 11, 52, NULL);
981 fc_val_from_str(buffer, 0, 15, 64, NULL);
984 panic("Unsupported mode in tarval_convert_to()");
986 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
993 case irms_internal_boolean:
994 /* beware: this is C semantic for the INTERNAL boolean mode */
995 if (get_mode_sort(dst_mode) == irms_int_number)
996 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
1000 if (get_mode_sort(dst_mode) == irms_int_number) {
1001 buffer = alloca(sc_get_buffer_length());
1002 memcpy(buffer, src->value, sc_get_buffer_length());
1003 sign_extend(buffer, src->mode);
1004 return get_tarval_overflow(buffer, src->length, dst_mode);
1015 tarval *tarval_not(tarval *a) {
1020 /* works for vector mode without changes */
1022 switch (get_mode_sort(a->mode)) {
1023 case irms_reference:
1024 case irms_int_number:
1025 buffer = alloca(sc_get_buffer_length());
1026 sc_not(a->value, buffer);
1027 return get_tarval(buffer, a->length, a->mode);
1029 case irms_internal_boolean:
1030 if (a == tarval_b_true)
1031 return tarval_b_false;
1032 if (a == tarval_b_false)
1033 return tarval_b_true;
1037 assert(0 && "bitwise negation is only allowed for integer and boolean");
1043 * arithmetic negation
1045 tarval *tarval_neg(tarval *a) {
1049 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1051 /* note: negation is allowed even for unsigned modes. */
1053 if (get_mode_n_vector_elems(a->mode) > 1) {
1054 /* vector arithmetic not implemented yet */
1058 switch (get_mode_sort(a->mode)) {
1059 case irms_int_number:
1060 buffer = alloca(sc_get_buffer_length());
1061 sc_neg(a->value, buffer);
1062 return get_tarval_overflow(buffer, a->length, a->mode);
1064 case irms_float_number:
1065 /* it should be safe to enable this even if other arithmetic is disabled */
1067 return tarval_bad;*/
1069 fc_neg(a->value, NULL);
1070 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1080 tarval *tarval_add(tarval *a, tarval *b) {
1085 assert(a->mode == b->mode);
1087 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1088 /* vector arithmetic not implemented yet */
1092 switch (get_mode_sort(a->mode)) {
1093 case irms_int_number:
1094 /* modes of a,b are equal, so result has mode of a as this might be the character */
1095 buffer = alloca(sc_get_buffer_length());
1096 sc_add(a->value, b->value, buffer);
1097 return get_tarval_overflow(buffer, a->length, a->mode);
1099 case irms_float_number:
1103 fc_add(a->value, b->value, NULL);
1104 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1114 tarval *tarval_sub(tarval *a, tarval *b) {
1119 assert(a->mode == b->mode);
1121 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1122 /* vector arithmetic not implemented yet */
1125 switch (get_mode_sort(a->mode)) {
1126 case irms_int_number:
1127 /* modes of a,b are equal, so result has mode of a as this might be the character */
1128 buffer = alloca(sc_get_buffer_length());
1129 sc_sub(a->value, b->value, buffer);
1130 return get_tarval_overflow(buffer, a->length, a->mode);
1132 case irms_float_number:
1136 fc_sub(a->value, b->value, NULL);
1137 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1147 tarval *tarval_mul(tarval *a, tarval *b) {
1152 assert(a->mode == b->mode);
1154 if (get_mode_n_vector_elems(a->mode) > 1) {
1155 /* vector arithmetic not implemented yet */
1159 switch (get_mode_sort(a->mode)) {
1160 case irms_int_number:
1161 /* modes of a,b are equal */
1162 buffer = alloca(sc_get_buffer_length());
1163 sc_mul(a->value, b->value, buffer);
1164 return get_tarval_overflow(buffer, a->length, a->mode);
1166 case irms_float_number:
1170 fc_mul(a->value, b->value, NULL);
1171 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1179 * floating point division
1181 tarval *tarval_quo(tarval *a, tarval *b) {
1184 assert((a->mode == b->mode) && mode_is_float(a->mode));
1189 if (get_mode_n_vector_elems(a->mode) > 1) {
1190 /* vector arithmetic not implemented yet */
1194 fc_div(a->value, b->value, NULL);
1195 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1200 * overflow is impossible, but look out for division by zero
1202 tarval *tarval_div(tarval *a, tarval *b) {
1205 assert((a->mode == b->mode) && mode_is_int(a->mode));
1207 if (get_mode_n_vector_elems(a->mode) > 1) {
1208 /* vector arithmetic not implemented yet */
1213 if (b == get_mode_null(b->mode)) return tarval_bad;
1214 /* modes of a,b are equal */
1215 sc_div(a->value, b->value, NULL);
1216 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1221 * overflow is impossible, but look out for division by zero
1223 tarval *tarval_mod(tarval *a, tarval *b) {
1226 assert((a->mode == b->mode) && mode_is_int(a->mode));
1228 if (get_mode_n_vector_elems(a->mode) > 1) {
1229 /* vector arithmetic not implemented yet */
1234 if (b == get_mode_null(b->mode)) return tarval_bad;
1235 /* modes of a,b are equal */
1236 sc_mod(a->value, b->value, NULL);
1237 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1241 * integer division AND remainder
1242 * overflow is impossible, but look out for division by zero
1244 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1245 int len = sc_get_buffer_length();
1246 char *div_res = alloca(len);
1247 char *mod_res = alloca(len);
1251 assert((a->mode == b->mode) && mode_is_int(a->mode));
1253 if (get_mode_n_vector_elems(a->mode) > 1) {
1254 /* vector arithmetic not implemented yet */
1260 if (b == get_mode_null(b->mode)) return tarval_bad;
1261 /* modes of a,b are equal */
1262 sc_divmod(a->value, b->value, div_res, mod_res);
1263 *mod = get_tarval(mod_res, len, a->mode);
1264 return get_tarval(div_res, len, a->mode);
1270 tarval *tarval_abs(tarval *a) {
1274 assert(mode_is_num(a->mode));
1276 if (get_mode_n_vector_elems(a->mode) > 1) {
1277 /* vector arithmetic not implemented yet */
1281 switch (get_mode_sort(a->mode)) {
1282 case irms_int_number:
1283 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1284 buffer = alloca(sc_get_buffer_length());
1285 sc_neg(a->value, buffer);
1286 return get_tarval_overflow(buffer, a->length, a->mode);
1290 case irms_float_number:
1291 /* it should be safe to enable this even if other arithmetic is disabled */
1293 return tarval_bad;*/
1295 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1296 fc_neg(a->value, NULL);
1297 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1310 tarval *tarval_and(tarval *a, tarval *b) {
1313 assert(a->mode == b->mode);
1315 /* works even for vector modes */
1317 switch(get_mode_sort(a->mode)) {
1318 case irms_internal_boolean:
1319 return (a == tarval_b_false) ? a : b;
1321 case irms_int_number:
1322 sc_and(a->value, b->value, NULL);
1323 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1326 assert(0 && "operation not defined on mode");
1334 tarval *tarval_or (tarval *a, tarval *b) {
1337 assert(a->mode == b->mode);
1339 /* works even for vector modes */
1341 switch (get_mode_sort(a->mode)) {
1342 case irms_internal_boolean:
1343 return (a == tarval_b_true) ? a : b;
1345 case irms_int_number:
1346 sc_or(a->value, b->value, NULL);
1347 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1350 assert(0 && "operation not defined on mode");
1356 * bitwise exclusive or (xor)
1358 tarval *tarval_eor(tarval *a, tarval *b) {
1361 assert((a->mode == b->mode));
1363 /* works even for vector modes */
1365 switch (get_mode_sort(a->mode)) {
1366 case irms_internal_boolean:
1367 return (a == b)? tarval_b_false : tarval_b_true;
1369 case irms_int_number:
1370 sc_xor(a->value, b->value, NULL);
1371 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1374 assert(0 && "operation not defined on mode");
1380 * bitwise left shift
1382 tarval *tarval_shl(tarval *a, tarval *b) {
1383 char *temp_val = NULL;
1387 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1389 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1390 /* vector arithmetic not implemented yet */
1394 if (get_mode_modulo_shift(a->mode) != 0) {
1395 temp_val = alloca(sc_get_buffer_length());
1397 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1398 sc_mod(b->value, temp_val, temp_val);
1400 temp_val = (char*)b->value;
1402 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1403 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1407 * bitwise unsigned right shift
1409 tarval *tarval_shr(tarval *a, tarval *b) {
1410 char *temp_val = NULL;
1414 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1416 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1417 /* vector arithmetic not implemented yet */
1421 if (get_mode_modulo_shift(a->mode) != 0) {
1422 temp_val = alloca(sc_get_buffer_length());
1424 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1425 sc_mod(b->value, temp_val, temp_val);
1427 temp_val = (char*)b->value;
1429 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1430 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1434 * bitwise signed right shift
1436 tarval *tarval_shrs(tarval *a, tarval *b) {
1437 char *temp_val = NULL;
1441 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1443 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1444 /* vector arithmetic not implemented yet */
1448 if (get_mode_modulo_shift(a->mode) != 0) {
1449 temp_val = alloca(sc_get_buffer_length());
1451 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1452 sc_mod(b->value, temp_val, temp_val);
1454 temp_val = (char*)b->value;
1456 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1457 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1461 * bitwise rotation to left
1463 tarval *tarval_rotl(tarval *a, tarval *b) {
1464 char *temp_val = NULL;
1468 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 = 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_rotl(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 * carry flag of the last operation
1490 int tarval_carry(void) {
1491 panic("tarval_carry() requetsed: not implemented on all operations");
1492 return sc_had_carry();
1498 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1499 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1503 const tarval_mode_info *mode_info;
1504 const char *prefix, *suffix;
1506 mode_info = tv->mode->tv_priv;
1508 mode_info = &default_info;
1509 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1510 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1512 switch (get_mode_sort(tv->mode)) {
1513 case irms_reference:
1514 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1516 case irms_int_number:
1517 switch (mode_info->mode_output) {
1520 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1524 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1530 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1533 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1535 case irms_float_number:
1536 switch (mode_info->mode_output) {
1538 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1541 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1546 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1550 case irms_internal_boolean:
1551 switch (mode_info->mode_output) {
1557 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1561 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1564 case irms_control_flow:
1566 case irms_auxiliary:
1567 if (tv == tarval_bad)
1568 return snprintf(buf, len, "<TV_BAD>");
1569 if (tv == tarval_undefined)
1570 return snprintf(buf, len, "<TV_UNDEF>");
1571 return snprintf(buf, len, "<TV_??""?>");
1578 * Output of tarvals to stdio.
1580 int tarval_printf(tarval *tv) {
1584 res = tarval_snprintf(buf, sizeof(buf), tv);
1585 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1590 char *get_tarval_bitpattern(tarval *tv) {
1592 int n = get_mode_size_bits(tv->mode);
1593 int bytes = (n + 7) / 8;
1594 char *res = xmalloc((n + 1) * sizeof(char));
1597 for(i = 0; i < bytes; i++) {
1598 byte = get_tarval_sub_bits(tv, i);
1599 for(j = 1; j < 256; j <<= 1)
1601 res[pos++] = j & byte ? '1' : '0';
1610 * access to the bitpattern
1612 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1613 switch (get_mode_arithmetic(tv->mode)) {
1614 case irma_twos_complement:
1615 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1617 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1619 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1624 * Specify the output options of one mode.
1626 * This functions stores the modinfo, so DO NOT DESTROY it.
1628 * Returns zero on success.
1630 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1633 mode->tv_priv = modeinfo;
1638 * Returns the output options of one mode.
1640 * This functions returns the mode info of a given mode.
1642 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1645 return mode->tv_priv;
1649 * Returns non-zero if a given (integer) tarval has only one single bit
1652 int tarval_is_single_bit(tarval *tv) {
1656 if (!tv || tv == tarval_bad) return 0;
1657 if (! mode_is_int(tv->mode)) return 0;
1659 l = get_mode_size_bytes(tv->mode);
1660 for (bits = 0, i = l - 1; i >= 0; --i) {
1661 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1663 /* check for more than one bit in these */
1675 * Returns non-zero if the mantissa of a floating point IEEE-754
1676 * tarval is zero (i.e. 1.0Exxx)
1678 int tarval_ieee754_zero_mantissa(tarval *tv) {
1679 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1680 return fc_zero_mantissa(tv->value);
1683 /* Returns the exponent of a floating point IEEE-754 tarval. */
1684 int tarval_ieee754_get_exponent(tarval *tv) {
1685 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1686 return fc_get_exponent(tv->value);
1690 * Check if the tarval can be converted to the given mode without
1693 int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode) {
1694 char exp_size, mant_size;
1695 switch (get_mode_size_bits(mode)) {
1697 exp_size = 8; mant_size = 23;
1700 exp_size = 11; mant_size = 52;
1704 exp_size = 15; mant_size = 64;
1707 panic("Unsupported mode in tarval_ieee754_can_conv_lossless()");
1710 return fc_can_lossless_conv_to(tv->value, exp_size, mant_size);
1713 /* Set the immediate precision for IEEE-754 results. */
1714 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1715 return fc_set_immediate_precision(bits);
1718 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1719 unsigned tarval_ieee754_get_exact(void) {
1720 return fc_is_exact();
1723 /* check if its the a floating point NaN */
1724 int tarval_is_NaN(tarval *tv) {
1725 if (! mode_is_float(tv->mode))
1727 return fc_is_nan(tv->value);
1730 /* check if its the a floating point +inf */
1731 int tarval_is_plus_inf(tarval *tv) {
1732 if (! mode_is_float(tv->mode))
1734 return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1737 /* check if its the a floating point -inf */
1738 int tarval_is_minus_inf(tarval *tv) {
1739 if (! mode_is_float(tv->mode))
1741 return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1744 /* check if the tarval represents a finite value */
1745 int tarval_is_finite(tarval *tv) {
1746 if (mode_is_float(tv->mode))
1747 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1752 * Sets the overflow mode for integer operations.
1754 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1755 int_overflow_mode = ov_mode;
1758 /* Get the overflow mode for integer operations. */
1759 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1760 return int_overflow_mode;
1763 /* Enable/Disable floating point constant folding. */
1764 int tarval_enable_fp_ops(int enable) {
1765 int old = !no_float;
1772 * default mode_info for output as HEX
1774 static const tarval_mode_info hex_output = {
1781 * Initialization of the tarval module: called before init_mode()
1783 void init_tarval_1(long null_value) {
1784 _null_value = null_value;
1786 /* initialize the sets holding the tarvals with a comparison function and
1787 * an initial size, which is the expected number of constants */
1788 tarvals = new_set(cmp_tv, N_CONSTANTS);
1789 values = new_set(memcmp, N_CONSTANTS);
1790 /* init strcalc with precision of 68 to support floating point values with 64
1791 * bit mantissa (needs extra bits for rounding and overflow) */
1797 * Initialization of the tarval module: called after init_mode()
1799 void init_tarval_2(void) {
1800 tarval_bad->kind = k_tarval;
1801 tarval_bad->mode = mode_BAD;
1802 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1804 tarval_undefined->kind = k_tarval;
1805 tarval_undefined->mode = mode_ANY;
1806 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1808 tarval_b_true->kind = k_tarval;
1809 tarval_b_true->mode = mode_b;
1810 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1812 tarval_b_false->kind = k_tarval;
1813 tarval_b_false->mode = mode_b;
1814 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1817 * assign output modes that are compatible with the
1818 * old implementation: Hex output
1820 set_tarval_mode_output_option(mode_Bs, &hex_output);
1821 set_tarval_mode_output_option(mode_Bu, &hex_output);
1822 set_tarval_mode_output_option(mode_Hs, &hex_output);
1823 set_tarval_mode_output_option(mode_Hu, &hex_output);
1824 set_tarval_mode_output_option(mode_Is, &hex_output);
1825 set_tarval_mode_output_option(mode_Iu, &hex_output);
1826 set_tarval_mode_output_option(mode_Ls, &hex_output);
1827 set_tarval_mode_output_option(mode_Lu, &hex_output);
1828 set_tarval_mode_output_option(mode_P, &hex_output);
1831 /* free all memory occupied by tarval. */
1832 void finish_tarval(void) {
1835 del_set(tarvals); tarvals = NULL;
1836 del_set(values); values = NULL;
1839 int (is_tarval)(const void *thing) {
1840 return _is_tarval(thing);
1843 /****************************************************************************
1845 ****************************************************************************/