3 * File name: ir/tv/tv.c
4 * Purpose: Representation of and static computations on target machine
10 * Copyright: (c) 2003 Universität Karlsruhe
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * Values are stored in a format depending upon chosen arithmetic
16 * module. Default uses strcalc and fltcalc.
20 /* This implementation assumes:
21 * - target has IEEE-754 floating-point arithmetic. */
29 #include <assert.h> /* assertions */
30 #include <stdlib.h> /* atoi() */
32 # include <string.h> /* nice things for strings */
35 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
48 #include "set.h" /* to store tarvals in */
49 #include "entity_t.h" /* needed to store pointers to entities */
51 #include "irnode.h" /* defines boolean return values (pnc_number)*/
55 #include "firm_common.h"
57 /** Size of hash tables. Should correspond to average number of distinct constant
59 #define N_CONSTANTS 2048
61 /* get the integer overflow mode */
62 #define GET_OVERFLOW_MODE() int_overflow_mode
64 /* unused, float to int doesn't work yet */
65 enum float_to_int_mode {
70 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
72 #define SWITCH_NOINFINITY 0
73 #define SWITCH_NODENORMALS 0
75 /****************************************************************************
76 * local definitions and macros
77 ****************************************************************************/
79 # define TARVAL_VERIFY(a) tarval_verify((a))
81 # define TARVAL_VERIFY(a) ((void)0)
84 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
85 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
87 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
88 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
90 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
92 static long long count = 0;
93 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
95 # define ANNOUNCE() ((void)0)
97 /****************************************************************************
99 ****************************************************************************/
100 static struct set *tarvals; /* container for tarval structs */
101 static struct set *values; /* container for values */
102 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
104 /****************************************************************************
106 ****************************************************************************/
108 static int hash_val(const void *value, unsigned int length);
109 static int hash_tv(tarval *tv);
110 static void _fail_verify(tarval *tv, const char* file, int line)
112 /* print a memory image of the tarval and throw an assertion */
114 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
116 printf("%s:%d: Invalid tarval (null)", file, line);
120 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
123 INLINE static void tarval_verify(tarval *tv)
129 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
130 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
132 if (!FIND_TARVAL(tv)) fail_verify(tv);
133 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
139 static int hash_tv(tarval *tv)
141 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
144 static int hash_val(const void *value, unsigned int length)
147 unsigned int hash = 0;
149 /* scramble the byte - array */
150 for (i = 0; i < length; i++)
152 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
153 hash += (hash << 11) ^ (hash >> 17);
159 /** finds tarval with value/mode or creates new tarval */
160 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
168 /* if there already is such a value, it is returned, else value
169 * is copied into the set */
170 tv.value = INSERT_VALUE(value, length);
174 /* if there is such a tarval, it is returned, else tv is copied
176 return (tarval *)INSERT_TARVAL(&tv);
182 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
184 switch (get_mode_sort(mode))
186 case irms_int_number:
187 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
188 switch (GET_OVERFLOW_MODE()) {
189 case TV_OVERFLOW_SATURATE:
190 return get_mode_max(mode);
191 case TV_OVERFLOW_WRAP:
193 char *temp = alloca(sc_get_buffer_length());
194 sc_val_from_ulong(-1, temp);
195 sc_and(temp, value, temp);
196 return get_tarval(temp, length, mode);
198 case TV_OVERFLOW_BAD:
201 return get_tarval(value, length, mode);
204 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
205 switch (GET_OVERFLOW_MODE()) {
206 case TV_OVERFLOW_SATURATE:
207 return get_mode_min(mode);
208 case TV_OVERFLOW_WRAP:
210 char *temp = alloca(sc_get_buffer_length());
211 sc_val_from_ulong(-1, temp);
212 sc_and(temp, value, temp);
213 return get_tarval(temp, length, mode);
215 case TV_OVERFLOW_BAD:
218 return get_tarval(value, length, mode);
223 case irms_float_number:
224 if (SWITCH_NOINFINITY && fc_is_inf(value))
226 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
229 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
231 return get_mode_null(mode);
238 return get_tarval(value, length, mode);
243 * public variables declared in tv.h
245 static tarval reserved_tv[4];
247 tarval *tarval_bad = &reserved_tv[0];
248 tarval *tarval_undefined = &reserved_tv[1];
249 tarval *tarval_b_false = &reserved_tv[2];
250 tarval *tarval_b_true = &reserved_tv[3];
253 * public functions declared in tv.h
257 * Constructors =============================================================
259 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
266 switch (get_mode_sort(mode))
268 case irms_control_flow:
274 case irms_internal_boolean:
275 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
276 if (strcasecmp(str, "true")) return tarval_b_true;
277 else if (strcasecmp(str, "false")) return tarval_b_true;
279 /* XXX This is C semantics */
280 return atoi(str) ? tarval_b_true : tarval_b_false;
282 case irms_float_number:
283 switch(get_mode_size_bits(mode)) {
285 fc_val_from_str(str, len, 8, 23, NULL);
288 fc_val_from_str(str, len, 11, 52, NULL);
291 fc_val_from_str(str, len, 15, 64, NULL);
294 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
297 /* same as integer modes */
298 case irms_int_number:
300 sc_val_from_str(str, len, NULL, mode);
301 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
304 assert(0); /* can't be reached, can it? */
309 * helper function, create a tarval from long
311 tarval *new_tarval_from_long(long l, ir_mode *mode)
316 switch(get_mode_sort(mode))
318 case irms_internal_boolean:
319 /* XXX C semantics ! */
320 return l ? tarval_b_true : tarval_b_false ;
323 /* same as integer modes */
324 case irms_int_number:
326 sc_val_from_long(l, NULL);
327 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
329 case irms_float_number:
330 return new_tarval_from_double((long double)l, mode);
333 assert(0 && "unsupported mode sort");
338 /* returns non-zero if can be converted to long */
339 int tarval_is_long(tarval *tv)
341 mode_sort sort = get_mode_sort(tv->mode);
344 if (sort != irms_int_number && sort != irms_character) return 0;
346 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
348 /* the value might be too big to fit in a long */
349 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
350 if (sc_comp(sc_get_buffer(), tv->value) == -1)
352 /* really doesn't fit */
359 /* this might overflow the machine's long, so use only with small values */
360 long get_tarval_long(tarval* tv)
363 assert(tarval_is_long(tv) && "tarval too big to fit in long");
365 return sc_val_to_long(tv->value);
368 tarval *new_tarval_from_double(long double d, ir_mode *mode)
371 assert(mode && (get_mode_sort(mode) == irms_float_number));
373 switch (get_mode_size_bits(mode)) {
375 fc_val_from_float(d, 8, 23, NULL);
378 fc_val_from_float(d, 11, 52, NULL);
381 fc_val_from_float(d, 15, 64, NULL);
384 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
387 /* returns non-zero if can be converted to double */
388 int tarval_is_double(tarval *tv)
393 return (get_mode_sort(tv->mode) == irms_float_number);
396 long double get_tarval_double(tarval *tv)
399 assert(tarval_is_double(tv));
401 return fc_val_to_float(tv->value);
406 * Access routines for tarval fields ========================================
409 /* get the mode of the tarval */
410 ir_mode *(get_tarval_mode)(const tarval *tv) {
411 return _get_tarval_mode(tv);
415 * Special value query functions ============================================
417 * These functions calculate and return a tarval representing the requested
419 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
420 * functions, but these are stored on initialization of the irmode module and
421 * therefore the irmode functions should be prefered to the functions below.
424 tarval *(get_tarval_bad)(void) {
425 return _get_tarval_bad();
428 tarval *(get_tarval_undefined)(void) {
429 return _get_tarval_undefined();
432 tarval *(get_tarval_b_false)(void) {
433 return _get_tarval_b_false();
436 tarval *(get_tarval_b_true)(void) {
437 return _get_tarval_b_true();
440 tarval *get_tarval_max(ir_mode *mode)
445 if (get_mode_n_vector_elems(mode) > 1) {
446 /* vector arithmetic not implemented yet */
450 switch(get_mode_sort(mode))
453 case irms_control_flow:
459 case irms_internal_boolean:
460 return tarval_b_true;
462 case irms_float_number:
463 switch(get_mode_size_bits(mode))
466 fc_get_max(8, 23, NULL);
469 fc_get_max(11, 52, NULL);
472 fc_get_max(15, 64, NULL);
475 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
477 case irms_int_number:
479 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
480 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
485 tarval *get_tarval_min(ir_mode *mode)
490 if (get_mode_n_vector_elems(mode) > 1) {
491 /* vector arithmetic not implemented yet */
495 switch(get_mode_sort(mode))
498 case irms_control_flow:
504 case irms_internal_boolean:
505 return tarval_b_false;
507 case irms_float_number:
508 switch(get_mode_size_bits(mode))
511 fc_get_min(8, 23, NULL);
514 fc_get_min(11, 52, NULL);
517 fc_get_min(15, 64, NULL);
520 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
522 case irms_int_number:
524 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
525 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
530 static long _null_value;
532 tarval *get_tarval_null(ir_mode *mode)
537 if (get_mode_n_vector_elems(mode) > 1) {
538 /* vector arithmetic not implemented yet */
542 switch(get_mode_sort(mode))
544 case irms_control_flow:
547 case irms_internal_boolean:
551 case irms_float_number:
552 return new_tarval_from_double(0.0, mode);
554 case irms_int_number:
556 return new_tarval_from_long(0l, mode);
559 return new_tarval_from_long(_null_value, mode);
564 tarval *get_tarval_one(ir_mode *mode)
569 if (get_mode_n_vector_elems(mode) > 1) {
570 /* vector arithmetic not implemented yet */
574 switch(get_mode_sort(mode))
576 case irms_control_flow:
579 case irms_internal_boolean:
584 case irms_float_number:
585 return new_tarval_from_double(1.0, mode);
587 case irms_int_number:
589 return new_tarval_from_long(1l, mode);
595 tarval *get_tarval_minus_one(ir_mode *mode)
600 if (get_mode_n_vector_elems(mode) > 1) {
601 /* vector arithmetic not implemented yet */
605 switch(get_mode_sort(mode))
607 case irms_control_flow:
610 case irms_internal_boolean:
615 case irms_float_number:
616 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
618 case irms_int_number:
620 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
625 tarval *get_tarval_nan(ir_mode *mode)
630 if (get_mode_n_vector_elems(mode) > 1) {
631 /* vector arithmetic not implemented yet */
635 if (get_mode_sort(mode) == irms_float_number) {
636 switch(get_mode_size_bits(mode))
639 fc_get_qnan(8, 23, NULL);
642 fc_get_qnan(11, 52, NULL);
645 fc_get_qnan(15, 64, NULL);
648 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
651 assert(0 && "tarval is not floating point");
656 tarval *get_tarval_plus_inf(ir_mode *mode)
661 if (get_mode_n_vector_elems(mode) > 1) {
662 /* vector arithmetic not implemented yet */
666 if (get_mode_sort(mode) == irms_float_number) {
667 switch(get_mode_size_bits(mode))
670 fc_get_plusinf(8, 23, NULL);
673 fc_get_plusinf(11, 52, NULL);
676 fc_get_plusinf(15, 64, NULL);
679 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
682 assert(0 && "tarval is not floating point");
687 tarval *get_tarval_minus_inf(ir_mode *mode)
692 if (get_mode_n_vector_elems(mode) > 1) {
693 /* vector arithmetic not implemented yet */
697 if (get_mode_sort(mode) == irms_float_number) {
698 switch(get_mode_size_bits(mode))
701 fc_get_minusinf(8, 23, NULL);
704 fc_get_minusinf(11, 52, NULL);
707 fc_get_minusinf(15, 64, NULL);
710 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
713 assert(0 && "tarval is not floating point");
719 * Arithmethic operations on tarvals ========================================
723 * test if negative number, 1 means 'yes'
725 int tarval_is_negative(tarval *a)
730 if (get_mode_n_vector_elems(a->mode) > 1) {
731 /* vector arithmetic not implemented yet */
732 assert(0 && "tarval_is_negative is not allowed for vector modes");
736 switch (get_mode_sort(a->mode))
738 case irms_int_number:
739 if (!mode_is_signed(a->mode)) return 0;
741 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
743 case irms_float_number:
744 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
747 assert(0 && "not implemented");
753 * test if null, 1 means 'yes'
755 int tarval_is_null(tarval *a)
757 ir_mode *m = get_tarval_mode(a);
759 return a == get_tarval_null(m);
763 * test if one, 1 means 'yes'
765 int tarval_is_one(tarval *a)
767 ir_mode *m = get_tarval_mode(a);
769 return a == get_tarval_one(m);
775 pn_Cmp tarval_cmp(tarval *a, tarval *b)
781 if (a == tarval_bad || b == tarval_bad) {
782 assert(0 && "Comparison with tarval_bad");
786 if (a == tarval_undefined || b == tarval_undefined)
789 if (a->mode != b->mode)
792 if (get_mode_n_vector_elems(a->mode) > 1) {
793 /* vector arithmetic not implemented yet */
794 assert(0 && "cmp not implemented for vector modes");
797 /* Here the two tarvals are unequal and of the same mode */
798 switch (get_mode_sort(a->mode))
800 case irms_control_flow:
808 case irms_float_number:
810 * BEWARE: we cannot compare a == b here, because
811 * a NaN is always Unordered to any other value, even to itself!
813 switch (fc_comp(a->value, b->value)) {
814 case -1: return pn_Cmp_Lt;
815 case 0: return pn_Cmp_Eq;
816 case 1: return pn_Cmp_Gt;
817 case 2: return pn_Cmp_Uo;
818 default: return pn_Cmp_False;
820 case irms_int_number:
824 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
826 case irms_internal_boolean:
829 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
835 * convert to other mode
837 tarval *tarval_convert_to(tarval *src, ir_mode *m)
845 if (src->mode == m) return src;
847 if (get_mode_n_vector_elems(src->mode) > 1) {
848 /* vector arithmetic not implemented yet */
852 switch (get_mode_sort(src->mode))
854 case irms_control_flow:
859 /* cast float to something */
860 case irms_float_number:
861 switch (get_mode_sort(m)) {
862 case irms_float_number:
863 switch (get_mode_size_bits(m))
866 fc_cast(src->value, 8, 23, NULL);
869 fc_cast(src->value, 11, 52, NULL);
872 fc_cast(src->value, 15, 64, NULL);
877 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
879 case irms_int_number:
880 switch (GET_FLOAT_TO_INT_MODE())
883 fc_int(src->value, NULL);
886 fc_rnd(src->value, NULL);
892 /* FIXME: floating point unit can't produce a value in integer
894 * an intermediate representation is needed here first. */
895 /* return get_tarval(); */
899 /* the rest can't be converted */
904 /* cast int to something */
905 case irms_int_number:
906 switch (get_mode_sort(m)) {
907 case irms_int_number:
909 return get_tarval_overflow(src->value, src->length, m);
911 case irms_internal_boolean:
912 /* XXX C semantics */
913 if (src == get_mode_null(src->mode)) return tarval_b_false;
914 else return tarval_b_true;
916 case irms_float_number:
917 /* XXX floating point unit does not understand internal integer
918 * representation, convert to string first, then create float from
920 buffer = alloca(100);
921 /* decimal string representation because hexadecimal output is
922 * interpreted unsigned by fc_val_from_str, so this is a HACK */
923 snprintf(buffer, 100, "%s",
924 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
925 switch (get_mode_size_bits(m))
928 fc_val_from_str(buffer, 0, 8, 23, NULL);
931 fc_val_from_str(buffer, 0, 11, 52, NULL);
934 fc_val_from_str(buffer, 0, 15, 64, NULL);
937 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
940 /* allow 0 to be casted */
941 if (src == get_mode_null(src->mode))
942 return get_mode_null(m);
950 case irms_internal_boolean:
951 switch (get_mode_sort(m))
953 case irms_int_number:
954 if (src == tarval_b_true) return get_mode_one(m);
955 else return get_mode_null(m);
974 tarval *tarval_not(tarval *a)
981 /* works for vector mode without changes */
983 switch (get_mode_sort(a->mode))
985 case irms_int_number:
986 buffer = alloca(sc_get_buffer_length());
987 sc_not(a->value, buffer);
988 return get_tarval(buffer, a->length, a->mode);
990 case irms_internal_boolean:
991 if (a == tarval_b_true)
992 return tarval_b_false;
993 if (a == tarval_b_false)
994 return tarval_b_true;
998 assert(0 && "bitwise negation is only allowed for integer and boolean");
1004 * arithmetic negation
1006 tarval *tarval_neg(tarval *a)
1012 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1014 /* note: negation is allowed even for unsigned modes. */
1016 if (get_mode_n_vector_elems(a->mode) > 1) {
1017 /* vector arithmetic not implemented yet */
1021 switch (get_mode_sort(a->mode))
1023 case irms_int_number:
1024 buffer = alloca(sc_get_buffer_length());
1025 sc_neg(a->value, buffer);
1026 return get_tarval_overflow(buffer, a->length, a->mode);
1028 case irms_float_number:
1029 fc_neg(a->value, NULL);
1030 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1040 tarval *tarval_add(tarval *a, tarval *b)
1047 assert(a->mode == b->mode);
1049 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1050 /* vector arithmetic not implemented yet */
1054 switch (get_mode_sort(a->mode))
1056 case irms_character:
1057 case irms_int_number:
1058 /* modes of a,b are equal, so result has mode of a as this might be the character */
1059 buffer = alloca(sc_get_buffer_length());
1060 sc_add(a->value, b->value, buffer);
1061 return get_tarval_overflow(buffer, a->length, a->mode);
1063 case irms_float_number:
1064 fc_add(a->value, b->value, NULL);
1065 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1075 tarval *tarval_sub(tarval *a, tarval *b)
1082 assert(a->mode == b->mode);
1084 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1085 /* vector arithmetic not implemented yet */
1088 switch (get_mode_sort(a->mode))
1090 case irms_character:
1091 case irms_int_number:
1092 /* modes of a,b are equal, so result has mode of a as this might be the character */
1093 buffer = alloca(sc_get_buffer_length());
1094 sc_sub(a->value, b->value, buffer);
1095 return get_tarval_overflow(buffer, a->length, a->mode);
1097 case irms_float_number:
1098 fc_sub(a->value, b->value, NULL);
1099 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1109 tarval *tarval_mul(tarval *a, tarval *b)
1116 assert(a->mode == b->mode);
1118 if (get_mode_n_vector_elems(a->mode) > 1) {
1119 /* vector arithmetic not implemented yet */
1123 switch (get_mode_sort(a->mode))
1125 case irms_int_number:
1126 /* modes of a,b are equal */
1127 buffer = alloca(sc_get_buffer_length());
1128 sc_mul(a->value, b->value, buffer);
1129 return get_tarval_overflow(buffer, a->length, a->mode);
1131 case irms_float_number:
1132 fc_mul(a->value, b->value, NULL);
1133 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1141 * floating point division
1143 tarval *tarval_quo(tarval *a, tarval *b)
1148 assert((a->mode == b->mode) && mode_is_float(a->mode));
1150 if (get_mode_n_vector_elems(a->mode) > 1) {
1151 /* vector arithmetic not implemented yet */
1155 fc_div(a->value, b->value, NULL);
1156 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1161 * overflow is impossible, but look out for division by zero
1163 tarval *tarval_div(tarval *a, tarval *b)
1168 assert((a->mode == b->mode) && mode_is_int(a->mode));
1170 if (get_mode_n_vector_elems(a->mode) > 1) {
1171 /* vector arithmetic not implemented yet */
1176 if (b == get_mode_null(b->mode)) return tarval_bad;
1177 /* modes of a,b are equal */
1178 sc_div(a->value, b->value, NULL);
1179 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1184 * overflow is impossible, but look out for division by zero
1186 tarval *tarval_mod(tarval *a, tarval *b)
1191 assert((a->mode == b->mode) && mode_is_int(a->mode));
1193 if (get_mode_n_vector_elems(a->mode) > 1) {
1194 /* vector arithmetic not implemented yet */
1199 if (b == get_mode_null(b->mode)) return tarval_bad;
1200 /* modes of a,b are equal */
1201 sc_mod(a->value, b->value, NULL);
1202 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1208 tarval *tarval_abs(tarval *a)
1214 assert(mode_is_num(a->mode));
1216 if (get_mode_n_vector_elems(a->mode) > 1) {
1217 /* vector arithmetic not implemented yet */
1221 switch (get_mode_sort(a->mode))
1223 case irms_int_number:
1224 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1226 buffer = alloca(sc_get_buffer_length());
1227 sc_neg(a->value, buffer);
1228 return get_tarval_overflow(buffer, a->length, a->mode);
1232 case irms_float_number:
1233 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1235 fc_neg(a->value, NULL);
1236 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1249 tarval *tarval_and(tarval *a, tarval *b)
1254 assert(a->mode == b->mode);
1256 /* works even for vector modes */
1258 switch(get_mode_sort(a->mode))
1260 case irms_internal_boolean:
1261 return (a == tarval_b_false) ? a : b;
1263 case irms_int_number:
1264 sc_and(a->value, b->value, NULL);
1265 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1268 assert(0 && "operation not defined on mode");
1276 tarval *tarval_or (tarval *a, tarval *b)
1281 assert(a->mode == b->mode);
1283 /* works even for vector modes */
1285 switch (get_mode_sort(a->mode))
1287 case irms_internal_boolean:
1288 return (a == tarval_b_true) ? a : b;
1290 case irms_int_number:
1291 sc_or(a->value, b->value, NULL);
1292 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1295 assert(0 && "operation not defined on mode");
1301 * bitwise exclusive or (xor)
1303 tarval *tarval_eor(tarval *a, tarval *b)
1308 assert((a->mode == b->mode));
1310 /* works even for vector modes */
1312 switch (get_mode_sort(a->mode))
1314 case irms_internal_boolean:
1315 return (a == b)? tarval_b_false : tarval_b_true;
1317 case irms_int_number:
1318 sc_xor(a->value, b->value, NULL);
1319 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1322 assert(0 && "operation not defined on mode");
1328 * bitwise left shift
1330 tarval *tarval_shl(tarval *a, tarval *b)
1332 char *temp_val = NULL;
1336 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1338 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1339 /* vector arithmetic not implemented yet */
1343 if (get_mode_modulo_shift(a->mode) != 0)
1345 temp_val = alloca(sc_get_buffer_length());
1347 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1348 sc_mod(b->value, temp_val, temp_val);
1351 temp_val = (char*)b->value;
1353 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1354 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1358 * bitwise unsigned right shift
1360 tarval *tarval_shr(tarval *a, tarval *b)
1362 char *temp_val = NULL;
1366 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1368 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1369 /* vector arithmetic not implemented yet */
1373 if (get_mode_modulo_shift(a->mode) != 0)
1375 temp_val = alloca(sc_get_buffer_length());
1377 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1378 sc_mod(b->value, temp_val, temp_val);
1381 temp_val = (char*)b->value;
1383 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1384 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1388 * bitwise signed right shift
1390 tarval *tarval_shrs(tarval *a, tarval *b)
1392 char *temp_val = NULL;
1396 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1398 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1399 /* vector arithmetic not implemented yet */
1403 if (get_mode_modulo_shift(a->mode) != 0)
1405 temp_val = alloca(sc_get_buffer_length());
1407 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1408 sc_mod(b->value, temp_val, temp_val);
1411 temp_val = (char*)b->value;
1413 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1414 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1420 tarval *tarval_rot(tarval *a, tarval *b)
1422 char *temp_val = NULL;
1426 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1428 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1429 /* vector arithmetic not implemented yet */
1433 if (get_mode_modulo_shift(a->mode) != 0)
1435 temp_val = alloca(sc_get_buffer_length());
1437 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1438 sc_mod(b->value, temp_val, temp_val);
1441 temp_val = (char*)b->value;
1443 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1444 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1448 * carry flag of the last operation
1450 int tarval_carry(void)
1452 return sc_had_carry();
1458 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1460 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1464 const tarval_mode_info *mode_info;
1465 const char *prefix, *suffix;
1469 mode_info = tv->mode->tv_priv;
1471 mode_info = &default_info;
1472 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1473 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1475 switch (get_mode_sort(tv->mode))
1477 case irms_int_number:
1478 case irms_character:
1479 switch (mode_info->mode_output) {
1482 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1486 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1492 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1495 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1497 case irms_float_number:
1498 switch (mode_info->mode_output) {
1500 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1503 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1508 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1512 case irms_reference:
1513 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1514 if (tv->value != NULL) {
1515 if (len > tv->length) {
1516 memcpy(buf, tv->value, tv->length);
1517 buf[tv->length] = '\0';
1521 memcpy(buf, tv->value, len-1);
1527 return snprintf(buf, len, "void");
1529 case irms_internal_boolean:
1530 switch (mode_info->mode_output) {
1536 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1540 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1543 case irms_control_flow:
1545 case irms_auxiliary:
1546 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1554 * Output of tarvals to stdio.
1556 int tarval_printf(tarval *tv) {
1560 res = tarval_snprintf(buf, sizeof(buf), tv);
1561 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1567 char *get_tarval_bitpattern(tarval *tv)
1570 int n = get_mode_size_bits(tv->mode);
1571 int bytes = (n + 7) / 8;
1572 char *res = malloc((n + 1) * sizeof(char));
1575 for(i = 0; i < bytes; i++) {
1576 byte = get_tarval_sub_bits(tv, i);
1577 for(j = 1; j < 256; j <<= 1)
1579 res[pos++] = j & byte ? '1' : '0';
1588 * access to the bitpattern
1590 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1592 switch (get_mode_sort(tv->mode)) {
1593 case irms_int_number:
1594 case irms_character:
1595 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1597 case irms_float_number:
1598 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1606 * Specify the output options of one mode.
1608 * This functions stores the modinfo, so DO NOT DESTROY it.
1610 * Returns zero on success.
1612 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1616 mode->tv_priv = modeinfo;
1621 * Returns the output options of one mode.
1623 * This functions returns the mode info of a given mode.
1625 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1629 return mode->tv_priv;
1633 * Identifying tarvals values for algebraic simplifications.
1636 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1637 * - TV_CLASSIFY_ONE for multiplicative neutral,
1638 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1639 * - TV_CLASSIFY_OTHER else
1641 tarval_classification_t classify_tarval(tarval *tv)
1644 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1646 if (tv == get_mode_null(tv->mode))
1647 return TV_CLASSIFY_NULL;
1648 else if (tv == get_mode_one(tv->mode))
1649 return TV_CLASSIFY_ONE;
1650 else if ((get_mode_sort(tv->mode) == irms_int_number)
1651 && (tv == new_tarval_from_long(-1, tv->mode)))
1652 return TV_CLASSIFY_ALL_ONE;
1654 return TV_CLASSIFY_OTHER;
1658 * Returns non-zero if a given (integer) tarval has only one single bit
1661 int is_single_bit_tarval(tarval *tv) {
1665 if (!tv || tv == tarval_bad) return 0;
1666 if (! mode_is_int(tv->mode)) return 0;
1668 l = get_mode_size_bytes(tv->mode);
1669 for (bits = 0, i = l - 1; i >= 0; --i) {
1670 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1672 /* check for more than one bit in these */
1684 * Sets the overflow mode for integer operations.
1686 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1687 int_overflow_mode = ov_mode;
1691 * Get the overflow mode for integer operations.
1693 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1694 return int_overflow_mode;
1698 * default mode_info for output as HEX
1700 static const tarval_mode_info hex_output = {
1707 * default mode_info for output as reference
1709 static const tarval_mode_info reference_output = {
1716 * Initialization of the tarval module: called before init_mode()
1718 void init_tarval_1(long null_value)
1721 _null_value = null_value;
1723 /* initialize the sets holding the tarvals with a comparison function and
1724 * an initial size, which is the expected number of constants */
1725 tarvals = new_set(memcmp, N_CONSTANTS);
1726 values = new_set(memcmp, N_CONSTANTS);
1727 /* init strcalc with precision of 68 to support floating point values with 64
1728 * bit mantissa (needs extra bits for rounding and overflow) */
1734 * Initialization of the tarval module: called after init_mode()
1736 void init_tarval_2(void)
1740 tarval_bad->kind = k_tarval;
1741 tarval_bad->mode = mode_BAD;
1743 tarval_undefined->kind = k_tarval;
1744 tarval_undefined->mode = mode_ANY;
1746 tarval_b_true->kind = k_tarval;
1747 tarval_b_true->mode = mode_b;
1749 tarval_b_false->kind = k_tarval;
1750 tarval_b_false->mode = mode_b;
1753 * assign output modes that are compatible with the
1754 * old implementation: Hex output
1756 set_tarval_mode_output_option(mode_U, &hex_output);
1757 set_tarval_mode_output_option(mode_C, &hex_output);
1758 set_tarval_mode_output_option(mode_Bs, &hex_output);
1759 set_tarval_mode_output_option(mode_Bu, &hex_output);
1760 set_tarval_mode_output_option(mode_Hs, &hex_output);
1761 set_tarval_mode_output_option(mode_Hu, &hex_output);
1762 set_tarval_mode_output_option(mode_Is, &hex_output);
1763 set_tarval_mode_output_option(mode_Iu, &hex_output);
1764 set_tarval_mode_output_option(mode_Ls, &hex_output);
1765 set_tarval_mode_output_option(mode_Lu, &hex_output);
1766 set_tarval_mode_output_option(mode_P, &reference_output);
1769 /* free all memory occupied by tarval. */
1770 void finish_tarval(void) {
1773 del_set(tarvals); tarvals = NULL;
1774 del_set(values); values = NULL;
1777 /****************************************************************************
1779 ****************************************************************************/