3 * File name: ir/tv/tv.c
4 * Purpose: Representation of and static computations on target machine
10 * Copyright: (c) 2003 Universität Karlsruhe
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * Values are stored in a format depending upon chosen arithmetic
16 * module. Default uses strcalc and fltcalc.
20 /* This implementation assumes:
21 * - target has IEEE-754 floating-point arithmetic. */
29 #include <assert.h> /* assertions */
30 #include <stdlib.h> /* atoi() */
32 # include <string.h> /* nice things for strings */
35 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
48 #include "set.h" /* to store tarvals in */
49 #include "entity_t.h" /* needed to store pointers to entities */
51 #include "irnode.h" /* defines boolean return values (pnc_number)*/
56 /** Size of hash tables. Should correspond to average number of distinct constant
58 #define N_CONSTANTS 2048
60 /* get the integer overflow mode */
61 #define GET_OVERFLOW_MODE() int_overflow_mode
63 /* unused, float to int doesn't work yet */
64 enum float_to_int_mode {
69 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
71 #define SWITCH_NOINFINITY 0
72 #define SWITCH_NODENORMALS 0
74 /****************************************************************************
75 * local definitions and macros
76 ****************************************************************************/
78 # define TARVAL_VERIFY(a) tarval_verify((a))
80 # define TARVAL_VERIFY(a) ((void)0)
83 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
84 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
86 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
87 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
89 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
91 static long long count = 0;
92 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
94 # define ANNOUNCE() ((void)0)
96 /****************************************************************************
98 ****************************************************************************/
99 static struct set *tarvals; /* container for tarval structs */
100 static struct set *values; /* container for values */
101 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
103 /****************************************************************************
105 ****************************************************************************/
107 static int hash_val(const void *value, unsigned int length);
108 static int hash_tv(tarval *tv);
109 static void _fail_verify(tarval *tv, const char* file, int line)
111 /* print a memory image of the tarval and throw an assertion */
113 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
115 printf("%s:%d: Invalid tarval (null)", file, line);
119 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
122 INLINE static void tarval_verify(tarval *tv)
128 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
129 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
131 if (!FIND_TARVAL(tv)) fail_verify(tv);
132 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
138 static int hash_tv(tarval *tv)
140 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
143 static int hash_val(const void *value, unsigned int length)
146 unsigned int hash = 0;
148 /* scramble the byte - array */
149 for (i = 0; i < length; i++)
151 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
152 hash += (hash << 11) ^ (hash >> 17);
158 /** finds tarval with value/mode or creates new tarval */
159 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
167 /* if there already is such a value, it is returned, else value
168 * is copied into the set */
169 tv.value = INSERT_VALUE(value, length);
173 /* if there is such a tarval, it is returned, else tv is copied
175 return (tarval *)INSERT_TARVAL(&tv);
181 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
183 switch (get_mode_sort(mode))
185 case irms_int_number:
186 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
187 switch (GET_OVERFLOW_MODE()) {
188 case TV_OVERFLOW_SATURATE:
189 return get_mode_max(mode);
190 case TV_OVERFLOW_WRAP:
192 char *temp = alloca(sc_get_buffer_length());
193 char *diff = alloca(sc_get_buffer_length());
194 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
195 sc_val_from_ulong(1, temp);
196 sc_add(diff, temp, diff);
197 sc_sub(value, diff, temp);
198 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
199 sc_sub(temp, diff, temp);
200 return get_tarval(temp, length, mode);
202 case TV_OVERFLOW_BAD:
205 return get_tarval(value, length, mode);
208 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
209 switch (GET_OVERFLOW_MODE()) {
210 case TV_OVERFLOW_SATURATE:
211 return get_mode_min(mode);
212 case TV_OVERFLOW_WRAP:
214 char *temp = alloca(sc_get_buffer_length());
215 char *diff = alloca(sc_get_buffer_length());
216 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
217 sc_val_from_ulong(1, temp);
218 sc_add(diff, temp, diff);
219 sc_add(value, diff, temp);
220 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
221 sc_add(temp, diff, temp);
222 return get_tarval(temp, length, mode);
224 case TV_OVERFLOW_BAD:
227 return get_tarval(value, length, mode);
232 case irms_float_number:
233 if (SWITCH_NOINFINITY && fc_is_inf(value))
235 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
238 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
240 return get_mode_null(mode);
246 return get_tarval(value, length, mode);
251 * public variables declared in tv.h
253 static tarval reserved_tv[5];
255 tarval *tarval_bad = &reserved_tv[0];
256 tarval *tarval_undefined = &reserved_tv[1];
257 tarval *tarval_b_false = &reserved_tv[2];
258 tarval *tarval_b_true = &reserved_tv[3];
259 tarval *tarval_P_void = &reserved_tv[4];
262 * public functions declared in tv.h
266 * Constructors =============================================================
268 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
275 switch (get_mode_sort(mode))
277 case irms_control_flow:
283 case irms_internal_boolean:
284 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
285 if (strcasecmp(str, "true")) return tarval_b_true;
286 else if (strcasecmp(str, "false")) return tarval_b_true;
288 /* XXX This is C semantics */
289 return atoi(str) ? tarval_b_true : tarval_b_false;
291 case irms_float_number:
292 switch(get_mode_size_bits(mode)) {
294 fc_val_from_str(str, len, 8, 23, NULL);
297 fc_val_from_str(str, len, 11, 52, NULL);
300 fc_val_from_str(str, len, 15, 64, NULL);
303 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
305 case irms_int_number:
307 sc_val_from_str(str, len, NULL);
308 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
311 return get_tarval(str, len, mode);
314 assert(0); /* can't be reached, can it? */
319 * helper function, create a tarval from long
321 tarval *new_tarval_from_long(long l, ir_mode *mode)
326 switch(get_mode_sort(mode))
328 case irms_internal_boolean:
329 /* XXX C semantics ! */
330 return l ? tarval_b_true : tarval_b_false ;
332 case irms_int_number:
334 sc_val_from_long(l, NULL);
335 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
337 case irms_float_number:
338 return new_tarval_from_double((long double)l, mode);
341 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
344 assert(0 && "unsupported mode sort");
349 /* returns non-zero if can be converted to long */
350 int tarval_is_long(tarval *tv)
352 mode_sort sort = get_mode_sort(tv->mode);
355 if (sort != irms_int_number && sort != irms_character) return 0;
357 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
359 /* the value might be too big to fit in a long */
360 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
361 if (sc_comp(sc_get_buffer(), tv->value) == -1)
363 /* really doesn't fit */
370 /* this might overflow the machine's long, so use only with small values */
371 long get_tarval_long(tarval* tv)
374 assert(tarval_is_long(tv) && "tarval too big to fit in long");
376 return sc_val_to_long(tv->value);
379 tarval *new_tarval_from_double(long double d, ir_mode *mode)
382 assert(mode && (get_mode_sort(mode) == irms_float_number));
384 switch (get_mode_size_bits(mode)) {
386 fc_val_from_float(d, 8, 23, NULL);
389 fc_val_from_float(d, 11, 52, NULL);
392 fc_val_from_float(d, 15, 64, NULL);
395 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
398 /* returns non-zero if can be converted to double */
399 int tarval_is_double(tarval *tv)
404 return (get_mode_sort(tv->mode) == irms_float_number);
407 long double get_tarval_double(tarval *tv)
410 assert(tarval_is_double(tv));
412 return fc_val_to_float(tv->value);
417 * Access routines for tarval fields ========================================
420 /* get the mode of the tarval */
421 ir_mode *(get_tarval_mode)(const tarval *tv) {
422 return _get_tarval_mode(tv);
426 * Special value query functions ============================================
428 * These functions calculate and return a tarval representing the requested
430 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
431 * functions, but these are stored on initialization of the irmode module and
432 * therefore the irmode functions should be prefered to the functions below.
435 tarval *(get_tarval_bad)(void) {
436 return _get_tarval_bad();
439 tarval *(get_tarval_undefined)(void) {
440 return _get_tarval_undefined();
443 tarval *(get_tarval_b_false)(void) {
444 return _get_tarval_b_false();
447 tarval *(get_tarval_b_true)(void) {
448 return _get_tarval_b_true();
451 tarval *(get_tarval_P_void)(void) {
452 return _get_tarval_P_void();
455 tarval *get_tarval_max(ir_mode *mode)
460 if (get_mode_n_vector_elems(mode) > 1) {
461 /* vector arithmetic not implemented yet */
465 switch(get_mode_sort(mode))
468 case irms_control_flow:
474 case irms_internal_boolean:
475 return tarval_b_true;
477 case irms_float_number:
478 switch(get_mode_size_bits(mode))
481 fc_get_max(8, 23, NULL);
484 fc_get_max(11, 52, NULL);
487 fc_get_max(15, 64, NULL);
490 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
492 case irms_int_number:
494 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
495 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
500 tarval *get_tarval_min(ir_mode *mode)
505 if (get_mode_n_vector_elems(mode) > 1) {
506 /* vector arithmetic not implemented yet */
510 switch(get_mode_sort(mode))
513 case irms_control_flow:
519 case irms_internal_boolean:
520 return tarval_b_false;
522 case irms_float_number:
523 switch(get_mode_size_bits(mode))
526 fc_get_min(8, 23, NULL);
529 fc_get_min(11, 52, NULL);
532 fc_get_min(15, 64, NULL);
535 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
537 case irms_int_number:
539 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
540 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
545 tarval *get_tarval_null(ir_mode *mode)
550 if (get_mode_n_vector_elems(mode) > 1) {
551 /* vector arithmetic not implemented yet */
555 switch(get_mode_sort(mode))
557 case irms_control_flow:
560 case irms_internal_boolean:
564 case irms_float_number:
565 return new_tarval_from_double(0.0, mode);
567 case irms_int_number:
569 return new_tarval_from_long(0l, mode);
572 return tarval_P_void;
577 tarval *get_tarval_one(ir_mode *mode)
582 if (get_mode_n_vector_elems(mode) > 1) {
583 /* vector arithmetic not implemented yet */
587 switch(get_mode_sort(mode))
589 case irms_control_flow:
592 case irms_internal_boolean:
597 case irms_float_number:
598 return new_tarval_from_double(1.0, mode);
600 case irms_int_number:
602 return new_tarval_from_long(1l, mode);
608 tarval *get_tarval_minus_one(ir_mode *mode)
613 if (get_mode_n_vector_elems(mode) > 1) {
614 /* vector arithmetic not implemented yet */
618 switch(get_mode_sort(mode))
620 case irms_control_flow:
623 case irms_internal_boolean:
628 case irms_float_number:
629 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
631 case irms_int_number:
633 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
638 tarval *get_tarval_nan(ir_mode *mode)
643 if (get_mode_n_vector_elems(mode) > 1) {
644 /* vector arithmetic not implemented yet */
648 if (get_mode_sort(mode) == irms_float_number) {
649 switch(get_mode_size_bits(mode))
652 fc_get_qnan(8, 23, NULL);
655 fc_get_qnan(11, 52, NULL);
658 fc_get_qnan(15, 64, NULL);
661 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
664 assert(0 && "tarval is not floating point");
669 tarval *get_tarval_plus_inf(ir_mode *mode)
674 if (get_mode_n_vector_elems(mode) > 1) {
675 /* vector arithmetic not implemented yet */
679 if (get_mode_sort(mode) == irms_float_number) {
680 switch(get_mode_size_bits(mode))
683 fc_get_plusinf(8, 23, NULL);
686 fc_get_plusinf(11, 52, NULL);
689 fc_get_plusinf(15, 64, NULL);
692 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
695 assert(0 && "tarval is not floating point");
700 tarval *get_tarval_minus_inf(ir_mode *mode)
705 if (get_mode_n_vector_elems(mode) > 1) {
706 /* vector arithmetic not implemented yet */
710 if (get_mode_sort(mode) == irms_float_number) {
711 switch(get_mode_size_bits(mode))
714 fc_get_minusinf(8, 23, NULL);
717 fc_get_minusinf(11, 52, NULL);
720 fc_get_minusinf(15, 64, NULL);
723 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
726 assert(0 && "tarval is not floating point");
732 * Arithmethic operations on tarvals ========================================
736 * test if negative number, 1 means 'yes'
738 int tarval_is_negative(tarval *a)
743 if (get_mode_n_vector_elems(a->mode) > 1) {
744 /* vector arithmetic not implemented yet */
745 assert(0 && "tarval_is_negative is not allowed for vector modes");
749 switch (get_mode_sort(a->mode))
751 case irms_int_number:
752 if (!mode_is_signed(a->mode)) return 0;
754 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
756 case irms_float_number:
757 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
760 assert(0 && "not implemented");
766 * test if null, 1 means 'yes'
768 int tarval_is_null(tarval *a)
770 ir_mode *m = get_tarval_mode(a);
772 return a == get_tarval_null(m);
776 * test if one, 1 means 'yes'
778 int tarval_is_one(tarval *a)
780 ir_mode *m = get_tarval_mode(a);
782 return a == get_tarval_one(m);
788 pn_Cmp tarval_cmp(tarval *a, tarval *b)
794 if (a == tarval_bad || b == tarval_bad) {
795 assert(0 && "Comparison with tarval_bad");
799 if (a == tarval_undefined || b == tarval_undefined)
802 if (a->mode != b->mode)
805 if (get_mode_n_vector_elems(a->mode) > 1) {
806 /* vector arithmetic not implemented yet */
807 assert(0 && "cmp not implemented for vector modes");
810 /* Here the two tarvals are unequal and of the same mode */
811 switch (get_mode_sort(a->mode))
813 case irms_control_flow:
821 case irms_float_number:
823 * BEWARE: we cannot compare a == b here, because
824 * a NaN is always Unordered to any other value, even to itself!
826 switch (fc_comp(a->value, b->value)) {
827 case -1: return pn_Cmp_Lt;
828 case 0: return pn_Cmp_Eq;
829 case 1: return pn_Cmp_Gt;
830 case 2: return pn_Cmp_Uo;
831 default: return pn_Cmp_False;
833 case irms_int_number:
837 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
839 case irms_internal_boolean:
842 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
848 * convert to other mode
850 tarval *tarval_convert_to(tarval *src, ir_mode *m)
858 if (src->mode == m) return src;
860 if (get_mode_n_vector_elems(src->mode) > 1) {
861 /* vector arithmetic not implemented yet */
865 switch (get_mode_sort(src->mode))
867 case irms_control_flow:
872 /* cast float to something */
873 case irms_float_number:
874 switch (get_mode_sort(m)) {
875 case irms_float_number:
876 switch (get_mode_size_bits(m))
879 fc_cast(src->value, 8, 23, NULL);
882 fc_cast(src->value, 11, 52, NULL);
885 fc_cast(src->value, 15, 64, NULL);
890 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
892 case irms_int_number:
893 switch (GET_FLOAT_TO_INT_MODE())
896 fc_int(src->value, NULL);
899 fc_rnd(src->value, NULL);
905 /* FIXME: floating point unit can't produce a value in integer
907 * an intermediate representation is needed here first. */
908 /* return get_tarval(); */
912 /* the rest can't be converted */
917 /* cast int to something */
918 case irms_int_number:
919 switch (get_mode_sort(m)) {
920 case irms_int_number:
922 return get_tarval_overflow(src->value, src->length, m);
924 case irms_internal_boolean:
925 /* XXX C semantics */
926 if (src == get_mode_null(src->mode)) return tarval_b_false;
927 else return tarval_b_true;
929 case irms_float_number:
930 /* XXX floating point unit does not understand internal integer
931 * representation, convert to string first, then create float from
933 buffer = alloca(100);
934 /* decimal string representation because hexadecimal output is
935 * interpreted unsigned by fc_val_from_str, so this is a HACK */
936 snprintf(buffer, 100, "%s",
937 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
938 switch (get_mode_size_bits(m))
941 fc_val_from_str(buffer, 0, 8, 23, NULL);
944 fc_val_from_str(buffer, 0, 11, 52, NULL);
947 fc_val_from_str(buffer, 0, 15, 64, NULL);
950 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
954 /* allow 0 to be casted */
955 if (src == get_mode_null(src->mode))
956 return get_mode_null(m);
964 case irms_internal_boolean:
965 switch (get_mode_sort(m))
967 case irms_int_number:
968 if (src == tarval_b_true) return get_mode_one(m);
969 else return get_mode_null(m);
988 tarval *tarval_not(tarval *a)
995 /* works for vector mode without changes */
997 switch (get_mode_sort(a->mode))
999 case irms_int_number:
1000 buffer = alloca(sc_get_buffer_length());
1001 sc_not(a->value, buffer);
1002 return get_tarval(buffer, a->length, a->mode);
1004 case irms_internal_boolean:
1005 if (a == tarval_b_true)
1006 return tarval_b_false;
1007 if (a == tarval_b_false)
1008 return tarval_b_true;
1012 assert(0 && "bitwise negation is only allowed for integer and boolean");
1018 * arithmetic negation
1020 tarval *tarval_neg(tarval *a)
1026 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1028 /* note: negation is allowed even for unsigned modes. */
1030 if (get_mode_n_vector_elems(a->mode) > 1) {
1031 /* vector arithmetic not implemented yet */
1035 switch (get_mode_sort(a->mode))
1037 case irms_int_number:
1038 buffer = alloca(sc_get_buffer_length());
1039 sc_neg(a->value, buffer);
1040 return get_tarval_overflow(buffer, a->length, a->mode);
1042 case irms_float_number:
1043 fc_neg(a->value, NULL);
1044 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1054 tarval *tarval_add(tarval *a, tarval *b)
1061 assert(a->mode == b->mode);
1063 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1064 /* vector arithmetic not implemented yet */
1068 switch (get_mode_sort(a->mode))
1070 case irms_character:
1071 case irms_int_number:
1072 /* modes of a,b are equal, so result has mode of a as this might be the character */
1073 buffer = alloca(sc_get_buffer_length());
1074 sc_add(a->value, b->value, buffer);
1075 return get_tarval_overflow(buffer, a->length, a->mode);
1077 case irms_float_number:
1078 fc_add(a->value, b->value, NULL);
1079 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1089 tarval *tarval_sub(tarval *a, tarval *b)
1096 assert(a->mode == b->mode);
1098 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1099 /* vector arithmetic not implemented yet */
1102 switch (get_mode_sort(a->mode))
1104 case irms_character:
1105 case irms_int_number:
1106 /* modes of a,b are equal, so result has mode of a as this might be the character */
1107 buffer = alloca(sc_get_buffer_length());
1108 sc_sub(a->value, b->value, buffer);
1109 return get_tarval_overflow(buffer, a->length, a->mode);
1111 case irms_float_number:
1112 fc_sub(a->value, b->value, NULL);
1113 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1123 tarval *tarval_mul(tarval *a, tarval *b)
1130 assert(a->mode == b->mode);
1132 if (get_mode_n_vector_elems(a->mode) > 1) {
1133 /* vector arithmetic not implemented yet */
1137 switch (get_mode_sort(a->mode))
1139 case irms_int_number:
1140 /* modes of a,b are equal */
1141 buffer = alloca(sc_get_buffer_length());
1142 sc_mul(a->value, b->value, buffer);
1143 return get_tarval_overflow(buffer, a->length, a->mode);
1145 case irms_float_number:
1146 fc_mul(a->value, b->value, NULL);
1147 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1155 * floating point division
1157 tarval *tarval_quo(tarval *a, tarval *b)
1162 assert((a->mode == b->mode) && mode_is_float(a->mode));
1164 if (get_mode_n_vector_elems(a->mode) > 1) {
1165 /* vector arithmetic not implemented yet */
1169 fc_div(a->value, b->value, NULL);
1170 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1175 * overflow is impossible, but look out for division by zero
1177 tarval *tarval_div(tarval *a, tarval *b)
1182 assert((a->mode == b->mode) && mode_is_int(a->mode));
1184 if (get_mode_n_vector_elems(a->mode) > 1) {
1185 /* vector arithmetic not implemented yet */
1190 if (b == get_mode_null(b->mode)) return tarval_bad;
1191 /* modes of a,b are equal */
1192 sc_div(a->value, b->value, NULL);
1193 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1198 * overflow is impossible, but look out for division by zero
1200 tarval *tarval_mod(tarval *a, tarval *b)
1205 assert((a->mode == b->mode) && mode_is_int(a->mode));
1207 if (get_mode_n_vector_elems(a->mode) > 1) {
1208 /* vector arithmetic not implemented yet */
1213 if (b == get_mode_null(b->mode)) return tarval_bad;
1214 /* modes of a,b are equal */
1215 sc_mod(a->value, b->value, NULL);
1216 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1222 tarval *tarval_abs(tarval *a)
1228 assert(mode_is_num(a->mode));
1230 if (get_mode_n_vector_elems(a->mode) > 1) {
1231 /* vector arithmetic not implemented yet */
1235 switch (get_mode_sort(a->mode))
1237 case irms_int_number:
1238 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1240 buffer = alloca(sc_get_buffer_length());
1241 sc_neg(a->value, buffer);
1242 return get_tarval_overflow(buffer, a->length, a->mode);
1246 case irms_float_number:
1247 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1249 fc_neg(a->value, NULL);
1250 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1263 tarval *tarval_and(tarval *a, tarval *b)
1268 assert(a->mode == b->mode);
1270 /* works even for vector modes */
1272 switch(get_mode_sort(a->mode))
1274 case irms_internal_boolean:
1275 return (a == tarval_b_false) ? a : b;
1277 case irms_int_number:
1278 sc_and(a->value, b->value, NULL);
1279 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1282 assert(0 && "operation not defined on mode");
1290 tarval *tarval_or (tarval *a, tarval *b)
1295 assert(a->mode == b->mode);
1297 /* works even for vector modes */
1299 switch (get_mode_sort(a->mode))
1301 case irms_internal_boolean:
1302 return (a == tarval_b_true) ? a : b;
1304 case irms_int_number:
1305 sc_or(a->value, b->value, NULL);
1306 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1309 assert(0 && "operation not defined on mode");
1315 * bitwise exclusive or (xor)
1317 tarval *tarval_eor(tarval *a, tarval *b)
1322 assert((a->mode == b->mode));
1324 /* works even for vector modes */
1326 switch (get_mode_sort(a->mode))
1328 case irms_internal_boolean:
1329 return (a == b)? tarval_b_false : tarval_b_true;
1331 case irms_int_number:
1332 sc_xor(a->value, b->value, NULL);
1333 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1336 assert(0 && "operation not defined on mode");
1342 * bitwise left shift
1344 tarval *tarval_shl(tarval *a, tarval *b)
1346 char *temp_val = NULL;
1350 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1352 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1353 /* vector arithmetic not implemented yet */
1357 if (get_mode_modulo_shift(a->mode) != 0)
1359 temp_val = alloca(sc_get_buffer_length());
1361 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1362 sc_mod(b->value, temp_val, temp_val);
1365 temp_val = (char*)b->value;
1367 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1368 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1372 * bitwise unsigned right shift
1374 tarval *tarval_shr(tarval *a, tarval *b)
1376 char *temp_val = NULL;
1380 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1382 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1383 /* vector arithmetic not implemented yet */
1387 if (get_mode_modulo_shift(a->mode) != 0)
1389 temp_val = alloca(sc_get_buffer_length());
1391 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1392 sc_mod(b->value, temp_val, temp_val);
1395 temp_val = (char*)b->value;
1397 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1398 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1402 * bitwise signed right shift
1404 tarval *tarval_shrs(tarval *a, tarval *b)
1406 char *temp_val = NULL;
1410 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1412 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1413 /* vector arithmetic not implemented yet */
1417 if (get_mode_modulo_shift(a->mode) != 0)
1419 temp_val = alloca(sc_get_buffer_length());
1421 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1422 sc_mod(b->value, temp_val, temp_val);
1425 temp_val = (char*)b->value;
1427 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1428 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1434 tarval *tarval_rot(tarval *a, tarval *b)
1436 char *temp_val = NULL;
1440 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1442 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1443 /* vector arithmetic not implemented yet */
1447 if (get_mode_modulo_shift(a->mode) != 0)
1449 temp_val = alloca(sc_get_buffer_length());
1451 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1452 sc_mod(b->value, temp_val, temp_val);
1455 temp_val = (char*)b->value;
1457 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1458 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1462 * carry flag of the last operation
1464 int tarval_carry(void)
1466 return sc_had_carry();
1472 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1474 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1478 const tarval_mode_info *mode_info;
1479 const char *prefix, *suffix;
1483 mode_info = tv->mode->tv_priv;
1485 mode_info = &default_info;
1486 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1487 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1489 switch (get_mode_sort(tv->mode))
1491 case irms_int_number:
1492 case irms_character:
1493 switch (mode_info->mode_output) {
1496 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1500 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1506 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1509 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1511 case irms_float_number:
1512 switch (mode_info->mode_output) {
1514 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1517 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1522 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1526 case irms_reference:
1527 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1528 if (tv->value != NULL){
1529 if (len > tv->length) {
1530 memcpy(buf, tv->value, tv->length);
1531 buf[tv->length] = '\0';
1535 memcpy(buf, tv->value, len-1);
1541 return snprintf(buf, len, "void");
1543 case irms_internal_boolean:
1544 switch (mode_info->mode_output) {
1550 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1554 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1557 case irms_control_flow:
1559 case irms_auxiliary:
1560 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1568 * Output of tarvals to stdio.
1570 int tarval_printf(tarval *tv) {
1574 res = tarval_snprintf(buf, sizeof(buf), tv);
1575 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1581 char *get_tarval_bitpattern(tarval *tv)
1584 int n = get_mode_size_bits(tv->mode);
1585 int bytes = (n + 7) / 8;
1586 char *res = malloc((n + 1) * sizeof(char));
1589 for(i = 0; i < bytes; i++) {
1590 byte = get_tarval_sub_bits(tv, i);
1591 for(j = 1; j < 256; j <<= 1)
1593 res[pos++] = j & byte ? '1' : '0';
1602 * access to the bitpattern
1604 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1606 switch (get_mode_sort(tv->mode)) {
1607 case irms_int_number:
1608 case irms_character:
1609 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1611 case irms_float_number:
1612 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1620 * Specify the output options of one mode.
1622 * This functions stores the modinfo, so DO NOT DESTROY it.
1624 * Returns zero on success.
1626 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1630 mode->tv_priv = modeinfo;
1635 * Returns the output options of one mode.
1637 * This functions returns the mode info of a given mode.
1639 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1643 return mode->tv_priv;
1647 * Identifying tarvals values for algebraic simplifications.
1650 * - TV_CLASSIFY_NULL for additive neutral,
1651 * - TV_CLASSIFY_ONE for multiplicative neutral,
1652 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1653 * - TV_CLASSIFY_OTHER else
1655 tarval_classification_t classify_tarval(tarval *tv)
1658 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1660 if (tv == get_mode_null(tv->mode))
1661 return TV_CLASSIFY_NULL;
1662 else if (tv == get_mode_one(tv->mode))
1663 return TV_CLASSIFY_ONE;
1664 else if ((get_mode_sort(tv->mode) == irms_int_number)
1665 && (tv == new_tarval_from_long(-1, tv->mode)))
1666 return TV_CLASSIFY_ALL_ONE;
1668 return TV_CLASSIFY_OTHER;
1672 * Sets the overflow mode for integer operations.
1674 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1675 int_overflow_mode = ov_mode;
1679 * Get the overflow mode for integer operations.
1681 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1682 return int_overflow_mode;
1686 * default mode_info for output as HEX
1688 static const tarval_mode_info hex_output = {
1695 * default mode_info for output as reference
1697 static const tarval_mode_info reference_output = {
1705 * Initialization of the tarval module: called before init_mode()
1707 void init_tarval_1(void)
1710 /* initialize the sets holding the tarvals with a comparison function and
1711 * an initial size, which is the expected number of constants */
1712 tarvals = new_set(memcmp, N_CONSTANTS);
1713 values = new_set(memcmp, N_CONSTANTS);
1714 /* init strcalc with precision of 68 to support floating point values with 64
1715 * bit mantissa (needs extra bits for rounding and overflow) */
1721 * Initialization of the tarval module: called after init_mode()
1723 void init_tarval_2(void)
1727 tarval.bad->kind = k_tarval;
1728 tarval_bad->mode = mode_BAD;
1730 tarval_undefined->kind = k_tarval;
1731 tarval_undefined->mode = mode_ANY;
1733 tarval_b_true->kind = k_tarval;
1734 tarval_b_true->mode = mode_b;
1736 tarval_b_false->kind = k_tarval
1737 tarval_b_false->mode = mode_b;
1739 tarval_P_void->kind = k_tarval;
1740 tarval_P_void->mode = mode_P;
1743 * assign output modes that are compatible with the
1744 * old implementation: Hex output
1746 set_tarval_mode_output_option(mode_U, &hex_output);
1747 set_tarval_mode_output_option(mode_C, &hex_output);
1748 set_tarval_mode_output_option(mode_Bs, &hex_output);
1749 set_tarval_mode_output_option(mode_Bu, &hex_output);
1750 set_tarval_mode_output_option(mode_Hs, &hex_output);
1751 set_tarval_mode_output_option(mode_Hu, &hex_output);
1752 set_tarval_mode_output_option(mode_Is, &hex_output);
1753 set_tarval_mode_output_option(mode_Iu, &hex_output);
1754 set_tarval_mode_output_option(mode_Ls, &hex_output);
1755 set_tarval_mode_output_option(mode_Lu, &hex_output);
1756 set_tarval_mode_output_option(mode_P, &reference_output);
1759 /* free all memory occupied by tarval. */
1760 void finish_tarval(void) {
1763 del_set(tarvals); tarvals = NULL;
1764 del_set(values); values = NULL;
1767 /****************************************************************************
1769 ****************************************************************************/