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)*/
55 #include "firm_common.h"
57 /** Size of hash tables. Should correspond to average number of distinct constant
59 #define N_CONSTANTS 2048
61 /* get the integer overflow mode */
62 #define GET_OVERFLOW_MODE() int_overflow_mode
64 /* unused, float to int doesn't work yet */
65 enum float_to_int_mode {
70 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
72 #define SWITCH_NOINFINITY 0
73 #define SWITCH_NODENORMALS 0
75 /****************************************************************************
76 * local definitions and macros
77 ****************************************************************************/
79 # define TARVAL_VERIFY(a) tarval_verify((a))
81 # define TARVAL_VERIFY(a) ((void)0)
84 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
85 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
87 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
88 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
90 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
92 static long long count = 0;
93 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
95 # define ANNOUNCE() ((void)0)
97 /****************************************************************************
99 ****************************************************************************/
100 static struct set *tarvals; /* container for tarval structs */
101 static struct set *values; /* container for values */
102 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
104 /****************************************************************************
106 ****************************************************************************/
108 static int hash_val(const void *value, unsigned int length);
109 static int hash_tv(tarval *tv);
110 static void _fail_verify(tarval *tv, const char* file, int line)
112 /* print a memory image of the tarval and throw an assertion */
114 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
116 printf("%s:%d: Invalid tarval (null)", file, line);
120 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
123 INLINE static void tarval_verify(tarval *tv)
129 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
130 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
132 if (!FIND_TARVAL(tv)) fail_verify(tv);
133 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
139 static int hash_tv(tarval *tv)
141 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
144 static int hash_val(const void *value, unsigned int length)
147 unsigned int hash = 0;
149 /* scramble the byte - array */
150 for (i = 0; i < length; i++)
152 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
153 hash += (hash << 11) ^ (hash >> 17);
159 /** finds tarval with value/mode or creates new tarval */
160 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
168 /* if there already is such a value, it is returned, else value
169 * is copied into the set */
170 tv.value = INSERT_VALUE(value, length);
174 /* if there is such a tarval, it is returned, else tv is copied
176 return (tarval *)INSERT_TARVAL(&tv);
182 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
184 switch (get_mode_sort(mode))
186 case irms_int_number:
187 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
188 switch (GET_OVERFLOW_MODE()) {
189 case TV_OVERFLOW_SATURATE:
190 return get_mode_max(mode);
191 case TV_OVERFLOW_WRAP:
193 char *temp = alloca(sc_get_buffer_length());
194 char *diff = alloca(sc_get_buffer_length());
195 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
196 sc_val_from_ulong(1, temp);
197 sc_add(diff, temp, diff);
198 sc_sub(value, diff, temp);
199 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
200 sc_sub(temp, diff, temp);
201 return get_tarval(temp, length, mode);
203 case TV_OVERFLOW_BAD:
206 return get_tarval(value, length, mode);
209 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
210 switch (GET_OVERFLOW_MODE()) {
211 case TV_OVERFLOW_SATURATE:
212 return get_mode_min(mode);
213 case TV_OVERFLOW_WRAP:
215 char *temp = alloca(sc_get_buffer_length());
216 char *diff = alloca(sc_get_buffer_length());
217 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
218 sc_val_from_ulong(1, temp);
219 sc_add(diff, temp, diff);
220 sc_add(value, diff, temp);
221 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
222 sc_add(temp, diff, temp);
223 return get_tarval(temp, length, mode);
225 case TV_OVERFLOW_BAD:
228 return get_tarval(value, length, mode);
233 case irms_float_number:
234 if (SWITCH_NOINFINITY && fc_is_inf(value))
236 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
239 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
241 return get_mode_null(mode);
248 return get_tarval(value, length, mode);
253 * public variables declared in tv.h
255 static tarval reserved_tv[4];
257 tarval *tarval_bad = &reserved_tv[0];
258 tarval *tarval_undefined = &reserved_tv[1];
259 tarval *tarval_b_false = &reserved_tv[2];
260 tarval *tarval_b_true = &reserved_tv[3];
263 * public functions declared in tv.h
267 * Constructors =============================================================
269 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
276 switch (get_mode_sort(mode))
278 case irms_control_flow:
284 case irms_internal_boolean:
285 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
286 if (strcasecmp(str, "true")) return tarval_b_true;
287 else if (strcasecmp(str, "false")) return tarval_b_true;
289 /* XXX This is C semantics */
290 return atoi(str) ? tarval_b_true : tarval_b_false;
292 case irms_float_number:
293 switch(get_mode_size_bits(mode)) {
295 fc_val_from_str(str, len, 8, 23, NULL);
298 fc_val_from_str(str, len, 11, 52, NULL);
301 fc_val_from_str(str, len, 15, 64, NULL);
304 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
307 /* same as integer modes */
308 case irms_int_number:
310 sc_val_from_str(str, len, NULL, mode);
311 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), 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 ;
333 /* same as integer modes */
334 case irms_int_number:
336 sc_val_from_long(l, NULL);
337 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
339 case irms_float_number:
340 return new_tarval_from_double((long double)l, mode);
343 assert(0 && "unsupported mode sort");
348 /* returns non-zero if can be converted to long */
349 int tarval_is_long(tarval *tv)
351 mode_sort sort = get_mode_sort(tv->mode);
354 if (sort != irms_int_number && sort != irms_character) return 0;
356 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
358 /* the value might be too big to fit in a long */
359 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
360 if (sc_comp(sc_get_buffer(), tv->value) == -1)
362 /* really doesn't fit */
369 /* this might overflow the machine's long, so use only with small values */
370 long get_tarval_long(tarval* tv)
373 assert(tarval_is_long(tv) && "tarval too big to fit in long");
375 return sc_val_to_long(tv->value);
378 tarval *new_tarval_from_double(long double d, ir_mode *mode)
381 assert(mode && (get_mode_sort(mode) == irms_float_number));
383 switch (get_mode_size_bits(mode)) {
385 fc_val_from_float(d, 8, 23, NULL);
388 fc_val_from_float(d, 11, 52, NULL);
391 fc_val_from_float(d, 15, 64, NULL);
394 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
397 /* returns non-zero if can be converted to double */
398 int tarval_is_double(tarval *tv)
403 return (get_mode_sort(tv->mode) == irms_float_number);
406 long double get_tarval_double(tarval *tv)
409 assert(tarval_is_double(tv));
411 return fc_val_to_float(tv->value);
416 * Access routines for tarval fields ========================================
419 /* get the mode of the tarval */
420 ir_mode *(get_tarval_mode)(const tarval *tv) {
421 return _get_tarval_mode(tv);
425 * Special value query functions ============================================
427 * These functions calculate and return a tarval representing the requested
429 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
430 * functions, but these are stored on initialization of the irmode module and
431 * therefore the irmode functions should be prefered to the functions below.
434 tarval *(get_tarval_bad)(void) {
435 return _get_tarval_bad();
438 tarval *(get_tarval_undefined)(void) {
439 return _get_tarval_undefined();
442 tarval *(get_tarval_b_false)(void) {
443 return _get_tarval_b_false();
446 tarval *(get_tarval_b_true)(void) {
447 return _get_tarval_b_true();
450 tarval *get_tarval_max(ir_mode *mode)
455 if (get_mode_n_vector_elems(mode) > 1) {
456 /* vector arithmetic not implemented yet */
460 switch(get_mode_sort(mode))
463 case irms_control_flow:
469 case irms_internal_boolean:
470 return tarval_b_true;
472 case irms_float_number:
473 switch(get_mode_size_bits(mode))
476 fc_get_max(8, 23, NULL);
479 fc_get_max(11, 52, NULL);
482 fc_get_max(15, 64, NULL);
485 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
487 case irms_int_number:
489 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
490 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
495 tarval *get_tarval_min(ir_mode *mode)
500 if (get_mode_n_vector_elems(mode) > 1) {
501 /* vector arithmetic not implemented yet */
505 switch(get_mode_sort(mode))
508 case irms_control_flow:
514 case irms_internal_boolean:
515 return tarval_b_false;
517 case irms_float_number:
518 switch(get_mode_size_bits(mode))
521 fc_get_min(8, 23, NULL);
524 fc_get_min(11, 52, NULL);
527 fc_get_min(15, 64, NULL);
530 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
532 case irms_int_number:
534 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
535 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
540 static long _null_value;
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 new_tarval_from_long(_null_value, mode);
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);
950 /* allow 0 to be casted */
951 if (src == get_mode_null(src->mode))
952 return get_mode_null(m);
960 case irms_internal_boolean:
961 switch (get_mode_sort(m))
963 case irms_int_number:
964 if (src == tarval_b_true) return get_mode_one(m);
965 else return get_mode_null(m);
984 tarval *tarval_not(tarval *a)
991 /* works for vector mode without changes */
993 switch (get_mode_sort(a->mode))
995 case irms_int_number:
996 buffer = alloca(sc_get_buffer_length());
997 sc_not(a->value, buffer);
998 return get_tarval(buffer, a->length, a->mode);
1000 case irms_internal_boolean:
1001 if (a == tarval_b_true)
1002 return tarval_b_false;
1003 if (a == tarval_b_false)
1004 return tarval_b_true;
1008 assert(0 && "bitwise negation is only allowed for integer and boolean");
1014 * arithmetic negation
1016 tarval *tarval_neg(tarval *a)
1022 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1024 /* note: negation is allowed even for unsigned modes. */
1026 if (get_mode_n_vector_elems(a->mode) > 1) {
1027 /* vector arithmetic not implemented yet */
1031 switch (get_mode_sort(a->mode))
1033 case irms_int_number:
1034 buffer = alloca(sc_get_buffer_length());
1035 sc_neg(a->value, buffer);
1036 return get_tarval_overflow(buffer, a->length, a->mode);
1038 case irms_float_number:
1039 fc_neg(a->value, NULL);
1040 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1050 tarval *tarval_add(tarval *a, tarval *b)
1057 assert(a->mode == b->mode);
1059 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1060 /* vector arithmetic not implemented yet */
1064 switch (get_mode_sort(a->mode))
1066 case irms_character:
1067 case irms_int_number:
1068 /* modes of a,b are equal, so result has mode of a as this might be the character */
1069 buffer = alloca(sc_get_buffer_length());
1070 sc_add(a->value, b->value, buffer);
1071 return get_tarval_overflow(buffer, a->length, a->mode);
1073 case irms_float_number:
1074 fc_add(a->value, b->value, NULL);
1075 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1085 tarval *tarval_sub(tarval *a, tarval *b)
1092 assert(a->mode == b->mode);
1094 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1095 /* vector arithmetic not implemented yet */
1098 switch (get_mode_sort(a->mode))
1100 case irms_character:
1101 case irms_int_number:
1102 /* modes of a,b are equal, so result has mode of a as this might be the character */
1103 buffer = alloca(sc_get_buffer_length());
1104 sc_sub(a->value, b->value, buffer);
1105 return get_tarval_overflow(buffer, a->length, a->mode);
1107 case irms_float_number:
1108 fc_sub(a->value, b->value, NULL);
1109 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1119 tarval *tarval_mul(tarval *a, tarval *b)
1126 assert(a->mode == b->mode);
1128 if (get_mode_n_vector_elems(a->mode) > 1) {
1129 /* vector arithmetic not implemented yet */
1133 switch (get_mode_sort(a->mode))
1135 case irms_int_number:
1136 /* modes of a,b are equal */
1137 buffer = alloca(sc_get_buffer_length());
1138 sc_mul(a->value, b->value, buffer);
1139 return get_tarval_overflow(buffer, a->length, a->mode);
1141 case irms_float_number:
1142 fc_mul(a->value, b->value, NULL);
1143 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1151 * floating point division
1153 tarval *tarval_quo(tarval *a, tarval *b)
1158 assert((a->mode == b->mode) && mode_is_float(a->mode));
1160 if (get_mode_n_vector_elems(a->mode) > 1) {
1161 /* vector arithmetic not implemented yet */
1165 fc_div(a->value, b->value, NULL);
1166 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1171 * overflow is impossible, but look out for division by zero
1173 tarval *tarval_div(tarval *a, tarval *b)
1178 assert((a->mode == b->mode) && mode_is_int(a->mode));
1180 if (get_mode_n_vector_elems(a->mode) > 1) {
1181 /* vector arithmetic not implemented yet */
1186 if (b == get_mode_null(b->mode)) return tarval_bad;
1187 /* modes of a,b are equal */
1188 sc_div(a->value, b->value, NULL);
1189 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1194 * overflow is impossible, but look out for division by zero
1196 tarval *tarval_mod(tarval *a, tarval *b)
1201 assert((a->mode == b->mode) && mode_is_int(a->mode));
1203 if (get_mode_n_vector_elems(a->mode) > 1) {
1204 /* vector arithmetic not implemented yet */
1209 if (b == get_mode_null(b->mode)) return tarval_bad;
1210 /* modes of a,b are equal */
1211 sc_mod(a->value, b->value, NULL);
1212 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1218 tarval *tarval_abs(tarval *a)
1224 assert(mode_is_num(a->mode));
1226 if (get_mode_n_vector_elems(a->mode) > 1) {
1227 /* vector arithmetic not implemented yet */
1231 switch (get_mode_sort(a->mode))
1233 case irms_int_number:
1234 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1236 buffer = alloca(sc_get_buffer_length());
1237 sc_neg(a->value, buffer);
1238 return get_tarval_overflow(buffer, a->length, a->mode);
1242 case irms_float_number:
1243 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1245 fc_neg(a->value, NULL);
1246 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1259 tarval *tarval_and(tarval *a, tarval *b)
1264 assert(a->mode == b->mode);
1266 /* works even for vector modes */
1268 switch(get_mode_sort(a->mode))
1270 case irms_internal_boolean:
1271 return (a == tarval_b_false) ? a : b;
1273 case irms_int_number:
1274 sc_and(a->value, b->value, NULL);
1275 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1278 assert(0 && "operation not defined on mode");
1286 tarval *tarval_or (tarval *a, tarval *b)
1291 assert(a->mode == b->mode);
1293 /* works even for vector modes */
1295 switch (get_mode_sort(a->mode))
1297 case irms_internal_boolean:
1298 return (a == tarval_b_true) ? a : b;
1300 case irms_int_number:
1301 sc_or(a->value, b->value, NULL);
1302 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1305 assert(0 && "operation not defined on mode");
1311 * bitwise exclusive or (xor)
1313 tarval *tarval_eor(tarval *a, tarval *b)
1318 assert((a->mode == b->mode));
1320 /* works even for vector modes */
1322 switch (get_mode_sort(a->mode))
1324 case irms_internal_boolean:
1325 return (a == b)? tarval_b_false : tarval_b_true;
1327 case irms_int_number:
1328 sc_xor(a->value, b->value, NULL);
1329 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1332 assert(0 && "operation not defined on mode");
1338 * bitwise left shift
1340 tarval *tarval_shl(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_shl(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 * bitwise unsigned right shift
1370 tarval *tarval_shr(tarval *a, tarval *b)
1372 char *temp_val = NULL;
1376 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1378 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1379 /* vector arithmetic not implemented yet */
1383 if (get_mode_modulo_shift(a->mode) != 0)
1385 temp_val = alloca(sc_get_buffer_length());
1387 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1388 sc_mod(b->value, temp_val, temp_val);
1391 temp_val = (char*)b->value;
1393 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1394 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1398 * bitwise signed right shift
1400 tarval *tarval_shrs(tarval *a, tarval *b)
1402 char *temp_val = NULL;
1406 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1408 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1409 /* vector arithmetic not implemented yet */
1413 if (get_mode_modulo_shift(a->mode) != 0)
1415 temp_val = alloca(sc_get_buffer_length());
1417 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1418 sc_mod(b->value, temp_val, temp_val);
1421 temp_val = (char*)b->value;
1423 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1424 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1430 tarval *tarval_rot(tarval *a, tarval *b)
1432 char *temp_val = NULL;
1436 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1438 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1439 /* vector arithmetic not implemented yet */
1443 if (get_mode_modulo_shift(a->mode) != 0)
1445 temp_val = alloca(sc_get_buffer_length());
1447 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1448 sc_mod(b->value, temp_val, temp_val);
1451 temp_val = (char*)b->value;
1453 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1454 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1458 * carry flag of the last operation
1460 int tarval_carry(void)
1462 return sc_had_carry();
1468 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1470 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1474 const tarval_mode_info *mode_info;
1475 const char *prefix, *suffix;
1479 mode_info = tv->mode->tv_priv;
1481 mode_info = &default_info;
1482 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1483 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1485 switch (get_mode_sort(tv->mode))
1487 case irms_int_number:
1488 case irms_character:
1489 switch (mode_info->mode_output) {
1492 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1496 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1502 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1505 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1507 case irms_float_number:
1508 switch (mode_info->mode_output) {
1510 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1513 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1518 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1522 case irms_reference:
1523 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1524 if (tv->value != NULL){
1525 if (len > tv->length) {
1526 memcpy(buf, tv->value, tv->length);
1527 buf[tv->length] = '\0';
1531 memcpy(buf, tv->value, len-1);
1537 return snprintf(buf, len, "void");
1539 case irms_internal_boolean:
1540 switch (mode_info->mode_output) {
1546 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1550 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1553 case irms_control_flow:
1555 case irms_auxiliary:
1556 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1564 * Output of tarvals to stdio.
1566 int tarval_printf(tarval *tv) {
1570 res = tarval_snprintf(buf, sizeof(buf), tv);
1571 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1577 char *get_tarval_bitpattern(tarval *tv)
1580 int n = get_mode_size_bits(tv->mode);
1581 int bytes = (n + 7) / 8;
1582 char *res = malloc((n + 1) * sizeof(char));
1585 for(i = 0; i < bytes; i++) {
1586 byte = get_tarval_sub_bits(tv, i);
1587 for(j = 1; j < 256; j <<= 1)
1589 res[pos++] = j & byte ? '1' : '0';
1598 * access to the bitpattern
1600 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1602 switch (get_mode_sort(tv->mode)) {
1603 case irms_int_number:
1604 case irms_character:
1605 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1607 case irms_float_number:
1608 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1616 * Specify the output options of one mode.
1618 * This functions stores the modinfo, so DO NOT DESTROY it.
1620 * Returns zero on success.
1622 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1626 mode->tv_priv = modeinfo;
1631 * Returns the output options of one mode.
1633 * This functions returns the mode info of a given mode.
1635 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1639 return mode->tv_priv;
1643 * Identifying tarvals values for algebraic simplifications.
1646 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1647 * - TV_CLASSIFY_ONE for multiplicative neutral,
1648 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1649 * - TV_CLASSIFY_OTHER else
1651 tarval_classification_t classify_tarval(tarval *tv)
1654 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1656 if (tv == get_mode_null(tv->mode))
1657 return TV_CLASSIFY_NULL;
1658 else if (tv == get_mode_one(tv->mode))
1659 return TV_CLASSIFY_ONE;
1660 else if ((get_mode_sort(tv->mode) == irms_int_number)
1661 && (tv == new_tarval_from_long(-1, tv->mode)))
1662 return TV_CLASSIFY_ALL_ONE;
1664 return TV_CLASSIFY_OTHER;
1668 * Returns non-zero if a given (integer) tarval has only one single bit
1671 int is_single_bit_tarval(tarval *tv) {
1675 if (!tv || tv == tarval_bad) return 0;
1676 if (! mode_is_int(tv->mode)) return 0;
1678 l = get_mode_size_bytes(tv->mode);
1679 for (bits = 0, i = l - 1; i >= 0; --i) {
1680 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1682 /* check for more than one bit in these */
1694 * Sets the overflow mode for integer operations.
1696 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1697 int_overflow_mode = ov_mode;
1701 * Get the overflow mode for integer operations.
1703 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1704 return int_overflow_mode;
1708 * default mode_info for output as HEX
1710 static const tarval_mode_info hex_output = {
1717 * default mode_info for output as reference
1719 static const tarval_mode_info reference_output = {
1726 * Initialization of the tarval module: called before init_mode()
1728 void init_tarval_1(long null_value)
1731 _null_value = null_value;
1733 /* initialize the sets holding the tarvals with a comparison function and
1734 * an initial size, which is the expected number of constants */
1735 tarvals = new_set(memcmp, N_CONSTANTS);
1736 values = new_set(memcmp, N_CONSTANTS);
1737 /* init strcalc with precision of 68 to support floating point values with 64
1738 * bit mantissa (needs extra bits for rounding and overflow) */
1744 * Initialization of the tarval module: called after init_mode()
1746 void init_tarval_2(void)
1750 tarval_bad->kind = k_tarval;
1751 tarval_bad->mode = mode_BAD;
1753 tarval_undefined->kind = k_tarval;
1754 tarval_undefined->mode = mode_ANY;
1756 tarval_b_true->kind = k_tarval;
1757 tarval_b_true->mode = mode_b;
1759 tarval_b_false->kind = k_tarval;
1760 tarval_b_false->mode = mode_b;
1763 * assign output modes that are compatible with the
1764 * old implementation: Hex output
1766 set_tarval_mode_output_option(mode_U, &hex_output);
1767 set_tarval_mode_output_option(mode_C, &hex_output);
1768 set_tarval_mode_output_option(mode_Bs, &hex_output);
1769 set_tarval_mode_output_option(mode_Bu, &hex_output);
1770 set_tarval_mode_output_option(mode_Hs, &hex_output);
1771 set_tarval_mode_output_option(mode_Hu, &hex_output);
1772 set_tarval_mode_output_option(mode_Is, &hex_output);
1773 set_tarval_mode_output_option(mode_Iu, &hex_output);
1774 set_tarval_mode_output_option(mode_Ls, &hex_output);
1775 set_tarval_mode_output_option(mode_Lu, &hex_output);
1776 set_tarval_mode_output_option(mode_P, &reference_output);
1779 /* free all memory occupied by tarval. */
1780 void finish_tarval(void) {
1783 del_set(tarvals); tarvals = NULL;
1784 del_set(values); values = NULL;
1787 /****************************************************************************
1789 ****************************************************************************/