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 */
46 #include "set.h" /* to store tarvals in */
47 #include "entity_t.h" /* needed to store pointers to entities */
49 #include "irnode.h" /* defines boolean return values (pnc_number)*/
54 /** Size of hash tables. Should correspond to average number of distinct constant
56 #define N_CONSTANTS 2048
58 /* get the integer overflow mode */
59 #define GET_OVERFLOW_MODE() int_overflow_mode
61 /* unused, float to int doesn't work yet */
62 enum float_to_int_mode {
67 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
69 #define SWITCH_NOINFINITY 0
70 #define SWITCH_NODENORMALS 0
72 /****************************************************************************
73 * local definitions and macros
74 ****************************************************************************/
76 # define TARVAL_VERIFY(a) tarval_verify((a))
78 # define TARVAL_VERIFY(a) ((void)0)
81 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
82 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
84 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
85 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
87 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
89 static long long count = 0;
90 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
92 # define ANNOUNCE() ((void)0)
94 /****************************************************************************
96 ****************************************************************************/
97 static struct set *tarvals; /* container for tarval structs */
98 static struct set *values; /* container for values */
99 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
101 /****************************************************************************
103 ****************************************************************************/
105 static int hash_val(const void *value, unsigned int length);
106 static int hash_tv(tarval *tv);
107 static void _fail_verify(tarval *tv, const char* file, int line)
109 /* print a memory image of the tarval and throw an assertion */
111 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
113 printf("%s:%d: Invalid tarval (null)", file, line);
117 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
120 INLINE static void tarval_verify(tarval *tv)
126 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
127 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
129 if (!FIND_TARVAL(tv)) fail_verify(tv);
130 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
136 static int hash_tv(tarval *tv)
138 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
141 static int hash_val(const void *value, unsigned int length)
144 unsigned int hash = 0;
146 /* scramble the byte - array */
147 for (i = 0; i < length; i++)
149 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
150 hash += (hash << 11) ^ (hash >> 17);
156 /* finds tarval with value/mode or creates new tarval */
157 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
164 /* if there already is such a value, it is returned, else value
165 * is copied into the set */
166 tv.value = INSERT_VALUE(value, length);
170 /* if there is such a tarval, it is returned, else tv is copied
172 return (tarval *)INSERT_TARVAL(&tv);
178 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
180 switch (get_mode_sort(mode))
182 case irms_int_number:
183 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
184 switch (GET_OVERFLOW_MODE()) {
185 case TV_OVERFLOW_SATURATE:
186 return get_mode_max(mode);
187 case TV_OVERFLOW_WRAP:
189 char *temp = alloca(sc_get_buffer_length());
190 char *diff = alloca(sc_get_buffer_length());
191 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
192 sc_val_from_ulong(1, temp);
193 sc_add(diff, temp, diff);
194 sc_sub(value, diff, temp);
195 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
196 sc_sub(temp, diff, 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 char *diff = alloca(sc_get_buffer_length());
213 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
214 sc_val_from_ulong(1, temp);
215 sc_add(diff, temp, diff);
216 sc_add(value, diff, temp);
217 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
218 sc_add(temp, diff, temp);
219 return get_tarval(temp, length, mode);
221 case TV_OVERFLOW_BAD:
224 return get_tarval(value, length, mode);
229 case irms_float_number:
230 if (SWITCH_NOINFINITY && fc_is_inf(value))
232 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
235 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
237 return get_mode_null(mode);
243 return get_tarval(value, length, mode);
248 * public variables declared in tv.h
250 static tarval reserved_tv[5];
252 tarval *tarval_bad = &reserved_tv[0];
253 tarval *tarval_undefined = &reserved_tv[1];
254 tarval *tarval_b_false = &reserved_tv[2];
255 tarval *tarval_b_true = &reserved_tv[3];
256 tarval *tarval_P_void = &reserved_tv[4];
259 * public functions declared in tv.h
263 * Constructors =============================================================
265 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
272 switch (get_mode_sort(mode))
274 case irms_control_flow:
280 case irms_internal_boolean:
281 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
282 if (strcasecmp(str, "true")) return tarval_b_true;
283 else if (strcasecmp(str, "false")) return tarval_b_true;
285 /* XXX This is C semantics */
286 return atoi(str) ? tarval_b_true : tarval_b_false;
288 case irms_float_number:
289 switch(get_mode_size_bits(mode)) {
291 fc_val_from_str(str, len, 8, 23, NULL);
294 fc_val_from_str(str, len, 11, 52, NULL);
297 fc_val_from_str(str, len, 15, 64, NULL);
300 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
302 case irms_int_number:
304 sc_val_from_str(str, len, NULL);
305 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
308 return get_tarval(str, len, mode);
311 assert(0); /* can't be reached, can it? */
316 * helper function, create a tarval from long
318 tarval *new_tarval_from_long(long l, ir_mode *mode)
323 switch(get_mode_sort(mode))
325 case irms_internal_boolean:
326 /* XXX C semantics ! */
327 return l ? tarval_b_true : tarval_b_false ;
329 case irms_int_number:
331 sc_val_from_long(l, NULL);
332 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
334 case irms_float_number:
335 return new_tarval_from_double((long double)l, mode);
338 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
341 assert(0 && "unsupported mode sort");
346 /* returns non-zero if can be converted to long */
347 int tarval_is_long(tarval *tv)
349 mode_sort sort = get_mode_sort(tv->mode);
352 if (sort != irms_int_number && sort != irms_character) return 0;
354 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
356 /* the value might be too big to fit in a long */
357 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
358 if (sc_comp(sc_get_buffer(), tv->value) == -1)
360 /* really doesn't fit */
367 /* this might overflow the machine's long, so use only with small values */
368 long get_tarval_long(tarval* tv)
371 assert(tarval_is_long(tv) && "tarval too big to fit in long");
373 return sc_val_to_long(tv->value);
376 tarval *new_tarval_from_double(long double d, ir_mode *mode)
379 assert(mode && (get_mode_sort(mode) == irms_float_number));
381 switch (get_mode_size_bits(mode)) {
383 fc_val_from_float(d, 8, 23, NULL);
386 fc_val_from_float(d, 11, 52, NULL);
389 fc_val_from_float(d, 15, 64, NULL);
392 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
395 /* returns non-zero if can be converted to double */
396 int tarval_is_double(tarval *tv)
401 return (get_mode_sort(tv->mode) == irms_float_number);
404 long double get_tarval_double(tarval *tv)
407 assert(tarval_is_double(tv));
409 return fc_val_to_float(tv->value);
414 * Access routines for tarval fields ========================================
417 /* get the mode of the tarval */
418 ir_mode *(get_tarval_mode)(const tarval *tv) {
419 return _get_tarval_mode(tv);
423 * Special value query functions ============================================
425 * These functions calculate and return a tarval representing the requested
427 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
428 * functions, but these are stored on initialization of the irmode module and
429 * therefore the irmode functions should be prefered to the functions below.
432 tarval *(get_tarval_bad)(void) {
433 return _get_tarval_bad();
436 tarval *(get_tarval_undefined)(void) {
437 return _get_tarval_undefined();
440 tarval *(get_tarval_b_false)(void) {
441 return _get_tarval_b_false();
444 tarval *(get_tarval_b_true)(void) {
445 return _get_tarval_b_true();
448 tarval *(get_tarval_P_void)(void) {
449 return _get_tarval_P_void();
452 tarval *get_tarval_max(ir_mode *mode)
457 if (get_mode_n_vector_elems(mode) > 1) {
458 /* vector arithmetic not implemented yet */
462 switch(get_mode_sort(mode))
465 case irms_control_flow:
471 case irms_internal_boolean:
472 return tarval_b_true;
474 case irms_float_number:
475 switch(get_mode_size_bits(mode))
478 fc_get_max(8, 23, NULL);
481 fc_get_max(11, 52, NULL);
484 fc_get_max(15, 64, NULL);
487 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
489 case irms_int_number:
491 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
492 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
497 tarval *get_tarval_min(ir_mode *mode)
502 if (get_mode_n_vector_elems(mode) > 1) {
503 /* vector arithmetic not implemented yet */
507 switch(get_mode_sort(mode))
510 case irms_control_flow:
516 case irms_internal_boolean:
517 return tarval_b_false;
519 case irms_float_number:
520 switch(get_mode_size_bits(mode))
523 fc_get_min(8, 23, NULL);
526 fc_get_min(11, 52, NULL);
529 fc_get_min(15, 64, NULL);
532 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
534 case irms_int_number:
536 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
537 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
542 tarval *get_tarval_null(ir_mode *mode)
547 if (get_mode_n_vector_elems(mode) > 1) {
548 /* vector arithmetic not implemented yet */
552 switch(get_mode_sort(mode))
554 case irms_control_flow:
557 case irms_internal_boolean:
561 case irms_float_number:
562 return new_tarval_from_double(0.0, mode);
564 case irms_int_number:
566 return new_tarval_from_long(0l, mode);
569 return tarval_P_void;
574 tarval *get_tarval_one(ir_mode *mode)
579 if (get_mode_n_vector_elems(mode) > 1) {
580 /* vector arithmetic not implemented yet */
584 switch(get_mode_sort(mode))
586 case irms_control_flow:
589 case irms_internal_boolean:
594 case irms_float_number:
595 return new_tarval_from_double(1.0, mode);
597 case irms_int_number:
599 return new_tarval_from_long(1l, mode);
605 tarval *get_tarval_minus_one(ir_mode *mode)
610 if (get_mode_n_vector_elems(mode) > 1) {
611 /* vector arithmetic not implemented yet */
615 switch(get_mode_sort(mode))
617 case irms_control_flow:
620 case irms_internal_boolean:
625 case irms_float_number:
626 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
628 case irms_int_number:
630 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
635 tarval *get_tarval_nan(ir_mode *mode)
640 if (get_mode_n_vector_elems(mode) > 1) {
641 /* vector arithmetic not implemented yet */
645 if (get_mode_sort(mode) == irms_float_number) {
646 switch(get_mode_size_bits(mode))
649 fc_get_qnan(8, 23, NULL);
652 fc_get_qnan(11, 52, NULL);
655 fc_get_qnan(15, 64, NULL);
658 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
661 assert(0 && "tarval is not floating point");
666 tarval *get_tarval_plus_inf(ir_mode *mode)
671 if (get_mode_n_vector_elems(mode) > 1) {
672 /* vector arithmetic not implemented yet */
676 if (get_mode_sort(mode) == irms_float_number) {
677 switch(get_mode_size_bits(mode))
680 fc_get_plusinf(8, 23, NULL);
683 fc_get_plusinf(11, 52, NULL);
686 fc_get_plusinf(15, 64, NULL);
689 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
692 assert(0 && "tarval is not floating point");
697 tarval *get_tarval_minus_inf(ir_mode *mode)
702 if (get_mode_n_vector_elems(mode) > 1) {
703 /* vector arithmetic not implemented yet */
707 if (get_mode_sort(mode) == irms_float_number) {
708 switch(get_mode_size_bits(mode))
711 fc_get_minusinf(8, 23, NULL);
714 fc_get_minusinf(11, 52, NULL);
717 fc_get_minusinf(15, 64, NULL);
720 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
723 assert(0 && "tarval is not floating point");
729 * Arithmethic operations on tarvals ========================================
733 * test if negative number, 1 means 'yes'
735 int tarval_is_negative(tarval *a)
740 if (get_mode_n_vector_elems(a->mode) > 1) {
741 /* vector arithmetic not implemented yet */
742 assert(0 && "tarval_is_negative is not allowed for vector modes");
746 switch (get_mode_sort(a->mode))
748 case irms_int_number:
749 if (!mode_is_signed(a->mode)) return 0;
751 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
753 case irms_float_number:
754 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
757 assert(0 && "not implemented");
763 * test if null, 1 means 'yes'
765 int tarval_is_null(tarval *a)
767 ir_mode *m = get_tarval_mode(a);
769 return a == get_tarval_null(m);
773 * test if one, 1 means 'yes'
775 int tarval_is_one(tarval *a)
777 ir_mode *m = get_tarval_mode(a);
779 return a == get_tarval_one(m);
785 pn_Cmp tarval_cmp(tarval *a, tarval *b)
791 if (a == tarval_bad || b == tarval_bad) {
792 assert(0 && "Comparison with tarval_bad");
796 if (a == tarval_undefined || b == tarval_undefined)
799 if (a->mode != b->mode)
802 if (get_mode_n_vector_elems(a->mode) > 1) {
803 /* vector arithmetic not implemented yet */
804 assert(0 && "cmp not implemented for vector modes");
807 /* Here the two tarvals are unequal and of the same mode */
808 switch (get_mode_sort(a->mode))
810 case irms_control_flow:
818 case irms_float_number:
820 * BEWARE: we cannot compare a == b here, because
821 * a NaN is always Unordered to any other value, even to itself!
823 switch (fc_comp(a->value, b->value)) {
824 case -1: return pn_Cmp_Lt;
825 case 0: return pn_Cmp_Eq;
826 case 1: return pn_Cmp_Gt;
827 case 2: return pn_Cmp_Uo;
828 default: return pn_Cmp_False;
830 case irms_int_number:
834 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
836 case irms_internal_boolean:
839 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
845 * convert to other mode
847 tarval *tarval_convert_to(tarval *src, ir_mode *m)
855 if (src->mode == m) return src;
857 if (get_mode_n_vector_elems(src->mode) > 1) {
858 /* vector arithmetic not implemented yet */
862 switch (get_mode_sort(src->mode))
864 case irms_control_flow:
869 /* cast float to something */
870 case irms_float_number:
871 switch (get_mode_sort(m)) {
872 case irms_float_number:
873 switch (get_mode_size_bits(m))
876 fc_cast(src->value, 8, 23, NULL);
879 fc_cast(src->value, 11, 52, NULL);
882 fc_cast(src->value, 15, 64, NULL);
887 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
889 case irms_int_number:
890 switch (GET_FLOAT_TO_INT_MODE())
893 fc_int(src->value, NULL);
896 fc_rnd(src->value, NULL);
902 /* FIXME: floating point unit can't produce a value in integer
904 * an intermediate representation is needed here first. */
905 /* return get_tarval(); */
909 /* the rest can't be converted */
914 /* cast int to something */
915 case irms_int_number:
916 switch (get_mode_sort(m)) {
917 case irms_int_number:
919 return get_tarval_overflow(src->value, src->length, m);
921 case irms_internal_boolean:
922 /* XXX C semantics */
923 if (src == get_mode_null(src->mode)) return tarval_b_false;
924 else return tarval_b_true;
926 case irms_float_number:
927 /* XXX floating point unit does not understand internal integer
928 * representation, convert to string first, then create float from
930 buffer = alloca(100);
931 /* decimal string representation because hexadecimal output is
932 * interpreted unsigned by fc_val_from_str, so this is a HACK */
933 snprintf(buffer, 100, "%s",
934 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
935 switch (get_mode_size_bits(m))
938 fc_val_from_str(buffer, 0, 8, 23, NULL);
941 fc_val_from_str(buffer, 0, 11, 52, NULL);
944 fc_val_from_str(buffer, 0, 15, 64, NULL);
947 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
951 /* allow 0 to be casted */
952 if (src == get_mode_null(src->mode))
953 return get_mode_null(m);
961 case irms_internal_boolean:
962 switch (get_mode_sort(m))
964 case irms_int_number:
965 if (src == tarval_b_true) return get_mode_one(m);
966 else return get_mode_null(m);
985 tarval *tarval_not(tarval *a)
992 /* works for vector mode without changes */
994 switch (get_mode_sort(a->mode))
996 case irms_int_number:
997 buffer = alloca(sc_get_buffer_length());
998 sc_not(a->value, buffer);
999 return get_tarval(buffer, a->length, a->mode);
1001 case irms_internal_boolean:
1002 if (a == tarval_b_true)
1003 return tarval_b_false;
1004 if (a == tarval_b_false)
1005 return tarval_b_true;
1009 assert(0 && "bitwise negation is only allowed for integer and boolean");
1015 * arithmetic negation
1017 tarval *tarval_neg(tarval *a)
1023 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1025 /* note: negation is allowed even for unsigned modes. */
1027 if (get_mode_n_vector_elems(a->mode) > 1) {
1028 /* vector arithmetic not implemented yet */
1032 switch (get_mode_sort(a->mode))
1034 case irms_int_number:
1035 buffer = alloca(sc_get_buffer_length());
1036 sc_neg(a->value, buffer);
1037 return get_tarval_overflow(buffer, a->length, a->mode);
1039 case irms_float_number:
1040 fc_neg(a->value, NULL);
1041 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1051 tarval *tarval_add(tarval *a, tarval *b)
1058 assert(a->mode == b->mode);
1060 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1061 /* vector arithmetic not implemented yet */
1065 switch (get_mode_sort(a->mode))
1067 case irms_character:
1068 case irms_int_number:
1069 /* modes of a,b are equal, so result has mode of a as this might be the character */
1070 buffer = alloca(sc_get_buffer_length());
1071 sc_add(a->value, b->value, buffer);
1072 return get_tarval_overflow(buffer, a->length, a->mode);
1074 case irms_float_number:
1075 fc_add(a->value, b->value, NULL);
1076 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1086 tarval *tarval_sub(tarval *a, tarval *b)
1093 assert(a->mode == b->mode);
1095 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1096 /* vector arithmetic not implemented yet */
1099 switch (get_mode_sort(a->mode))
1101 case irms_character:
1102 case irms_int_number:
1103 /* modes of a,b are equal, so result has mode of a as this might be the character */
1104 buffer = alloca(sc_get_buffer_length());
1105 sc_sub(a->value, b->value, buffer);
1106 return get_tarval_overflow(buffer, a->length, a->mode);
1108 case irms_float_number:
1109 fc_sub(a->value, b->value, NULL);
1110 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1120 tarval *tarval_mul(tarval *a, tarval *b)
1127 assert(a->mode == b->mode);
1129 if (get_mode_n_vector_elems(a->mode) > 1) {
1130 /* vector arithmetic not implemented yet */
1134 switch (get_mode_sort(a->mode))
1136 case irms_int_number:
1137 /* modes of a,b are equal */
1138 buffer = alloca(sc_get_buffer_length());
1139 sc_mul(a->value, b->value, buffer);
1140 return get_tarval_overflow(buffer, a->length, a->mode);
1142 case irms_float_number:
1143 fc_mul(a->value, b->value, NULL);
1144 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1152 * floating point division
1154 tarval *tarval_quo(tarval *a, tarval *b)
1159 assert((a->mode == b->mode) && mode_is_float(a->mode));
1161 if (get_mode_n_vector_elems(a->mode) > 1) {
1162 /* vector arithmetic not implemented yet */
1166 fc_div(a->value, b->value, NULL);
1167 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1172 * overflow is impossible, but look out for division by zero
1174 tarval *tarval_div(tarval *a, tarval *b)
1179 assert((a->mode == b->mode) && mode_is_int(a->mode));
1181 if (get_mode_n_vector_elems(a->mode) > 1) {
1182 /* vector arithmetic not implemented yet */
1187 if (b == get_mode_null(b->mode)) return tarval_bad;
1188 /* modes of a,b are equal */
1189 sc_div(a->value, b->value, NULL);
1190 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1195 * overflow is impossible, but look out for division by zero
1197 tarval *tarval_mod(tarval *a, tarval *b)
1202 assert((a->mode == b->mode) && mode_is_int(a->mode));
1204 if (get_mode_n_vector_elems(a->mode) > 1) {
1205 /* vector arithmetic not implemented yet */
1210 if (b == get_mode_null(b->mode)) return tarval_bad;
1211 /* modes of a,b are equal */
1212 sc_mod(a->value, b->value, NULL);
1213 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1219 tarval *tarval_abs(tarval *a)
1225 assert(mode_is_num(a->mode));
1227 if (get_mode_n_vector_elems(a->mode) > 1) {
1228 /* vector arithmetic not implemented yet */
1232 switch (get_mode_sort(a->mode))
1234 case irms_int_number:
1235 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1237 buffer = alloca(sc_get_buffer_length());
1238 sc_neg(a->value, buffer);
1239 return get_tarval_overflow(buffer, a->length, a->mode);
1243 case irms_float_number:
1244 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1246 fc_neg(a->value, NULL);
1247 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1260 tarval *tarval_and(tarval *a, tarval *b)
1265 assert(a->mode == b->mode);
1267 /* works even for vector modes */
1269 switch(get_mode_sort(a->mode))
1271 case irms_internal_boolean:
1272 return (a == tarval_b_false) ? a : b;
1274 case irms_int_number:
1275 sc_and(a->value, b->value, NULL);
1276 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1279 assert(0 && "operation not defined on mode");
1287 tarval *tarval_or (tarval *a, tarval *b)
1292 assert(a->mode == b->mode);
1294 /* works even for vector modes */
1296 switch (get_mode_sort(a->mode))
1298 case irms_internal_boolean:
1299 return (a == tarval_b_true) ? a : b;
1301 case irms_int_number:
1302 sc_or(a->value, b->value, NULL);
1303 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1306 assert(0 && "operation not defined on mode");
1312 * bitwise exclusive or (xor)
1314 tarval *tarval_eor(tarval *a, tarval *b)
1319 assert((a->mode == b->mode));
1321 /* works even for vector modes */
1323 switch (get_mode_sort(a->mode))
1325 case irms_internal_boolean:
1326 return (a == b)? tarval_b_false : tarval_b_true;
1328 case irms_int_number:
1329 sc_xor(a->value, b->value, NULL);
1330 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1333 assert(0 && "operation not defined on mode");
1339 * bitwise left shift
1341 tarval *tarval_shl(tarval *a, tarval *b)
1343 char *temp_val = NULL;
1347 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1349 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1350 /* vector arithmetic not implemented yet */
1354 if (get_mode_modulo_shift(a->mode) != 0)
1356 temp_val = alloca(sc_get_buffer_length());
1358 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1359 sc_mod(b->value, temp_val, temp_val);
1362 temp_val = (char*)b->value;
1364 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1365 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1369 * bitwise unsigned right shift
1371 tarval *tarval_shr(tarval *a, tarval *b)
1373 char *temp_val = NULL;
1377 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1379 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1380 /* vector arithmetic not implemented yet */
1384 if (get_mode_modulo_shift(a->mode) != 0)
1386 temp_val = alloca(sc_get_buffer_length());
1388 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1389 sc_mod(b->value, temp_val, temp_val);
1392 temp_val = (char*)b->value;
1394 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1395 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1399 * bitwise signed right shift
1401 tarval *tarval_shrs(tarval *a, tarval *b)
1403 char *temp_val = NULL;
1407 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1409 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1410 /* vector arithmetic not implemented yet */
1414 if (get_mode_modulo_shift(a->mode) != 0)
1416 temp_val = alloca(sc_get_buffer_length());
1418 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1419 sc_mod(b->value, temp_val, temp_val);
1422 temp_val = (char*)b->value;
1424 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1425 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1431 tarval *tarval_rot(tarval *a, tarval *b)
1433 char *temp_val = NULL;
1437 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1439 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1440 /* vector arithmetic not implemented yet */
1444 if (get_mode_modulo_shift(a->mode) != 0)
1446 temp_val = alloca(sc_get_buffer_length());
1448 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1449 sc_mod(b->value, temp_val, temp_val);
1452 temp_val = (char*)b->value;
1454 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1455 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1459 * carry flag of the last operation
1461 int tarval_carry(void)
1463 return sc_had_carry();
1469 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1471 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1475 const tarval_mode_info *mode_info;
1476 const char *prefix, *suffix;
1480 mode_info = tv->mode->tv_priv;
1482 mode_info = &default_info;
1483 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1484 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1486 switch (get_mode_sort(tv->mode))
1488 case irms_int_number:
1489 case irms_character:
1490 switch (mode_info->mode_output) {
1493 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1497 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1503 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1506 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1508 case irms_float_number:
1509 switch (mode_info->mode_output) {
1511 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1514 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1519 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1523 case irms_reference:
1524 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1525 if (tv->value != NULL){
1526 if (len > tv->length) {
1527 memcpy(buf, tv->value, tv->length);
1528 buf[tv->length] = '\0';
1532 memcpy(buf, tv->value, len-1);
1538 return snprintf(buf, len, "void");
1540 case irms_internal_boolean:
1541 switch (mode_info->mode_output) {
1547 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1551 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1554 case irms_control_flow:
1556 case irms_auxiliary:
1557 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1565 * Output of tarvals to stdio.
1567 int tarval_printf(tarval *tv) {
1571 res = tarval_snprintf(buf, sizeof(buf), tv);
1572 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1578 char *get_tarval_bitpattern(tarval *tv)
1581 int n = get_mode_size_bits(tv->mode);
1582 int bytes = (n + 7) / 8;
1583 char *res = malloc((n + 1) * sizeof(char));
1586 for(i = 0; i < bytes; i++) {
1587 byte = get_tarval_sub_bits(tv, i);
1588 for(j = 1; j < 256; j <<= 1)
1590 res[pos++] = j & byte ? '1' : '0';
1599 * access to the bitpattern
1601 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1603 switch (get_mode_sort(tv->mode)) {
1604 case irms_int_number:
1605 case irms_character:
1606 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1608 case irms_float_number:
1609 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1617 * Specify the output options of one mode.
1619 * This functions stores the modinfo, so DO NOT DESTROY it.
1621 * Returns zero on success.
1623 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1627 mode->tv_priv = modeinfo;
1632 * Returns the output options of one mode.
1634 * This functions returns the mode info of a given mode.
1636 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1640 return mode->tv_priv;
1644 * Identifying tarvals values for algebraic simplifications.
1647 * - TV_CLASSIFY_NULL for additive neutral,
1648 * - TV_CLASSIFY_ONE for multiplicative neutral,
1649 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1650 * - TV_CLASSIFY_OTHER else
1652 tarval_classification_t classify_tarval(tarval *tv)
1655 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1657 if (tv == get_mode_null(tv->mode))
1658 return TV_CLASSIFY_NULL;
1659 else if (tv == get_mode_one(tv->mode))
1660 return TV_CLASSIFY_ONE;
1661 else if ((get_mode_sort(tv->mode) == irms_int_number)
1662 && (tv == new_tarval_from_long(-1, tv->mode)))
1663 return TV_CLASSIFY_ALL_ONE;
1665 return TV_CLASSIFY_OTHER;
1669 * Sets the overflow mode for integer operations.
1671 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1672 int_overflow_mode = ov_mode;
1676 * Get the overflow mode for integer operations.
1678 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1679 return int_overflow_mode;
1683 * default mode_info for output as HEX
1685 static const tarval_mode_info hex_output = {
1692 * default mode_info for output as reference
1694 static const tarval_mode_info reference_output = {
1702 * Initialization of the tarval module: called before init_mode()
1704 void init_tarval_1(void)
1707 /* initialize the sets holding the tarvals with a comparison function and
1708 * an initial size, which is the expected number of constants */
1709 tarvals = new_set(memcmp, N_CONSTANTS);
1710 values = new_set(memcmp, N_CONSTANTS);
1711 /* init strcalc with precision of 68 to support floating point values with 64
1712 * bit mantissa (needs extra bits for rounding and overflow) */
1718 * Initialization of the tarval module: called after init_mode()
1720 void init_tarval_2(void)
1724 tarval_bad->mode = mode_BAD;
1725 tarval_undefined->mode = mode_ANY;
1726 tarval_b_true->mode = mode_b;
1727 tarval_b_false->mode = mode_b;
1728 tarval_P_void->mode = mode_P;
1731 * assign output modes that are compatible with the
1732 * old implementation: Hex output
1734 set_tarval_mode_output_option(mode_U, &hex_output);
1735 set_tarval_mode_output_option(mode_C, &hex_output);
1736 set_tarval_mode_output_option(mode_Bs, &hex_output);
1737 set_tarval_mode_output_option(mode_Bu, &hex_output);
1738 set_tarval_mode_output_option(mode_Hs, &hex_output);
1739 set_tarval_mode_output_option(mode_Hu, &hex_output);
1740 set_tarval_mode_output_option(mode_Is, &hex_output);
1741 set_tarval_mode_output_option(mode_Iu, &hex_output);
1742 set_tarval_mode_output_option(mode_Ls, &hex_output);
1743 set_tarval_mode_output_option(mode_Lu, &hex_output);
1744 set_tarval_mode_output_option(mode_P, &reference_output);
1747 /* free all memory occupied by tarval. */
1748 void finish_tarval(void) {
1751 del_set(tarvals); tarvals = NULL;
1752 del_set(values); values = NULL;
1755 /****************************************************************************
1757 ****************************************************************************/