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() */
31 #include <string.h> /* nice things for strings */
33 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
44 #include "set.h" /* to store tarvals in */
45 /* #include "tune.h" */ /* some constants */
46 #include "entity_t.h" /* needed to store pointers to entities */
48 #include "irnode.h" /* defines boolean return values (pnc_number)*/
53 /** Size of hash tables. Should correspond to average number of distinct constant
55 #define N_CONSTANTS 2048
57 /* get the integer overflow mode */
58 #define GET_OVERFLOW_MODE() int_overflow_mode
60 /* unused, float to int doesn't work yet */
63 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
65 #define SWITCH_NOINFINITY 0
66 #define SWITCH_NODENORMALS 0
68 /****************************************************************************
69 * local definitions and macros
70 ****************************************************************************/
72 # define TARVAL_VERIFY(a) tarval_verify((a))
74 # define TARVAL_VERIFY(a) ((void)0)
77 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
78 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
80 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
81 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
83 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
85 static long long count = 0;
86 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
88 # define ANNOUNCE() ((void)0)
90 /****************************************************************************
92 ****************************************************************************/
93 static struct set *tarvals; /* container for tarval structs */
94 static struct set *values; /* container for values */
95 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
97 /****************************************************************************
99 ****************************************************************************/
101 static int hash_val(const void *value, unsigned int length);
102 static int hash_tv(tarval *tv);
103 static void _fail_verify(tarval *tv, const char* file, int line)
105 /* print a memory image of the tarval and throw an assertion */
107 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
109 printf("%s:%d: Invalid tarval (null)", file, line);
113 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
116 INLINE static void tarval_verify(tarval *tv)
122 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
123 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
125 if (!FIND_TARVAL(tv)) fail_verify(tv);
126 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
132 static int hash_tv(tarval *tv)
134 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
137 static int hash_val(const void *value, unsigned int length)
140 unsigned int hash = 0;
142 /* scramble the byte - array */
143 for (i = 0; i < length; i++)
145 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
146 hash += (hash << 11) ^ (hash >> 17);
152 /* finds tarval with value/mode or creates new tarval */
153 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
160 /* if there already is such a value, it is returned, else value
161 * is copied into the set */
162 tv.value = INSERT_VALUE(value, length);
166 /* if there is such a tarval, it is returned, else tv is copied
168 return (tarval *)INSERT_TARVAL(&tv);
174 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
176 switch (get_mode_sort(mode))
178 case irms_int_number:
179 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
180 switch (GET_OVERFLOW_MODE()) {
181 case TV_OVERFLOW_SATURATE:
182 return get_mode_max(mode);
183 case TV_OVERFLOW_WRAP:
185 char *temp = alloca(sc_get_buffer_length());
186 char *diff = alloca(sc_get_buffer_length());
187 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
188 sc_val_from_ulong(1, temp);
189 sc_add(diff, temp, diff);
190 sc_sub(value, diff, temp);
191 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
192 sc_sub(temp, diff, temp);
193 return get_tarval(temp, length, mode);
195 case TV_OVERFLOW_BAD:
198 return get_tarval(value, length, mode);
201 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
202 switch (GET_OVERFLOW_MODE()) {
203 case TV_OVERFLOW_SATURATE:
204 return get_mode_min(mode);
205 case TV_OVERFLOW_WRAP:
207 char *temp = alloca(sc_get_buffer_length());
208 char *diff = alloca(sc_get_buffer_length());
209 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
210 sc_val_from_ulong(1, temp);
211 sc_add(diff, temp, diff);
212 sc_add(value, diff, temp);
213 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
214 sc_add(temp, diff, temp);
215 return get_tarval(temp, length, mode);
217 case TV_OVERFLOW_BAD:
220 return get_tarval(value, length, mode);
225 case irms_float_number:
226 if (SWITCH_NOINFINITY && fc_is_inf(value))
228 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
231 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
233 return get_mode_null(mode);
239 return get_tarval(value, length, mode);
244 * public variables declared in tv.h
246 static tarval reserved_tv[5];
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];
252 tarval *tarval_P_void = &reserved_tv[4];
255 * public functions declared in tv.h
259 * Constructors =============================================================
261 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
268 switch (get_mode_sort(mode))
270 case irms_control_flow:
276 case irms_internal_boolean:
277 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
278 if (strcasecmp(str, "true")) return tarval_b_true;
279 else if (strcasecmp(str, "false")) return tarval_b_true;
281 /* XXX This is C semantics */
282 return atoi(str) ? tarval_b_true : tarval_b_false;
284 case irms_float_number:
285 switch(get_mode_size_bits(mode)) {
287 fc_val_from_str(str, len, 8, 23, NULL);
290 fc_val_from_str(str, len, 11, 52, NULL);
293 fc_val_from_str(str, len, 15, 64, NULL);
296 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
298 case irms_int_number:
300 sc_val_from_str(str, len, NULL);
301 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
304 return get_tarval(str, len, mode);
307 assert(0); /* can't be reached, can it? */
312 * helper function, create a tarval from long
314 tarval *new_tarval_from_long(long l, ir_mode *mode)
317 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
319 switch(get_mode_sort(mode))
321 case irms_internal_boolean:
322 /* XXX C semantics ! */
323 return l ? tarval_b_true : tarval_b_false ;
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 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
342 /* returns non-zero if can be converted to long */
343 int tarval_is_long(tarval *tv)
346 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
348 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
350 /* the value might be too big to fit in a long */
351 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
352 if (sc_comp(sc_get_buffer(), tv->value) == -1)
354 /* really doesn't fit */
361 /* this might overflow the machine's long, so use only with small values */
362 long get_tarval_long(tarval* tv)
365 assert(tarval_is_long(tv) && "tarval too big to fit in long");
367 return sc_val_to_long(tv->value);
370 tarval *new_tarval_from_double(long double d, ir_mode *mode)
373 assert(mode && (get_mode_sort(mode) == irms_float_number));
375 switch (get_mode_size_bits(mode)) {
377 fc_val_from_float(d, 8, 23, NULL);
380 fc_val_from_float(d, 11, 52, NULL);
383 fc_val_from_float(d, 15, 64, NULL);
386 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
389 /* returns non-zero if can be converted to double */
390 int tarval_is_double(tarval *tv)
395 return (get_mode_sort(tv->mode) == irms_float_number);
398 long double get_tarval_double(tarval *tv)
401 assert(tarval_is_double(tv));
403 return fc_val_to_float(tv->value);
408 * Access routines for tarval fields ========================================
410 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
418 void *get_tarval_link (tarval *tv)
427 * Special value query functions ============================================
429 * These functions calculate and return a tarval representing the requested
431 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
432 * functions, but these are stored on initialization of the irmode module and
433 * therefore the irmode functions should be prefered to the functions below.
436 tarval *get_tarval_bad(void)
441 tarval *get_tarval_undefined(void)
444 return tarval_undefined;
446 tarval *get_tarval_b_false(void)
449 return tarval_b_false;
451 tarval *get_tarval_b_true(void)
454 return tarval_b_true;
456 tarval *get_tarval_P_void(void)
459 return tarval_P_void;
462 tarval *get_tarval_max(ir_mode *mode)
467 if (get_mode_n_vector_elems(mode) > 1) {
468 /* vector arithmetic not implemented yet */
472 switch(get_mode_sort(mode))
475 case irms_control_flow:
481 case irms_internal_boolean:
482 return tarval_b_true;
484 case irms_float_number:
485 switch(get_mode_size_bits(mode))
488 fc_get_max(8, 23, NULL);
491 fc_get_max(11, 52, NULL);
494 fc_get_max(15, 64, NULL);
497 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
499 case irms_int_number:
501 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
502 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
507 tarval *get_tarval_min(ir_mode *mode)
512 if (get_mode_n_vector_elems(mode) > 1) {
513 /* vector arithmetic not implemented yet */
517 switch(get_mode_sort(mode))
520 case irms_control_flow:
526 case irms_internal_boolean:
527 return tarval_b_false;
529 case irms_float_number:
530 switch(get_mode_size_bits(mode))
533 fc_get_min(8, 23, NULL);
536 fc_get_min(11, 52, NULL);
539 fc_get_min(15, 64, NULL);
542 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
544 case irms_int_number:
546 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
547 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
552 tarval *get_tarval_null(ir_mode *mode)
557 if (get_mode_n_vector_elems(mode) > 1) {
558 /* vector arithmetic not implemented yet */
562 switch(get_mode_sort(mode))
564 case irms_control_flow:
567 case irms_internal_boolean:
571 case irms_float_number:
572 return new_tarval_from_double(0.0, mode);
574 case irms_int_number:
576 return new_tarval_from_long(0l, mode);
579 return tarval_P_void;
584 tarval *get_tarval_one(ir_mode *mode)
589 if (get_mode_n_vector_elems(mode) > 1) {
590 /* vector arithmetic not implemented yet */
594 switch(get_mode_sort(mode))
596 case irms_control_flow:
599 case irms_internal_boolean:
604 case irms_float_number:
605 return new_tarval_from_double(1.0, mode);
607 case irms_int_number:
609 return new_tarval_from_long(1l, mode);
615 tarval *get_tarval_nan(ir_mode *mode)
620 if (get_mode_n_vector_elems(mode) > 1) {
621 /* vector arithmetic not implemented yet */
625 if (get_mode_sort(mode) == irms_float_number) {
626 switch(get_mode_size_bits(mode))
629 fc_get_qnan(8, 23, NULL);
632 fc_get_qnan(11, 52, NULL);
635 fc_get_qnan(15, 64, NULL);
638 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
641 assert(0 && "tarval is not floating point");
646 tarval *get_tarval_inf(ir_mode *mode)
651 if (get_mode_n_vector_elems(mode) > 1) {
652 /* vector arithmetic not implemented yet */
656 if (get_mode_sort(mode) == irms_float_number) {
657 switch(get_mode_size_bits(mode))
660 fc_get_plusinf(8, 23, NULL);
663 fc_get_plusinf(11, 52, NULL);
666 fc_get_plusinf(15, 64, NULL);
669 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
672 assert(0 && "tarval is not floating point");
678 * Arithmethic operations on tarvals ========================================
682 * test if negative number, 1 means 'yes'
684 int tarval_is_negative(tarval *a)
689 if (get_mode_n_vector_elems(a->mode) > 1) {
690 /* vector arithmetic not implemented yet */
691 assert(0 && "tarval_is_negative is not allowed for vector modes");
695 switch (get_mode_sort(a->mode))
697 case irms_int_number:
698 if (!mode_is_signed(a->mode)) return 0;
700 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
702 case irms_float_number:
703 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
706 assert(0 && "not implemented");
712 * test if null, 1 means 'yes'
714 int tarval_is_null(tarval *a)
716 ir_mode *m = get_tarval_mode(a);
718 return a == get_tarval_null(m);
722 * test if one, 1 means 'yes'
724 int tarval_is_one(tarval *a)
726 ir_mode *m = get_tarval_mode(a);
728 return a == get_tarval_one(m);
734 pnc_number tarval_cmp(tarval *a, tarval *b)
740 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
741 if (a == tarval_undefined || b == tarval_undefined) return False;
742 if (a == b) return Eq;
743 if (a->mode != b->mode) return False;
745 if (get_mode_n_vector_elems(a->mode) > 1) {
746 /* vector arithmetic not implemented yet */
747 assert(0 && "cmp not implemented for vector modes");
750 /* Here the two tarvals are unequal and of the same mode */
751 switch (get_mode_sort(a->mode))
753 case irms_control_flow:
759 case irms_float_number:
760 switch (fc_comp(a->value, b->value)) {
762 case 0: assert(0 && "different tarvals compare equal"); return Eq;
765 default: return False;
767 case irms_int_number:
769 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
771 case irms_internal_boolean:
772 return (a == tarval_b_true)?(Gt):(Lt);
778 * convert to other mode
780 tarval *tarval_convert_to(tarval *src, ir_mode *m)
788 if (src->mode == m) return src;
790 if (get_mode_n_vector_elems(src->mode) > 1) {
791 /* vector arithmetic not implemented yet */
795 switch (get_mode_sort(src->mode))
797 case irms_control_flow:
802 /* cast float to something */
803 case irms_float_number:
804 switch (get_mode_sort(m)) {
805 case irms_float_number:
806 switch (get_mode_size_bits(m))
809 fc_cast(src->value, 8, 23, NULL);
812 fc_cast(src->value, 11, 52, NULL);
815 fc_cast(src->value, 15, 64, NULL);
820 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
822 case irms_int_number:
823 switch (GET_FLOAT_TO_INT_MODE())
826 fc_int(src->value, NULL);
829 fc_rnd(src->value, NULL);
834 /* XXX floating point unit can't produce a value in integer
836 * an intermediate representation is needed here first. */
837 /* return get_tarval(); */
841 /* the rest can't be converted */
846 /* cast int to something */
847 case irms_int_number:
848 switch (get_mode_sort(m)) {
849 case irms_int_number:
851 return get_tarval_overflow(src->value, src->length, m);
853 case irms_internal_boolean:
854 /* XXX C semantics */
855 if (src == get_mode_null(src->mode)) return tarval_b_false;
856 else return tarval_b_true;
858 case irms_float_number:
859 /* XXX floating point unit does not understand internal integer
860 * representation, convert to string first, then create float from
862 buffer = alloca(100);
863 /* decimal string representation because hexadecimal output is
864 * interpreted unsigned by fc_val_from_str, so this is a HACK */
865 snprintf(buffer, 100, "%s",
866 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
867 switch (get_mode_size_bits(m))
870 fc_val_from_str(buffer, 0, 8, 23, NULL);
873 fc_val_from_str(buffer, 0, 11, 52, NULL);
876 fc_val_from_str(buffer, 0, 15, 64, NULL);
879 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
886 case irms_internal_boolean:
887 switch (get_mode_sort(m))
889 case irms_int_number:
890 if (src == tarval_b_true) return get_mode_one(m);
891 else return get_mode_null(m);
910 tarval *tarval_not(tarval *a)
916 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
918 /* works for vector mode without changes */
920 switch (get_mode_sort(a->mode))
922 case irms_int_number:
923 buffer = alloca(sc_get_buffer_length());
924 sc_not(a->value, buffer);
925 return get_tarval(buffer, a->length, a->mode);
933 * arithmetic negation
935 tarval *tarval_neg(tarval *a)
941 assert(mode_is_num(a->mode)); /* negation only for numerical values */
943 /* note: negation is allowed even for unsigned modes. */
945 if (get_mode_n_vector_elems(a->mode) > 1) {
946 /* vector arithmetic not implemented yet */
950 switch (get_mode_sort(a->mode))
952 case irms_int_number:
953 buffer = alloca(sc_get_buffer_length());
954 sc_neg(a->value, buffer);
955 return get_tarval_overflow(buffer, a->length, a->mode);
957 case irms_float_number:
958 fc_neg(a->value, NULL);
959 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
969 tarval *tarval_add(tarval *a, tarval *b)
976 assert(a->mode == b->mode);
978 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
979 /* vector arithmetic not implemented yet */
983 switch (get_mode_sort(a->mode))
986 case irms_int_number:
987 /* modes of a,b are equal, so result has mode of a as this might be the character */
988 buffer = alloca(sc_get_buffer_length());
989 sc_add(a->value, b->value, buffer);
990 return get_tarval_overflow(buffer, a->length, a->mode);
992 case irms_float_number:
993 fc_add(a->value, b->value, NULL);
994 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1004 tarval *tarval_sub(tarval *a, tarval *b)
1011 assert(a->mode == b->mode);
1013 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1014 /* vector arithmetic not implemented yet */
1017 switch (get_mode_sort(a->mode))
1019 case irms_character:
1020 case irms_int_number:
1021 /* modes of a,b are equal, so result has mode of a as this might be the character */
1022 buffer = alloca(sc_get_buffer_length());
1023 sc_sub(a->value, b->value, buffer);
1024 return get_tarval_overflow(buffer, a->length, a->mode);
1026 case irms_float_number:
1027 fc_sub(a->value, b->value, NULL);
1028 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1038 tarval *tarval_mul(tarval *a, tarval *b)
1045 assert(a->mode == b->mode);
1047 if (get_mode_n_vector_elems(a->mode) > 1) {
1048 /* vector arithmetic not implemented yet */
1052 switch (get_mode_sort(a->mode))
1054 case irms_int_number:
1055 /* modes of a,b are equal */
1056 buffer = alloca(sc_get_buffer_length());
1057 sc_mul(a->value, b->value, buffer);
1058 return get_tarval_overflow(buffer, a->length, a->mode);
1060 case irms_float_number:
1061 fc_mul(a->value, b->value, NULL);
1062 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1070 * floating point division
1072 tarval *tarval_quo(tarval *a, tarval *b)
1077 assert((a->mode == b->mode) && mode_is_float(a->mode));
1079 if (get_mode_n_vector_elems(a->mode) > 1) {
1080 /* vector arithmetic not implemented yet */
1084 fc_div(a->value, b->value, NULL);
1085 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1090 * overflow is impossible, but look out for division by zero
1092 tarval *tarval_div(tarval *a, tarval *b)
1097 assert((a->mode == b->mode) && mode_is_int(a->mode));
1099 if (get_mode_n_vector_elems(a->mode) > 1) {
1100 /* vector arithmetic not implemented yet */
1105 if (b == get_mode_null(b->mode)) return tarval_bad;
1106 /* modes of a,b are equal */
1107 sc_div(a->value, b->value, NULL);
1108 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1113 * overflow is impossible, but look out for division by zero
1115 tarval *tarval_mod(tarval *a, tarval *b)
1120 assert((a->mode == b->mode) && mode_is_int(a->mode));
1122 if (get_mode_n_vector_elems(a->mode) > 1) {
1123 /* vector arithmetic not implemented yet */
1128 if (b == get_mode_null(b->mode)) return tarval_bad;
1129 /* modes of a,b are equal */
1130 sc_mod(a->value, b->value, NULL);
1131 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1137 tarval *tarval_abs(tarval *a)
1143 assert(mode_is_num(a->mode));
1145 if (get_mode_n_vector_elems(a->mode) > 1) {
1146 /* vector arithmetic not implemented yet */
1150 switch (get_mode_sort(a->mode))
1152 case irms_int_number:
1153 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1155 buffer = alloca(sc_get_buffer_length());
1156 sc_neg(a->value, buffer);
1157 return get_tarval_overflow(buffer, a->length, a->mode);
1161 case irms_float_number:
1162 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1164 fc_neg(a->value, NULL);
1165 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1178 tarval *tarval_and(tarval *a, tarval *b)
1183 assert(a->mode == b->mode);
1185 /* works even for vector modes */
1187 switch(get_mode_sort(a->mode))
1189 case irms_internal_boolean:
1190 return (a == tarval_b_false) ? a : b;
1192 case irms_int_number:
1193 sc_and(a->value, b->value, NULL);
1194 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1197 assert(0 && "operation not defined on mode");
1205 tarval *tarval_or (tarval *a, tarval *b)
1210 assert(a->mode == b->mode);
1212 /* works even for vector modes */
1214 switch (get_mode_sort(a->mode))
1216 case irms_internal_boolean:
1217 return (a == tarval_b_true) ? a : b;
1219 case irms_int_number:
1220 sc_or(a->value, b->value, NULL);
1221 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1224 assert(0 && "operation not defined on mode");
1230 * bitwise exclusive or (xor)
1232 tarval *tarval_eor(tarval *a, tarval *b)
1237 assert((a->mode == b->mode));
1239 /* works even for vector modes */
1241 switch (get_mode_sort(a->mode))
1243 case irms_internal_boolean:
1244 return (a == b)? tarval_b_false : tarval_b_true;
1246 case irms_int_number:
1247 sc_xor(a->value, b->value, NULL);
1248 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1251 assert(0 && "operation not defined on mode");
1257 * bitwise left shift
1259 tarval *tarval_shl(tarval *a, tarval *b)
1261 char *temp_val = NULL;
1265 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1267 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1268 /* vector arithmetic not implemented yet */
1272 if (get_mode_modulo_shift(a->mode) != 0)
1274 temp_val = alloca(sc_get_buffer_length());
1276 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1277 sc_mod(b->value, temp_val, temp_val);
1280 temp_val = (char*)b->value;
1282 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1283 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1287 * bitwise unsigned right shift
1289 tarval *tarval_shr(tarval *a, tarval *b)
1291 char *temp_val = NULL;
1295 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1297 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1298 /* vector arithmetic not implemented yet */
1302 if (get_mode_modulo_shift(a->mode) != 0)
1304 temp_val = alloca(sc_get_buffer_length());
1306 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1307 sc_mod(b->value, temp_val, temp_val);
1310 temp_val = (char*)b->value;
1312 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1313 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1317 * bitwise signed right shift
1319 tarval *tarval_shrs(tarval *a, tarval *b)
1321 char *temp_val = NULL;
1325 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1327 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1328 /* vector arithmetic not implemented yet */
1332 if (get_mode_modulo_shift(a->mode) != 0)
1334 temp_val = alloca(sc_get_buffer_length());
1336 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1337 sc_mod(b->value, temp_val, temp_val);
1340 temp_val = (char*)b->value;
1342 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1343 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1349 tarval *tarval_rot(tarval *a, tarval *b)
1351 char *temp_val = NULL;
1355 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1357 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1358 /* vector arithmetic not implemented yet */
1362 if (get_mode_modulo_shift(a->mode) != 0)
1364 temp_val = alloca(sc_get_buffer_length());
1366 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1367 sc_mod(b->value, temp_val, temp_val);
1370 temp_val = (char*)b->value;
1372 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1373 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1377 * carry flag of the last operation
1379 int tarval_carry(void)
1381 return sc_had_carry();
1387 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1389 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1393 const tarval_mode_info *mode_info;
1394 const char *prefix, *suffix;
1398 mode_info = tv->mode->tv_priv;
1400 mode_info = &default_info;
1401 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1402 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1404 switch (get_mode_sort(tv->mode))
1406 case irms_int_number:
1407 case irms_character:
1408 switch (mode_info->mode_output) {
1411 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1415 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1421 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1424 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1426 case irms_float_number:
1427 switch (mode_info->mode_output) {
1429 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1432 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1437 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1441 case irms_reference:
1442 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1443 if (tv->value != NULL){
1444 if (len > tv->length) {
1445 memcpy(buf, tv->value, tv->length);
1446 buf[tv->length] = '\0';
1450 memcpy(buf, tv->value, len-1);
1456 return snprintf(buf, len, "void");
1458 case irms_internal_boolean:
1459 switch (mode_info->mode_output) {
1465 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1469 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1472 case irms_control_flow:
1474 case irms_auxiliary:
1475 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1483 * Output of tarvals to stdio.
1485 int tarval_printf(tarval *tv) {
1489 res = tarval_snprintf(buf, sizeof(buf), tv);
1490 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1496 char *get_tarval_bitpattern(tarval *tv)
1499 int n = get_mode_size_bits(tv->mode);
1500 int bytes = (n + 7) / 8;
1501 char *res = malloc((n + 1) * sizeof(char));
1504 for(i = 0; i < bytes; i++) {
1505 byte = get_tarval_sub_bits(tv, i);
1506 for(j = 1; j < 256; j <<= 1)
1508 res[pos++] = j & byte ? '1' : '0';
1517 * access to the bitpattern
1519 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1521 switch (get_mode_sort(tv->mode)) {
1522 case irms_int_number:
1523 case irms_character:
1524 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1526 case irms_float_number:
1527 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1535 * Specify the output options of one mode.
1537 * This functions stores the modinfo, so DO NOT DESTROY it.
1539 * Returns zero on success.
1541 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1545 mode->tv_priv = modeinfo;
1550 * Returns the output options of one mode.
1552 * This functions returns the modinfo of a given mode.
1554 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1558 return mode->tv_priv;
1562 * Identifying tarvals values for algebraic simplifications.
1565 * - TV_CLASSIFY_NULL for additive neutral,
1566 * - TV_CLASSIFY_ONE for multiplicative neutral,
1567 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1568 * - TV_CLASSIFY_OTHER else
1570 tarval_classification_t classify_tarval(tarval *tv)
1573 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1575 if (tv == get_mode_null(tv->mode))
1576 return TV_CLASSIFY_NULL;
1577 else if (tv == get_mode_one(tv->mode))
1578 return TV_CLASSIFY_ONE;
1579 else if ((get_mode_sort(tv->mode) == irms_int_number)
1580 && (tv == new_tarval_from_long(-1, tv->mode)))
1581 return TV_CLASSIFY_ALL_ONE;
1583 return TV_CLASSIFY_OTHER;
1587 * Sets the overflow mode for integer operations.
1589 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1590 int_overflow_mode = ov_mode;
1594 * Get the overflow mode for integer operations.
1596 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1597 return int_overflow_mode;
1601 * default mode_info for output as HEX
1603 static const tarval_mode_info hex_output = {
1610 * default mode_info for output as reference
1612 static const tarval_mode_info reference_output = {
1620 * Initialization of the tarval module: called before init_mode()
1622 void init_tarval_1(void)
1625 /* initialize the sets holding the tarvals with a comparison function and
1626 * an initial size, which is the expected number of constants */
1627 tarvals = new_set(memcmp, N_CONSTANTS);
1628 values = new_set(memcmp, N_CONSTANTS);
1629 /* init strcalc with precision of 68 to support floating point values with 64
1630 * bit mantissa (needs extra bits for rounding and overflow) */
1636 * Initialization of the tarval module: called after init_mode()
1638 void init_tarval_2(void)
1642 tarval_bad->mode = mode_BAD;
1643 tarval_undefined->mode = mode_ANY;
1644 tarval_b_true->mode = mode_b;
1645 tarval_b_false->mode = mode_b;
1646 tarval_P_void->mode = mode_P;
1649 * assign output modes that are compatible with the
1650 * old implementation: Hex output
1652 set_tarval_mode_output_option(mode_U, &hex_output);
1653 set_tarval_mode_output_option(mode_C, &hex_output);
1654 set_tarval_mode_output_option(mode_Bs, &hex_output);
1655 set_tarval_mode_output_option(mode_Bu, &hex_output);
1656 set_tarval_mode_output_option(mode_Hs, &hex_output);
1657 set_tarval_mode_output_option(mode_Hu, &hex_output);
1658 set_tarval_mode_output_option(mode_Is, &hex_output);
1659 set_tarval_mode_output_option(mode_Iu, &hex_output);
1660 set_tarval_mode_output_option(mode_Ls, &hex_output);
1661 set_tarval_mode_output_option(mode_Lu, &hex_output);
1662 set_tarval_mode_output_option(mode_P, &reference_output);
1665 /* free all memory occupied by tarval. */
1666 void finish_tarval(void) {
1669 del_set(tarvals); tarvals = NULL;
1670 del_set(values); values = NULL;
1673 /****************************************************************************
1675 ****************************************************************************/