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)*/
56 #include "firm_common.h"
58 /** Size of hash tables. Should correspond to average number of distinct constant
60 #define N_CONSTANTS 2048
62 /* get the integer overflow mode */
63 #define GET_OVERFLOW_MODE() int_overflow_mode
65 /* unused, float to int doesn't work yet */
66 enum float_to_int_mode {
71 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
73 #define SWITCH_NOINFINITY 0
74 #define SWITCH_NODENORMALS 0
76 /****************************************************************************
77 * local definitions and macros
78 ****************************************************************************/
80 # define TARVAL_VERIFY(a) tarval_verify((a))
82 # define TARVAL_VERIFY(a) ((void)0)
85 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
86 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
88 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
89 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
91 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
93 static long long count = 0;
94 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
96 # define ANNOUNCE() ((void)0)
98 /****************************************************************************
100 ****************************************************************************/
101 static struct set *tarvals; /* container for tarval structs */
102 static struct set *values; /* container for values */
103 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
105 /****************************************************************************
107 ****************************************************************************/
109 static int hash_val(const void *value, unsigned int length);
110 static int hash_tv(tarval *tv);
111 static void _fail_verify(tarval *tv, const char* file, int line)
113 /* print a memory image of the tarval and throw an assertion */
115 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
117 printf("%s:%d: Invalid tarval (null)", file, line);
121 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
124 INLINE static void tarval_verify(tarval *tv)
130 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
131 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
133 if (!FIND_TARVAL(tv)) fail_verify(tv);
134 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
140 /** Hash a tarval. */
141 static int hash_tv(tarval *tv) {
142 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
145 /** Hash a value. Treat it as a byte array. */
146 static int hash_val(const void *value, unsigned int length) {
148 unsigned int hash = 0;
150 /* scramble the byte - array */
151 for (i = 0; i < length; i++)
153 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
154 hash += (hash << 11) ^ (hash >> 17);
160 /** finds tarval with value/mode or creates new tarval */
161 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
169 /* if there already is such a value, it is returned, else value
170 * is copied into the set */
171 tv.value = INSERT_VALUE(value, length);
175 /* if there is such a tarval, it is returned, else tv is copied
177 return (tarval *)INSERT_TARVAL(&tv);
183 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
185 switch (get_mode_sort(mode))
187 case irms_int_number:
188 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
189 switch (GET_OVERFLOW_MODE()) {
190 case TV_OVERFLOW_SATURATE:
191 return get_mode_max(mode);
192 case TV_OVERFLOW_WRAP:
194 char *temp = alloca(sc_get_buffer_length());
195 sc_val_from_ulong(-1, temp);
196 sc_and(temp, value, temp);
197 return get_tarval(temp, length, mode);
199 case TV_OVERFLOW_BAD:
202 return get_tarval(value, length, mode);
205 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
206 switch (GET_OVERFLOW_MODE()) {
207 case TV_OVERFLOW_SATURATE:
208 return get_mode_min(mode);
209 case TV_OVERFLOW_WRAP:
211 char *temp = alloca(sc_get_buffer_length());
212 sc_val_from_ulong(-1, temp);
213 sc_and(temp, value, temp);
214 return get_tarval(temp, length, mode);
216 case TV_OVERFLOW_BAD:
219 return get_tarval(value, length, mode);
224 case irms_float_number:
225 if (SWITCH_NOINFINITY && fc_is_inf(value))
227 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
230 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
232 return get_mode_null(mode);
239 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 buffer[100 - 1] = '\0';
926 switch (get_mode_size_bits(m))
929 fc_val_from_str(buffer, 0, 8, 23, NULL);
932 fc_val_from_str(buffer, 0, 11, 52, NULL);
935 fc_val_from_str(buffer, 0, 15, 64, NULL);
938 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
941 /* allow 0 to be casted */
942 if (src == get_mode_null(src->mode))
943 return get_mode_null(m);
951 case irms_internal_boolean:
952 switch (get_mode_sort(m))
954 case irms_int_number:
955 if (src == tarval_b_true) return get_mode_one(m);
956 else return get_mode_null(m);
975 tarval *tarval_not(tarval *a)
982 /* works for vector mode without changes */
984 switch (get_mode_sort(a->mode))
986 case irms_int_number:
987 buffer = alloca(sc_get_buffer_length());
988 sc_not(a->value, buffer);
989 return get_tarval(buffer, a->length, a->mode);
991 case irms_internal_boolean:
992 if (a == tarval_b_true)
993 return tarval_b_false;
994 if (a == tarval_b_false)
995 return tarval_b_true;
999 assert(0 && "bitwise negation is only allowed for integer and boolean");
1005 * arithmetic negation
1007 tarval *tarval_neg(tarval *a)
1013 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1015 /* note: negation is allowed even for unsigned modes. */
1017 if (get_mode_n_vector_elems(a->mode) > 1) {
1018 /* vector arithmetic not implemented yet */
1022 switch (get_mode_sort(a->mode))
1024 case irms_int_number:
1025 buffer = alloca(sc_get_buffer_length());
1026 sc_neg(a->value, buffer);
1027 return get_tarval_overflow(buffer, a->length, a->mode);
1029 case irms_float_number:
1030 fc_neg(a->value, NULL);
1031 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1041 tarval *tarval_add(tarval *a, tarval *b)
1048 assert(a->mode == b->mode);
1050 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1051 /* vector arithmetic not implemented yet */
1055 switch (get_mode_sort(a->mode))
1057 case irms_character:
1058 case irms_int_number:
1059 /* modes of a,b are equal, so result has mode of a as this might be the character */
1060 buffer = alloca(sc_get_buffer_length());
1061 sc_add(a->value, b->value, buffer);
1062 return get_tarval_overflow(buffer, a->length, a->mode);
1064 case irms_float_number:
1065 fc_add(a->value, b->value, NULL);
1066 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1076 tarval *tarval_sub(tarval *a, tarval *b)
1083 assert(a->mode == b->mode);
1085 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1086 /* vector arithmetic not implemented yet */
1089 switch (get_mode_sort(a->mode))
1091 case irms_character:
1092 case irms_int_number:
1093 /* modes of a,b are equal, so result has mode of a as this might be the character */
1094 buffer = alloca(sc_get_buffer_length());
1095 sc_sub(a->value, b->value, buffer);
1096 return get_tarval_overflow(buffer, a->length, a->mode);
1098 case irms_float_number:
1099 fc_sub(a->value, b->value, NULL);
1100 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1110 tarval *tarval_mul(tarval *a, tarval *b)
1117 assert(a->mode == b->mode);
1119 if (get_mode_n_vector_elems(a->mode) > 1) {
1120 /* vector arithmetic not implemented yet */
1124 switch (get_mode_sort(a->mode))
1126 case irms_int_number:
1127 /* modes of a,b are equal */
1128 buffer = alloca(sc_get_buffer_length());
1129 sc_mul(a->value, b->value, buffer);
1130 return get_tarval_overflow(buffer, a->length, a->mode);
1132 case irms_float_number:
1133 fc_mul(a->value, b->value, NULL);
1134 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1142 * floating point division
1144 tarval *tarval_quo(tarval *a, tarval *b)
1149 assert((a->mode == b->mode) && mode_is_float(a->mode));
1151 if (get_mode_n_vector_elems(a->mode) > 1) {
1152 /* vector arithmetic not implemented yet */
1156 fc_div(a->value, b->value, NULL);
1157 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1162 * overflow is impossible, but look out for division by zero
1164 tarval *tarval_div(tarval *a, tarval *b)
1169 assert((a->mode == b->mode) && mode_is_int(a->mode));
1171 if (get_mode_n_vector_elems(a->mode) > 1) {
1172 /* vector arithmetic not implemented yet */
1177 if (b == get_mode_null(b->mode)) return tarval_bad;
1178 /* modes of a,b are equal */
1179 sc_div(a->value, b->value, NULL);
1180 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1185 * overflow is impossible, but look out for division by zero
1187 tarval *tarval_mod(tarval *a, tarval *b)
1192 assert((a->mode == b->mode) && mode_is_int(a->mode));
1194 if (get_mode_n_vector_elems(a->mode) > 1) {
1195 /* vector arithmetic not implemented yet */
1200 if (b == get_mode_null(b->mode)) return tarval_bad;
1201 /* modes of a,b are equal */
1202 sc_mod(a->value, b->value, NULL);
1203 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1209 tarval *tarval_abs(tarval *a)
1215 assert(mode_is_num(a->mode));
1217 if (get_mode_n_vector_elems(a->mode) > 1) {
1218 /* vector arithmetic not implemented yet */
1222 switch (get_mode_sort(a->mode))
1224 case irms_int_number:
1225 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1227 buffer = alloca(sc_get_buffer_length());
1228 sc_neg(a->value, buffer);
1229 return get_tarval_overflow(buffer, a->length, a->mode);
1233 case irms_float_number:
1234 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1236 fc_neg(a->value, NULL);
1237 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1250 tarval *tarval_and(tarval *a, tarval *b)
1255 assert(a->mode == b->mode);
1257 /* works even for vector modes */
1259 switch(get_mode_sort(a->mode))
1261 case irms_internal_boolean:
1262 return (a == tarval_b_false) ? a : b;
1264 case irms_int_number:
1265 sc_and(a->value, b->value, NULL);
1266 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1269 assert(0 && "operation not defined on mode");
1277 tarval *tarval_or (tarval *a, tarval *b)
1282 assert(a->mode == b->mode);
1284 /* works even for vector modes */
1286 switch (get_mode_sort(a->mode))
1288 case irms_internal_boolean:
1289 return (a == tarval_b_true) ? a : b;
1291 case irms_int_number:
1292 sc_or(a->value, b->value, NULL);
1293 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1296 assert(0 && "operation not defined on mode");
1302 * bitwise exclusive or (xor)
1304 tarval *tarval_eor(tarval *a, tarval *b)
1309 assert((a->mode == b->mode));
1311 /* works even for vector modes */
1313 switch (get_mode_sort(a->mode))
1315 case irms_internal_boolean:
1316 return (a == b)? tarval_b_false : tarval_b_true;
1318 case irms_int_number:
1319 sc_xor(a->value, b->value, NULL);
1320 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1323 assert(0 && "operation not defined on mode");
1329 * bitwise left shift
1331 tarval *tarval_shl(tarval *a, tarval *b)
1333 char *temp_val = NULL;
1337 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1339 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1340 /* vector arithmetic not implemented yet */
1344 if (get_mode_modulo_shift(a->mode) != 0)
1346 temp_val = alloca(sc_get_buffer_length());
1348 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1349 sc_mod(b->value, temp_val, temp_val);
1352 temp_val = (char*)b->value;
1354 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1355 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1359 * bitwise unsigned right shift
1361 tarval *tarval_shr(tarval *a, tarval *b)
1363 char *temp_val = NULL;
1367 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1369 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1370 /* vector arithmetic not implemented yet */
1374 if (get_mode_modulo_shift(a->mode) != 0)
1376 temp_val = alloca(sc_get_buffer_length());
1378 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1379 sc_mod(b->value, temp_val, temp_val);
1382 temp_val = (char*)b->value;
1384 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1385 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1389 * bitwise signed right shift
1391 tarval *tarval_shrs(tarval *a, tarval *b)
1393 char *temp_val = NULL;
1397 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1399 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1400 /* vector arithmetic not implemented yet */
1404 if (get_mode_modulo_shift(a->mode) != 0)
1406 temp_val = alloca(sc_get_buffer_length());
1408 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1409 sc_mod(b->value, temp_val, temp_val);
1412 temp_val = (char*)b->value;
1414 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1415 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1421 tarval *tarval_rot(tarval *a, tarval *b)
1423 char *temp_val = NULL;
1427 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1429 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1430 /* vector arithmetic not implemented yet */
1434 if (get_mode_modulo_shift(a->mode) != 0)
1436 temp_val = alloca(sc_get_buffer_length());
1438 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1439 sc_mod(b->value, temp_val, temp_val);
1442 temp_val = (char*)b->value;
1444 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1445 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1449 * carry flag of the last operation
1451 int tarval_carry(void)
1453 return sc_had_carry();
1459 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1461 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1465 const tarval_mode_info *mode_info;
1466 const char *prefix, *suffix;
1470 mode_info = tv->mode->tv_priv;
1472 mode_info = &default_info;
1473 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1474 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1476 switch (get_mode_sort(tv->mode))
1478 case irms_reference:
1479 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1481 case irms_int_number:
1482 case irms_character:
1483 switch (mode_info->mode_output) {
1486 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1490 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1496 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1499 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1501 case irms_float_number:
1502 switch (mode_info->mode_output) {
1504 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1507 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1512 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1516 case irms_internal_boolean:
1517 switch (mode_info->mode_output) {
1523 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1527 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1530 case irms_control_flow:
1532 case irms_auxiliary:
1533 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1541 * Output of tarvals to stdio.
1543 int tarval_printf(tarval *tv) {
1547 res = tarval_snprintf(buf, sizeof(buf), tv);
1548 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1554 char *get_tarval_bitpattern(tarval *tv)
1557 int n = get_mode_size_bits(tv->mode);
1558 int bytes = (n + 7) / 8;
1559 char *res = xmalloc((n + 1) * sizeof(char));
1562 for(i = 0; i < bytes; i++) {
1563 byte = get_tarval_sub_bits(tv, i);
1564 for(j = 1; j < 256; j <<= 1)
1566 res[pos++] = j & byte ? '1' : '0';
1575 * access to the bitpattern
1577 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1579 switch (get_mode_sort(tv->mode)) {
1580 case irms_int_number:
1581 case irms_character:
1582 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1584 case irms_float_number:
1585 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1593 * Specify the output options of one mode.
1595 * This functions stores the modinfo, so DO NOT DESTROY it.
1597 * Returns zero on success.
1599 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1603 mode->tv_priv = modeinfo;
1608 * Returns the output options of one mode.
1610 * This functions returns the mode info of a given mode.
1612 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1616 return mode->tv_priv;
1620 * Identifying tarvals values for algebraic simplifications.
1623 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1624 * - TV_CLASSIFY_ONE for multiplicative neutral,
1625 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1626 * - TV_CLASSIFY_OTHER else
1628 tarval_classification_t classify_tarval(tarval *tv)
1631 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1633 if (tv == get_mode_null(tv->mode))
1634 return TV_CLASSIFY_NULL;
1635 else if (tv == get_mode_one(tv->mode))
1636 return TV_CLASSIFY_ONE;
1637 else if ((get_mode_sort(tv->mode) == irms_int_number)
1638 && (tv == new_tarval_from_long(-1, tv->mode)))
1639 return TV_CLASSIFY_ALL_ONE;
1641 return TV_CLASSIFY_OTHER;
1645 * Returns non-zero if a given (integer) tarval has only one single bit
1648 int is_single_bit_tarval(tarval *tv) {
1652 if (!tv || tv == tarval_bad) return 0;
1653 if (! mode_is_int(tv->mode)) return 0;
1655 l = get_mode_size_bytes(tv->mode);
1656 for (bits = 0, i = l - 1; i >= 0; --i) {
1657 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1659 /* check for more than one bit in these */
1671 * Sets the overflow mode for integer operations.
1673 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1674 int_overflow_mode = ov_mode;
1678 * Get the overflow mode for integer operations.
1680 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1681 return int_overflow_mode;
1685 * default mode_info for output as HEX
1687 static const tarval_mode_info hex_output = {
1694 * Initialization of the tarval module: called before init_mode()
1696 void init_tarval_1(long null_value)
1699 _null_value = null_value;
1701 /* initialize the sets holding the tarvals with a comparison function and
1702 * an initial size, which is the expected number of constants */
1703 tarvals = new_set(memcmp, N_CONSTANTS);
1704 values = new_set(memcmp, N_CONSTANTS);
1705 /* init strcalc with precision of 68 to support floating point values with 64
1706 * bit mantissa (needs extra bits for rounding and overflow) */
1712 * Initialization of the tarval module: called after init_mode()
1714 void init_tarval_2(void)
1718 tarval_bad->kind = k_tarval;
1719 tarval_bad->mode = mode_BAD;
1720 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1722 tarval_undefined->kind = k_tarval;
1723 tarval_undefined->mode = mode_ANY;
1724 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1726 tarval_b_true->kind = k_tarval;
1727 tarval_b_true->mode = mode_b;
1728 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1730 tarval_b_false->kind = k_tarval;
1731 tarval_b_false->mode = mode_b;
1732 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1735 * assign output modes that are compatible with the
1736 * old implementation: Hex output
1738 set_tarval_mode_output_option(mode_U, &hex_output);
1739 set_tarval_mode_output_option(mode_C, &hex_output);
1740 set_tarval_mode_output_option(mode_Bs, &hex_output);
1741 set_tarval_mode_output_option(mode_Bu, &hex_output);
1742 set_tarval_mode_output_option(mode_Hs, &hex_output);
1743 set_tarval_mode_output_option(mode_Hu, &hex_output);
1744 set_tarval_mode_output_option(mode_Is, &hex_output);
1745 set_tarval_mode_output_option(mode_Iu, &hex_output);
1746 set_tarval_mode_output_option(mode_Ls, &hex_output);
1747 set_tarval_mode_output_option(mode_Lu, &hex_output);
1748 set_tarval_mode_output_option(mode_P, &hex_output);
1751 /* free all memory occupied by tarval. */
1752 void finish_tarval(void) {
1755 del_set(tarvals); tarvals = NULL;
1756 del_set(values); values = NULL;
1759 /****************************************************************************
1761 ****************************************************************************/