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 static int hash_tv(tarval *tv)
142 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
145 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);
244 * public variables declared in tv.h
246 static tarval reserved_tv[4];
248 tarval *tarval_bad = &reserved_tv[0];
249 tarval *tarval_undefined = &reserved_tv[1];
250 tarval *tarval_b_false = &reserved_tv[2];
251 tarval *tarval_b_true = &reserved_tv[3];
254 * public functions declared in tv.h
258 * Constructors =============================================================
260 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
267 switch (get_mode_sort(mode))
269 case irms_control_flow:
275 case irms_internal_boolean:
276 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
277 if (strcasecmp(str, "true")) return tarval_b_true;
278 else if (strcasecmp(str, "false")) return tarval_b_true;
280 /* XXX This is C semantics */
281 return atoi(str) ? tarval_b_true : tarval_b_false;
283 case irms_float_number:
284 switch(get_mode_size_bits(mode)) {
286 fc_val_from_str(str, len, 8, 23, NULL);
289 fc_val_from_str(str, len, 11, 52, NULL);
292 fc_val_from_str(str, len, 15, 64, NULL);
295 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
298 /* same as integer modes */
299 case irms_int_number:
301 sc_val_from_str(str, len, NULL, mode);
302 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
305 assert(0); /* can't be reached, can it? */
310 * helper function, create a tarval from long
312 tarval *new_tarval_from_long(long l, ir_mode *mode)
317 switch(get_mode_sort(mode))
319 case irms_internal_boolean:
320 /* XXX C semantics ! */
321 return l ? tarval_b_true : tarval_b_false ;
324 /* same as integer modes */
325 case irms_int_number:
327 sc_val_from_long(l, NULL);
328 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
330 case irms_float_number:
331 return new_tarval_from_double((long double)l, mode);
334 assert(0 && "unsupported mode sort");
339 /* returns non-zero if can be converted to long */
340 int tarval_is_long(tarval *tv)
342 mode_sort sort = get_mode_sort(tv->mode);
345 if (sort != irms_int_number && sort != irms_character) return 0;
347 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
349 /* the value might be too big to fit in a long */
350 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
351 if (sc_comp(sc_get_buffer(), tv->value) == -1)
353 /* really doesn't fit */
360 /* this might overflow the machine's long, so use only with small values */
361 long get_tarval_long(tarval* tv)
364 assert(tarval_is_long(tv) && "tarval too big to fit in long");
366 return sc_val_to_long(tv->value);
369 tarval *new_tarval_from_double(long double d, ir_mode *mode)
372 assert(mode && (get_mode_sort(mode) == irms_float_number));
374 switch (get_mode_size_bits(mode)) {
376 fc_val_from_float(d, 8, 23, NULL);
379 fc_val_from_float(d, 11, 52, NULL);
382 fc_val_from_float(d, 15, 64, NULL);
385 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
388 /* returns non-zero if can be converted to double */
389 int tarval_is_double(tarval *tv)
394 return (get_mode_sort(tv->mode) == irms_float_number);
397 long double get_tarval_double(tarval *tv)
400 assert(tarval_is_double(tv));
402 return fc_val_to_float(tv->value);
407 * Access routines for tarval fields ========================================
410 /* get the mode of the tarval */
411 ir_mode *(get_tarval_mode)(const tarval *tv) {
412 return _get_tarval_mode(tv);
416 * Special value query functions ============================================
418 * These functions calculate and return a tarval representing the requested
420 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
421 * functions, but these are stored on initialization of the irmode module and
422 * therefore the irmode functions should be prefered to the functions below.
425 tarval *(get_tarval_bad)(void) {
426 return _get_tarval_bad();
429 tarval *(get_tarval_undefined)(void) {
430 return _get_tarval_undefined();
433 tarval *(get_tarval_b_false)(void) {
434 return _get_tarval_b_false();
437 tarval *(get_tarval_b_true)(void) {
438 return _get_tarval_b_true();
441 tarval *get_tarval_max(ir_mode *mode)
446 if (get_mode_n_vector_elems(mode) > 1) {
447 /* vector arithmetic not implemented yet */
451 switch(get_mode_sort(mode))
454 case irms_control_flow:
460 case irms_internal_boolean:
461 return tarval_b_true;
463 case irms_float_number:
464 switch(get_mode_size_bits(mode))
467 fc_get_max(8, 23, NULL);
470 fc_get_max(11, 52, NULL);
473 fc_get_max(15, 64, NULL);
476 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
478 case irms_int_number:
480 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
481 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
486 tarval *get_tarval_min(ir_mode *mode)
491 if (get_mode_n_vector_elems(mode) > 1) {
492 /* vector arithmetic not implemented yet */
496 switch(get_mode_sort(mode))
499 case irms_control_flow:
505 case irms_internal_boolean:
506 return tarval_b_false;
508 case irms_float_number:
509 switch(get_mode_size_bits(mode))
512 fc_get_min(8, 23, NULL);
515 fc_get_min(11, 52, NULL);
518 fc_get_min(15, 64, NULL);
521 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
523 case irms_int_number:
525 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
526 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
531 static long _null_value;
533 tarval *get_tarval_null(ir_mode *mode)
538 if (get_mode_n_vector_elems(mode) > 1) {
539 /* vector arithmetic not implemented yet */
543 switch(get_mode_sort(mode))
545 case irms_control_flow:
548 case irms_internal_boolean:
552 case irms_float_number:
553 return new_tarval_from_double(0.0, mode);
555 case irms_int_number:
557 return new_tarval_from_long(0l, mode);
560 return new_tarval_from_long(_null_value, mode);
565 tarval *get_tarval_one(ir_mode *mode)
570 if (get_mode_n_vector_elems(mode) > 1) {
571 /* vector arithmetic not implemented yet */
575 switch(get_mode_sort(mode))
577 case irms_control_flow:
580 case irms_internal_boolean:
585 case irms_float_number:
586 return new_tarval_from_double(1.0, mode);
588 case irms_int_number:
590 return new_tarval_from_long(1l, mode);
596 tarval *get_tarval_minus_one(ir_mode *mode)
601 if (get_mode_n_vector_elems(mode) > 1) {
602 /* vector arithmetic not implemented yet */
606 switch(get_mode_sort(mode))
608 case irms_control_flow:
611 case irms_internal_boolean:
616 case irms_float_number:
617 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
619 case irms_int_number:
621 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
626 tarval *get_tarval_nan(ir_mode *mode)
631 if (get_mode_n_vector_elems(mode) > 1) {
632 /* vector arithmetic not implemented yet */
636 if (get_mode_sort(mode) == irms_float_number) {
637 switch(get_mode_size_bits(mode))
640 fc_get_qnan(8, 23, NULL);
643 fc_get_qnan(11, 52, NULL);
646 fc_get_qnan(15, 64, NULL);
649 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
652 assert(0 && "tarval is not floating point");
657 tarval *get_tarval_plus_inf(ir_mode *mode)
662 if (get_mode_n_vector_elems(mode) > 1) {
663 /* vector arithmetic not implemented yet */
667 if (get_mode_sort(mode) == irms_float_number) {
668 switch(get_mode_size_bits(mode))
671 fc_get_plusinf(8, 23, NULL);
674 fc_get_plusinf(11, 52, NULL);
677 fc_get_plusinf(15, 64, NULL);
680 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
683 assert(0 && "tarval is not floating point");
688 tarval *get_tarval_minus_inf(ir_mode *mode)
693 if (get_mode_n_vector_elems(mode) > 1) {
694 /* vector arithmetic not implemented yet */
698 if (get_mode_sort(mode) == irms_float_number) {
699 switch(get_mode_size_bits(mode))
702 fc_get_minusinf(8, 23, NULL);
705 fc_get_minusinf(11, 52, NULL);
708 fc_get_minusinf(15, 64, NULL);
711 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
714 assert(0 && "tarval is not floating point");
720 * Arithmethic operations on tarvals ========================================
724 * test if negative number, 1 means 'yes'
726 int tarval_is_negative(tarval *a)
731 if (get_mode_n_vector_elems(a->mode) > 1) {
732 /* vector arithmetic not implemented yet */
733 assert(0 && "tarval_is_negative is not allowed for vector modes");
737 switch (get_mode_sort(a->mode))
739 case irms_int_number:
740 if (!mode_is_signed(a->mode)) return 0;
742 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
744 case irms_float_number:
745 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
748 assert(0 && "not implemented");
754 * test if null, 1 means 'yes'
756 int tarval_is_null(tarval *a)
758 ir_mode *m = get_tarval_mode(a);
760 return a == get_tarval_null(m);
764 * test if one, 1 means 'yes'
766 int tarval_is_one(tarval *a)
768 ir_mode *m = get_tarval_mode(a);
770 return a == get_tarval_one(m);
776 pn_Cmp tarval_cmp(tarval *a, tarval *b)
782 if (a == tarval_bad || b == tarval_bad) {
783 assert(0 && "Comparison with tarval_bad");
787 if (a == tarval_undefined || b == tarval_undefined)
790 if (a->mode != b->mode)
793 if (get_mode_n_vector_elems(a->mode) > 1) {
794 /* vector arithmetic not implemented yet */
795 assert(0 && "cmp not implemented for vector modes");
798 /* Here the two tarvals are unequal and of the same mode */
799 switch (get_mode_sort(a->mode))
801 case irms_control_flow:
809 case irms_float_number:
811 * BEWARE: we cannot compare a == b here, because
812 * a NaN is always Unordered to any other value, even to itself!
814 switch (fc_comp(a->value, b->value)) {
815 case -1: return pn_Cmp_Lt;
816 case 0: return pn_Cmp_Eq;
817 case 1: return pn_Cmp_Gt;
818 case 2: return pn_Cmp_Uo;
819 default: return pn_Cmp_False;
821 case irms_int_number:
825 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
827 case irms_internal_boolean:
830 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
836 * convert to other mode
838 tarval *tarval_convert_to(tarval *src, ir_mode *m)
846 if (src->mode == m) return src;
848 if (get_mode_n_vector_elems(src->mode) > 1) {
849 /* vector arithmetic not implemented yet */
853 switch (get_mode_sort(src->mode))
855 case irms_control_flow:
860 /* cast float to something */
861 case irms_float_number:
862 switch (get_mode_sort(m)) {
863 case irms_float_number:
864 switch (get_mode_size_bits(m))
867 fc_cast(src->value, 8, 23, NULL);
870 fc_cast(src->value, 11, 52, NULL);
873 fc_cast(src->value, 15, 64, NULL);
878 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
880 case irms_int_number:
881 switch (GET_FLOAT_TO_INT_MODE())
884 fc_int(src->value, NULL);
887 fc_rnd(src->value, NULL);
893 /* FIXME: floating point unit can't produce a value in integer
895 * an intermediate representation is needed here first. */
896 /* return get_tarval(); */
900 /* the rest can't be converted */
905 /* cast int to something */
906 case irms_int_number:
907 switch (get_mode_sort(m)) {
908 case irms_int_number:
910 return get_tarval_overflow(src->value, src->length, m);
912 case irms_internal_boolean:
913 /* XXX C semantics */
914 if (src == get_mode_null(src->mode)) return tarval_b_false;
915 else return tarval_b_true;
917 case irms_float_number:
918 /* XXX floating point unit does not understand internal integer
919 * representation, convert to string first, then create float from
921 buffer = alloca(100);
922 /* decimal string representation because hexadecimal output is
923 * interpreted unsigned by fc_val_from_str, so this is a HACK */
924 snprintf(buffer, 100, "%s",
925 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
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_int_number:
1479 case irms_character:
1480 switch (mode_info->mode_output) {
1483 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1487 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1493 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1496 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1498 case irms_float_number:
1499 switch (mode_info->mode_output) {
1501 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1504 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1509 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1513 case irms_reference:
1514 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1515 if (tv->value != NULL) {
1516 if (len > tv->length) {
1517 memcpy(buf, tv->value, tv->length);
1518 buf[tv->length] = '\0';
1522 memcpy(buf, tv->value, len-1);
1528 return snprintf(buf, len, "void");
1530 case irms_internal_boolean:
1531 switch (mode_info->mode_output) {
1537 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1541 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1544 case irms_control_flow:
1546 case irms_auxiliary:
1547 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1555 * Output of tarvals to stdio.
1557 int tarval_printf(tarval *tv) {
1561 res = tarval_snprintf(buf, sizeof(buf), tv);
1562 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1568 char *get_tarval_bitpattern(tarval *tv)
1571 int n = get_mode_size_bits(tv->mode);
1572 int bytes = (n + 7) / 8;
1573 char *res = xmalloc((n + 1) * sizeof(char));
1576 for(i = 0; i < bytes; i++) {
1577 byte = get_tarval_sub_bits(tv, i);
1578 for(j = 1; j < 256; j <<= 1)
1580 res[pos++] = j & byte ? '1' : '0';
1589 * access to the bitpattern
1591 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1593 switch (get_mode_sort(tv->mode)) {
1594 case irms_int_number:
1595 case irms_character:
1596 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1598 case irms_float_number:
1599 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1607 * Specify the output options of one mode.
1609 * This functions stores the modinfo, so DO NOT DESTROY it.
1611 * Returns zero on success.
1613 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1617 mode->tv_priv = modeinfo;
1622 * Returns the output options of one mode.
1624 * This functions returns the mode info of a given mode.
1626 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1630 return mode->tv_priv;
1634 * Identifying tarvals values for algebraic simplifications.
1637 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1638 * - TV_CLASSIFY_ONE for multiplicative neutral,
1639 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1640 * - TV_CLASSIFY_OTHER else
1642 tarval_classification_t classify_tarval(tarval *tv)
1645 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1647 if (tv == get_mode_null(tv->mode))
1648 return TV_CLASSIFY_NULL;
1649 else if (tv == get_mode_one(tv->mode))
1650 return TV_CLASSIFY_ONE;
1651 else if ((get_mode_sort(tv->mode) == irms_int_number)
1652 && (tv == new_tarval_from_long(-1, tv->mode)))
1653 return TV_CLASSIFY_ALL_ONE;
1655 return TV_CLASSIFY_OTHER;
1659 * Returns non-zero if a given (integer) tarval has only one single bit
1662 int is_single_bit_tarval(tarval *tv) {
1666 if (!tv || tv == tarval_bad) return 0;
1667 if (! mode_is_int(tv->mode)) return 0;
1669 l = get_mode_size_bytes(tv->mode);
1670 for (bits = 0, i = l - 1; i >= 0; --i) {
1671 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1673 /* check for more than one bit in these */
1685 * Sets the overflow mode for integer operations.
1687 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1688 int_overflow_mode = ov_mode;
1692 * Get the overflow mode for integer operations.
1694 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1695 return int_overflow_mode;
1699 * default mode_info for output as HEX
1701 static const tarval_mode_info hex_output = {
1708 * default mode_info for output as reference
1710 static const tarval_mode_info reference_output = {
1717 * Initialization of the tarval module: called before init_mode()
1719 void init_tarval_1(long null_value)
1722 _null_value = null_value;
1724 /* initialize the sets holding the tarvals with a comparison function and
1725 * an initial size, which is the expected number of constants */
1726 tarvals = new_set(memcmp, N_CONSTANTS);
1727 values = new_set(memcmp, N_CONSTANTS);
1728 /* init strcalc with precision of 68 to support floating point values with 64
1729 * bit mantissa (needs extra bits for rounding and overflow) */
1735 * Initialization of the tarval module: called after init_mode()
1737 void init_tarval_2(void)
1741 tarval_bad->kind = k_tarval;
1742 tarval_bad->mode = mode_BAD;
1744 tarval_undefined->kind = k_tarval;
1745 tarval_undefined->mode = mode_ANY;
1747 tarval_b_true->kind = k_tarval;
1748 tarval_b_true->mode = mode_b;
1750 tarval_b_false->kind = k_tarval;
1751 tarval_b_false->mode = mode_b;
1754 * assign output modes that are compatible with the
1755 * old implementation: Hex output
1757 set_tarval_mode_output_option(mode_U, &hex_output);
1758 set_tarval_mode_output_option(mode_C, &hex_output);
1759 set_tarval_mode_output_option(mode_Bs, &hex_output);
1760 set_tarval_mode_output_option(mode_Bu, &hex_output);
1761 set_tarval_mode_output_option(mode_Hs, &hex_output);
1762 set_tarval_mode_output_option(mode_Hu, &hex_output);
1763 set_tarval_mode_output_option(mode_Is, &hex_output);
1764 set_tarval_mode_output_option(mode_Iu, &hex_output);
1765 set_tarval_mode_output_option(mode_Ls, &hex_output);
1766 set_tarval_mode_output_option(mode_Lu, &hex_output);
1767 set_tarval_mode_output_option(mode_P, &reference_output);
1770 /* free all memory occupied by tarval. */
1771 void finish_tarval(void) {
1774 del_set(tarvals); tarvals = NULL;
1775 del_set(values); values = NULL;
1778 /****************************************************************************
1780 ****************************************************************************/