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 * Special value query functions ============================================
420 * These functions calculate and return a tarval representing the requested
422 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
423 * functions, but these are stored on initialization of the irmode module and
424 * therefore the irmode functions should be prefered to the functions below.
427 tarval *get_tarval_bad(void)
432 tarval *get_tarval_undefined(void)
435 return tarval_undefined;
437 tarval *get_tarval_b_false(void)
440 return tarval_b_false;
442 tarval *get_tarval_b_true(void)
445 return tarval_b_true;
447 tarval *get_tarval_P_void(void)
450 return tarval_P_void;
453 tarval *get_tarval_max(ir_mode *mode)
458 if (get_mode_n_vector_elems(mode) > 1) {
459 /* vector arithmetic not implemented yet */
463 switch(get_mode_sort(mode))
466 case irms_control_flow:
472 case irms_internal_boolean:
473 return tarval_b_true;
475 case irms_float_number:
476 switch(get_mode_size_bits(mode))
479 fc_get_max(8, 23, NULL);
482 fc_get_max(11, 52, NULL);
485 fc_get_max(15, 64, NULL);
488 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
490 case irms_int_number:
492 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
493 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
498 tarval *get_tarval_min(ir_mode *mode)
503 if (get_mode_n_vector_elems(mode) > 1) {
504 /* vector arithmetic not implemented yet */
508 switch(get_mode_sort(mode))
511 case irms_control_flow:
517 case irms_internal_boolean:
518 return tarval_b_false;
520 case irms_float_number:
521 switch(get_mode_size_bits(mode))
524 fc_get_min(8, 23, NULL);
527 fc_get_min(11, 52, NULL);
530 fc_get_min(15, 64, NULL);
533 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
535 case irms_int_number:
537 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
538 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
543 tarval *get_tarval_null(ir_mode *mode)
548 if (get_mode_n_vector_elems(mode) > 1) {
549 /* vector arithmetic not implemented yet */
553 switch(get_mode_sort(mode))
555 case irms_control_flow:
558 case irms_internal_boolean:
562 case irms_float_number:
563 return new_tarval_from_double(0.0, mode);
565 case irms_int_number:
567 return new_tarval_from_long(0l, mode);
570 return tarval_P_void;
575 tarval *get_tarval_one(ir_mode *mode)
580 if (get_mode_n_vector_elems(mode) > 1) {
581 /* vector arithmetic not implemented yet */
585 switch(get_mode_sort(mode))
587 case irms_control_flow:
590 case irms_internal_boolean:
595 case irms_float_number:
596 return new_tarval_from_double(1.0, mode);
598 case irms_int_number:
600 return new_tarval_from_long(1l, mode);
606 tarval *get_tarval_nan(ir_mode *mode)
611 if (get_mode_n_vector_elems(mode) > 1) {
612 /* vector arithmetic not implemented yet */
616 if (get_mode_sort(mode) == irms_float_number) {
617 switch(get_mode_size_bits(mode))
620 fc_get_qnan(8, 23, NULL);
623 fc_get_qnan(11, 52, NULL);
626 fc_get_qnan(15, 64, NULL);
629 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
632 assert(0 && "tarval is not floating point");
637 tarval *get_tarval_inf(ir_mode *mode)
642 if (get_mode_n_vector_elems(mode) > 1) {
643 /* vector arithmetic not implemented yet */
647 if (get_mode_sort(mode) == irms_float_number) {
648 switch(get_mode_size_bits(mode))
651 fc_get_plusinf(8, 23, NULL);
654 fc_get_plusinf(11, 52, NULL);
657 fc_get_plusinf(15, 64, NULL);
660 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
663 assert(0 && "tarval is not floating point");
669 * Arithmethic operations on tarvals ========================================
673 * test if negative number, 1 means 'yes'
675 int tarval_is_negative(tarval *a)
680 if (get_mode_n_vector_elems(a->mode) > 1) {
681 /* vector arithmetic not implemented yet */
682 assert(0 && "tarval_is_negative is not allowed for vector modes");
686 switch (get_mode_sort(a->mode))
688 case irms_int_number:
689 if (!mode_is_signed(a->mode)) return 0;
691 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
693 case irms_float_number:
694 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
697 assert(0 && "not implemented");
703 * test if null, 1 means 'yes'
705 int tarval_is_null(tarval *a)
707 ir_mode *m = get_tarval_mode(a);
709 return a == get_tarval_null(m);
713 * test if one, 1 means 'yes'
715 int tarval_is_one(tarval *a)
717 ir_mode *m = get_tarval_mode(a);
719 return a == get_tarval_one(m);
725 pnc_number tarval_cmp(tarval *a, tarval *b)
731 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
732 if (a == tarval_undefined || b == tarval_undefined) return False;
733 if (a == b) return Eq;
734 if (a->mode != b->mode) return False;
736 if (get_mode_n_vector_elems(a->mode) > 1) {
737 /* vector arithmetic not implemented yet */
738 assert(0 && "cmp not implemented for vector modes");
741 /* Here the two tarvals are unequal and of the same mode */
742 switch (get_mode_sort(a->mode))
744 case irms_control_flow:
750 case irms_float_number:
751 switch (fc_comp(a->value, b->value)) {
753 case 0: assert(0 && "different tarvals compare equal"); return Eq;
756 default: return False;
758 case irms_int_number:
760 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
762 case irms_internal_boolean:
763 return (a == tarval_b_true)?(Gt):(Lt);
769 * convert to other mode
771 tarval *tarval_convert_to(tarval *src, ir_mode *m)
779 if (src->mode == m) return src;
781 if (get_mode_n_vector_elems(src->mode) > 1) {
782 /* vector arithmetic not implemented yet */
786 switch (get_mode_sort(src->mode))
788 case irms_control_flow:
793 /* cast float to something */
794 case irms_float_number:
795 switch (get_mode_sort(m)) {
796 case irms_float_number:
797 switch (get_mode_size_bits(m))
800 fc_cast(src->value, 8, 23, NULL);
803 fc_cast(src->value, 11, 52, NULL);
806 fc_cast(src->value, 15, 64, NULL);
811 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
813 case irms_int_number:
814 switch (GET_FLOAT_TO_INT_MODE())
817 fc_int(src->value, NULL);
820 fc_rnd(src->value, NULL);
825 /* XXX floating point unit can't produce a value in integer
827 * an intermediate representation is needed here first. */
828 /* return get_tarval(); */
832 /* the rest can't be converted */
837 /* cast int to something */
838 case irms_int_number:
839 switch (get_mode_sort(m)) {
840 case irms_int_number:
842 return get_tarval_overflow(src->value, src->length, m);
844 case irms_internal_boolean:
845 /* XXX C semantics */
846 if (src == get_mode_null(src->mode)) return tarval_b_false;
847 else return tarval_b_true;
849 case irms_float_number:
850 /* XXX floating point unit does not understand internal integer
851 * representation, convert to string first, then create float from
853 buffer = alloca(100);
854 /* decimal string representation because hexadecimal output is
855 * interpreted unsigned by fc_val_from_str, so this is a HACK */
856 snprintf(buffer, 100, "%s",
857 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
858 switch (get_mode_size_bits(m))
861 fc_val_from_str(buffer, 0, 8, 23, NULL);
864 fc_val_from_str(buffer, 0, 11, 52, NULL);
867 fc_val_from_str(buffer, 0, 15, 64, NULL);
870 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
877 case irms_internal_boolean:
878 switch (get_mode_sort(m))
880 case irms_int_number:
881 if (src == tarval_b_true) return get_mode_one(m);
882 else return get_mode_null(m);
901 tarval *tarval_not(tarval *a)
907 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
909 /* works for vector mode without changes */
911 switch (get_mode_sort(a->mode))
913 case irms_int_number:
914 buffer = alloca(sc_get_buffer_length());
915 sc_not(a->value, buffer);
916 return get_tarval(buffer, a->length, a->mode);
924 * arithmetic negation
926 tarval *tarval_neg(tarval *a)
932 assert(mode_is_num(a->mode)); /* negation only for numerical values */
934 /* note: negation is allowed even for unsigned modes. */
936 if (get_mode_n_vector_elems(a->mode) > 1) {
937 /* vector arithmetic not implemented yet */
941 switch (get_mode_sort(a->mode))
943 case irms_int_number:
944 buffer = alloca(sc_get_buffer_length());
945 sc_neg(a->value, buffer);
946 return get_tarval_overflow(buffer, a->length, a->mode);
948 case irms_float_number:
949 fc_neg(a->value, NULL);
950 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
960 tarval *tarval_add(tarval *a, tarval *b)
967 assert(a->mode == b->mode);
969 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
970 /* vector arithmetic not implemented yet */
974 switch (get_mode_sort(a->mode))
977 case irms_int_number:
978 /* modes of a,b are equal, so result has mode of a as this might be the character */
979 buffer = alloca(sc_get_buffer_length());
980 sc_add(a->value, b->value, buffer);
981 return get_tarval_overflow(buffer, a->length, a->mode);
983 case irms_float_number:
984 fc_add(a->value, b->value, NULL);
985 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
995 tarval *tarval_sub(tarval *a, tarval *b)
1002 assert(a->mode == b->mode);
1004 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1005 /* vector arithmetic not implemented yet */
1008 switch (get_mode_sort(a->mode))
1010 case irms_character:
1011 case irms_int_number:
1012 /* modes of a,b are equal, so result has mode of a as this might be the character */
1013 buffer = alloca(sc_get_buffer_length());
1014 sc_sub(a->value, b->value, buffer);
1015 return get_tarval_overflow(buffer, a->length, a->mode);
1017 case irms_float_number:
1018 fc_sub(a->value, b->value, NULL);
1019 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1029 tarval *tarval_mul(tarval *a, tarval *b)
1036 assert(a->mode == b->mode);
1038 if (get_mode_n_vector_elems(a->mode) > 1) {
1039 /* vector arithmetic not implemented yet */
1043 switch (get_mode_sort(a->mode))
1045 case irms_int_number:
1046 /* modes of a,b are equal */
1047 buffer = alloca(sc_get_buffer_length());
1048 sc_mul(a->value, b->value, buffer);
1049 return get_tarval_overflow(buffer, a->length, a->mode);
1051 case irms_float_number:
1052 fc_mul(a->value, b->value, NULL);
1053 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1061 * floating point division
1063 tarval *tarval_quo(tarval *a, tarval *b)
1068 assert((a->mode == b->mode) && mode_is_float(a->mode));
1070 if (get_mode_n_vector_elems(a->mode) > 1) {
1071 /* vector arithmetic not implemented yet */
1075 fc_div(a->value, b->value, NULL);
1076 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1081 * overflow is impossible, but look out for division by zero
1083 tarval *tarval_div(tarval *a, tarval *b)
1088 assert((a->mode == b->mode) && mode_is_int(a->mode));
1090 if (get_mode_n_vector_elems(a->mode) > 1) {
1091 /* vector arithmetic not implemented yet */
1096 if (b == get_mode_null(b->mode)) return tarval_bad;
1097 /* modes of a,b are equal */
1098 sc_div(a->value, b->value, NULL);
1099 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1104 * overflow is impossible, but look out for division by zero
1106 tarval *tarval_mod(tarval *a, tarval *b)
1111 assert((a->mode == b->mode) && mode_is_int(a->mode));
1113 if (get_mode_n_vector_elems(a->mode) > 1) {
1114 /* vector arithmetic not implemented yet */
1119 if (b == get_mode_null(b->mode)) return tarval_bad;
1120 /* modes of a,b are equal */
1121 sc_mod(a->value, b->value, NULL);
1122 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1128 tarval *tarval_abs(tarval *a)
1134 assert(mode_is_num(a->mode));
1136 if (get_mode_n_vector_elems(a->mode) > 1) {
1137 /* vector arithmetic not implemented yet */
1141 switch (get_mode_sort(a->mode))
1143 case irms_int_number:
1144 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1146 buffer = alloca(sc_get_buffer_length());
1147 sc_neg(a->value, buffer);
1148 return get_tarval_overflow(buffer, a->length, a->mode);
1152 case irms_float_number:
1153 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1155 fc_neg(a->value, NULL);
1156 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1169 tarval *tarval_and(tarval *a, tarval *b)
1174 assert(a->mode == b->mode);
1176 /* works even for vector modes */
1178 switch(get_mode_sort(a->mode))
1180 case irms_internal_boolean:
1181 return (a == tarval_b_false) ? a : b;
1183 case irms_int_number:
1184 sc_and(a->value, b->value, NULL);
1185 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1188 assert(0 && "operation not defined on mode");
1196 tarval *tarval_or (tarval *a, tarval *b)
1201 assert(a->mode == b->mode);
1203 /* works even for vector modes */
1205 switch (get_mode_sort(a->mode))
1207 case irms_internal_boolean:
1208 return (a == tarval_b_true) ? a : b;
1210 case irms_int_number:
1211 sc_or(a->value, b->value, NULL);
1212 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1215 assert(0 && "operation not defined on mode");
1221 * bitwise exclusive or (xor)
1223 tarval *tarval_eor(tarval *a, tarval *b)
1228 assert((a->mode == b->mode));
1230 /* works even for vector modes */
1232 switch (get_mode_sort(a->mode))
1234 case irms_internal_boolean:
1235 return (a == b)? tarval_b_false : tarval_b_true;
1237 case irms_int_number:
1238 sc_xor(a->value, b->value, NULL);
1239 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1242 assert(0 && "operation not defined on mode");
1248 * bitwise left shift
1250 tarval *tarval_shl(tarval *a, tarval *b)
1252 char *temp_val = NULL;
1256 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1258 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1259 /* vector arithmetic not implemented yet */
1263 if (get_mode_modulo_shift(a->mode) != 0)
1265 temp_val = alloca(sc_get_buffer_length());
1267 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1268 sc_mod(b->value, temp_val, temp_val);
1271 temp_val = (char*)b->value;
1273 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1274 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1278 * bitwise unsigned right shift
1280 tarval *tarval_shr(tarval *a, tarval *b)
1282 char *temp_val = NULL;
1286 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1288 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1289 /* vector arithmetic not implemented yet */
1293 if (get_mode_modulo_shift(a->mode) != 0)
1295 temp_val = alloca(sc_get_buffer_length());
1297 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1298 sc_mod(b->value, temp_val, temp_val);
1301 temp_val = (char*)b->value;
1303 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1304 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1308 * bitwise signed right shift
1310 tarval *tarval_shrs(tarval *a, tarval *b)
1312 char *temp_val = NULL;
1316 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1318 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1319 /* vector arithmetic not implemented yet */
1323 if (get_mode_modulo_shift(a->mode) != 0)
1325 temp_val = alloca(sc_get_buffer_length());
1327 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1328 sc_mod(b->value, temp_val, temp_val);
1331 temp_val = (char*)b->value;
1333 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1334 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1340 tarval *tarval_rot(tarval *a, tarval *b)
1342 char *temp_val = NULL;
1346 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1348 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1349 /* vector arithmetic not implemented yet */
1353 if (get_mode_modulo_shift(a->mode) != 0)
1355 temp_val = alloca(sc_get_buffer_length());
1357 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1358 sc_mod(b->value, temp_val, temp_val);
1361 temp_val = (char*)b->value;
1363 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1364 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1368 * carry flag of the last operation
1370 int tarval_carry(void)
1372 return sc_had_carry();
1378 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1380 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1384 const tarval_mode_info *mode_info;
1385 const char *prefix, *suffix;
1389 mode_info = tv->mode->tv_priv;
1391 mode_info = &default_info;
1392 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1393 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1395 switch (get_mode_sort(tv->mode))
1397 case irms_int_number:
1398 case irms_character:
1399 switch (mode_info->mode_output) {
1402 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1406 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1412 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1415 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1417 case irms_float_number:
1418 switch (mode_info->mode_output) {
1420 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1423 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1428 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1432 case irms_reference:
1433 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1434 if (tv->value != NULL){
1435 if (len > tv->length) {
1436 memcpy(buf, tv->value, tv->length);
1437 buf[tv->length] = '\0';
1441 memcpy(buf, tv->value, len-1);
1447 return snprintf(buf, len, "void");
1449 case irms_internal_boolean:
1450 switch (mode_info->mode_output) {
1456 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1460 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1463 case irms_control_flow:
1465 case irms_auxiliary:
1466 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1474 * Output of tarvals to stdio.
1476 int tarval_printf(tarval *tv) {
1480 res = tarval_snprintf(buf, sizeof(buf), tv);
1481 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1487 char *get_tarval_bitpattern(tarval *tv)
1490 int n = get_mode_size_bits(tv->mode);
1491 int bytes = (n + 7) / 8;
1492 char *res = malloc((n + 1) * sizeof(char));
1495 for(i = 0; i < bytes; i++) {
1496 byte = get_tarval_sub_bits(tv, i);
1497 for(j = 1; j < 256; j <<= 1)
1499 res[pos++] = j & byte ? '1' : '0';
1508 * access to the bitpattern
1510 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1512 switch (get_mode_sort(tv->mode)) {
1513 case irms_int_number:
1514 case irms_character:
1515 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1517 case irms_float_number:
1518 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1526 * Specify the output options of one mode.
1528 * This functions stores the modinfo, so DO NOT DESTROY it.
1530 * Returns zero on success.
1532 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1536 mode->tv_priv = modeinfo;
1541 * Returns the output options of one mode.
1543 * This functions returns the modinfo of a given mode.
1545 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1549 return mode->tv_priv;
1553 * Identifying tarvals values for algebraic simplifications.
1556 * - TV_CLASSIFY_NULL for additive neutral,
1557 * - TV_CLASSIFY_ONE for multiplicative neutral,
1558 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1559 * - TV_CLASSIFY_OTHER else
1561 tarval_classification_t classify_tarval(tarval *tv)
1564 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1566 if (tv == get_mode_null(tv->mode))
1567 return TV_CLASSIFY_NULL;
1568 else if (tv == get_mode_one(tv->mode))
1569 return TV_CLASSIFY_ONE;
1570 else if ((get_mode_sort(tv->mode) == irms_int_number)
1571 && (tv == new_tarval_from_long(-1, tv->mode)))
1572 return TV_CLASSIFY_ALL_ONE;
1574 return TV_CLASSIFY_OTHER;
1578 * Sets the overflow mode for integer operations.
1580 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1581 int_overflow_mode = ov_mode;
1585 * Get the overflow mode for integer operations.
1587 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1588 return int_overflow_mode;
1592 * default mode_info for output as HEX
1594 static const tarval_mode_info hex_output = {
1601 * default mode_info for output as reference
1603 static const tarval_mode_info reference_output = {
1611 * Initialization of the tarval module: called before init_mode()
1613 void init_tarval_1(void)
1616 /* initialize the sets holding the tarvals with a comparison function and
1617 * an initial size, which is the expected number of constants */
1618 tarvals = new_set(memcmp, N_CONSTANTS);
1619 values = new_set(memcmp, N_CONSTANTS);
1620 /* init strcalc with precision of 68 to support floating point values with 64
1621 * bit mantissa (needs extra bits for rounding and overflow) */
1627 * Initialization of the tarval module: called after init_mode()
1629 void init_tarval_2(void)
1633 tarval_bad->mode = mode_BAD;
1634 tarval_undefined->mode = mode_ANY;
1635 tarval_b_true->mode = mode_b;
1636 tarval_b_false->mode = mode_b;
1637 tarval_P_void->mode = mode_P;
1640 * assign output modes that are compatible with the
1641 * old implementation: Hex output
1643 set_tarval_mode_output_option(mode_U, &hex_output);
1644 set_tarval_mode_output_option(mode_C, &hex_output);
1645 set_tarval_mode_output_option(mode_Bs, &hex_output);
1646 set_tarval_mode_output_option(mode_Bu, &hex_output);
1647 set_tarval_mode_output_option(mode_Hs, &hex_output);
1648 set_tarval_mode_output_option(mode_Hu, &hex_output);
1649 set_tarval_mode_output_option(mode_Is, &hex_output);
1650 set_tarval_mode_output_option(mode_Iu, &hex_output);
1651 set_tarval_mode_output_option(mode_Ls, &hex_output);
1652 set_tarval_mode_output_option(mode_Lu, &hex_output);
1653 set_tarval_mode_output_option(mode_P, &reference_output);
1656 /* free all memory occupied by tarval. */
1657 void finish_tarval(void) {
1660 del_set(tarvals); tarvals = NULL;
1661 del_set(values); values = NULL;
1664 /****************************************************************************
1666 ****************************************************************************/