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 "tune.h" */ /* some constants */
48 #include "entity_t.h" /* needed to store pointers to entities */
50 #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 */
64 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
66 #define SWITCH_NOINFINITY 0
67 #define SWITCH_NODENORMALS 0
69 /****************************************************************************
70 * local definitions and macros
71 ****************************************************************************/
73 # define TARVAL_VERIFY(a) tarval_verify((a))
75 # define TARVAL_VERIFY(a) ((void)0)
78 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
79 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
81 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
82 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
84 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
86 static long long count = 0;
87 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
89 # define ANNOUNCE() ((void)0)
91 /****************************************************************************
93 ****************************************************************************/
94 static struct set *tarvals; /* container for tarval structs */
95 static struct set *values; /* container for values */
96 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
98 /****************************************************************************
100 ****************************************************************************/
102 static int hash_val(const void *value, unsigned int length);
103 static int hash_tv(tarval *tv);
104 static void _fail_verify(tarval *tv, const char* file, int line)
106 /* print a memory image of the tarval and throw an assertion */
108 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
110 printf("%s:%d: Invalid tarval (null)", file, line);
114 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
117 INLINE static void tarval_verify(tarval *tv)
123 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
124 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
126 if (!FIND_TARVAL(tv)) fail_verify(tv);
127 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
133 static int hash_tv(tarval *tv)
135 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
138 static int hash_val(const void *value, unsigned int length)
141 unsigned int hash = 0;
143 /* scramble the byte - array */
144 for (i = 0; i < length; i++)
146 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
147 hash += (hash << 11) ^ (hash >> 17);
153 /* finds tarval with value/mode or creates new tarval */
154 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
161 /* if there already is such a value, it is returned, else value
162 * is copied into the set */
163 tv.value = INSERT_VALUE(value, length);
167 /* if there is such a tarval, it is returned, else tv is copied
169 return (tarval *)INSERT_TARVAL(&tv);
175 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
177 switch (get_mode_sort(mode))
179 case irms_int_number:
180 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
181 switch (GET_OVERFLOW_MODE()) {
182 case TV_OVERFLOW_SATURATE:
183 return get_mode_max(mode);
184 case TV_OVERFLOW_WRAP:
186 char *temp = alloca(sc_get_buffer_length());
187 char *diff = alloca(sc_get_buffer_length());
188 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
189 sc_val_from_ulong(1, temp);
190 sc_add(diff, temp, diff);
191 sc_sub(value, diff, temp);
192 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
193 sc_sub(temp, diff, temp);
194 return get_tarval(temp, length, mode);
196 case TV_OVERFLOW_BAD:
199 return get_tarval(value, length, mode);
202 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
203 switch (GET_OVERFLOW_MODE()) {
204 case TV_OVERFLOW_SATURATE:
205 return get_mode_min(mode);
206 case TV_OVERFLOW_WRAP:
208 char *temp = alloca(sc_get_buffer_length());
209 char *diff = alloca(sc_get_buffer_length());
210 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
211 sc_val_from_ulong(1, temp);
212 sc_add(diff, temp, diff);
213 sc_add(value, diff, temp);
214 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
215 sc_add(temp, diff, temp);
216 return get_tarval(temp, length, mode);
218 case TV_OVERFLOW_BAD:
221 return get_tarval(value, length, mode);
226 case irms_float_number:
227 if (SWITCH_NOINFINITY && fc_is_inf(value))
229 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
232 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
234 return get_mode_null(mode);
240 return get_tarval(value, length, mode);
245 * public variables declared in tv.h
247 static tarval reserved_tv[5];
249 tarval *tarval_bad = &reserved_tv[0];
250 tarval *tarval_undefined = &reserved_tv[1];
251 tarval *tarval_b_false = &reserved_tv[2];
252 tarval *tarval_b_true = &reserved_tv[3];
253 tarval *tarval_P_void = &reserved_tv[4];
256 * public functions declared in tv.h
260 * Constructors =============================================================
262 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
269 switch (get_mode_sort(mode))
271 case irms_control_flow:
277 case irms_internal_boolean:
278 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
279 if (strcasecmp(str, "true")) return tarval_b_true;
280 else if (strcasecmp(str, "false")) return tarval_b_true;
282 /* XXX This is C semantics */
283 return atoi(str) ? tarval_b_true : tarval_b_false;
285 case irms_float_number:
286 switch(get_mode_size_bits(mode)) {
288 fc_val_from_str(str, len, 8, 23, NULL);
291 fc_val_from_str(str, len, 11, 52, NULL);
294 fc_val_from_str(str, len, 15, 64, NULL);
297 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
299 case irms_int_number:
301 sc_val_from_str(str, len, NULL);
302 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
305 return get_tarval(str, len, mode);
308 assert(0); /* can't be reached, can it? */
313 * helper function, create a tarval from long
315 tarval *new_tarval_from_long(long l, ir_mode *mode)
318 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
320 switch(get_mode_sort(mode))
322 case irms_internal_boolean:
323 /* XXX C semantics ! */
324 return l ? tarval_b_true : tarval_b_false ;
326 case irms_int_number:
328 sc_val_from_long(l, NULL);
329 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
331 case irms_float_number:
332 return new_tarval_from_double((long double)l, mode);
335 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
343 /* returns non-zero if can be converted to long */
344 int tarval_is_long(tarval *tv)
347 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
349 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
351 /* the value might be too big to fit in a long */
352 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
353 if (sc_comp(sc_get_buffer(), tv->value) == -1)
355 /* really doesn't fit */
362 /* this might overflow the machine's long, so use only with small values */
363 long get_tarval_long(tarval* tv)
366 assert(tarval_is_long(tv) && "tarval too big to fit in long");
368 return sc_val_to_long(tv->value);
371 tarval *new_tarval_from_double(long double d, ir_mode *mode)
374 assert(mode && (get_mode_sort(mode) == irms_float_number));
376 switch (get_mode_size_bits(mode)) {
378 fc_val_from_float(d, 8, 23, NULL);
381 fc_val_from_float(d, 11, 52, NULL);
384 fc_val_from_float(d, 15, 64, NULL);
387 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
390 /* returns non-zero if can be converted to double */
391 int tarval_is_double(tarval *tv)
396 return (get_mode_sort(tv->mode) == irms_float_number);
399 long double get_tarval_double(tarval *tv)
402 assert(tarval_is_double(tv));
404 return fc_val_to_float(tv->value);
409 * Access routines for tarval fields ========================================
412 /* get the mode of the tarval */
413 ir_mode *(get_tarval_mode)(const tarval *tv) {
414 return _get_tarval_mode(tv);
418 * Special value query functions ============================================
420 * These functions calculate and return a tarval representing the requested
422 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
423 * functions, but these are stored on initialization of the irmode module and
424 * therefore the irmode functions should be prefered to the functions below.
427 tarval *(get_tarval_bad)(void) {
428 return _get_tarval_bad();
431 tarval *(get_tarval_undefined)(void) {
432 return _get_tarval_undefined();
435 tarval *(get_tarval_b_false)(void) {
436 return _get_tarval_b_false();
439 tarval *(get_tarval_b_true)(void) {
440 return _get_tarval_b_true();
443 tarval *(get_tarval_P_void)(void) {
444 return _get_tarval_P_void();
447 tarval *get_tarval_max(ir_mode *mode)
452 if (get_mode_n_vector_elems(mode) > 1) {
453 /* vector arithmetic not implemented yet */
457 switch(get_mode_sort(mode))
460 case irms_control_flow:
466 case irms_internal_boolean:
467 return tarval_b_true;
469 case irms_float_number:
470 switch(get_mode_size_bits(mode))
473 fc_get_max(8, 23, NULL);
476 fc_get_max(11, 52, NULL);
479 fc_get_max(15, 64, NULL);
482 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
484 case irms_int_number:
486 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
487 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
492 tarval *get_tarval_min(ir_mode *mode)
497 if (get_mode_n_vector_elems(mode) > 1) {
498 /* vector arithmetic not implemented yet */
502 switch(get_mode_sort(mode))
505 case irms_control_flow:
511 case irms_internal_boolean:
512 return tarval_b_false;
514 case irms_float_number:
515 switch(get_mode_size_bits(mode))
518 fc_get_min(8, 23, NULL);
521 fc_get_min(11, 52, NULL);
524 fc_get_min(15, 64, NULL);
527 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
529 case irms_int_number:
531 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
532 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
537 tarval *get_tarval_null(ir_mode *mode)
542 if (get_mode_n_vector_elems(mode) > 1) {
543 /* vector arithmetic not implemented yet */
547 switch(get_mode_sort(mode))
549 case irms_control_flow:
552 case irms_internal_boolean:
556 case irms_float_number:
557 return new_tarval_from_double(0.0, mode);
559 case irms_int_number:
561 return new_tarval_from_long(0l, mode);
564 return tarval_P_void;
569 tarval *get_tarval_one(ir_mode *mode)
574 if (get_mode_n_vector_elems(mode) > 1) {
575 /* vector arithmetic not implemented yet */
579 switch(get_mode_sort(mode))
581 case irms_control_flow:
584 case irms_internal_boolean:
589 case irms_float_number:
590 return new_tarval_from_double(1.0, mode);
592 case irms_int_number:
594 return new_tarval_from_long(1l, mode);
600 tarval *get_tarval_minus_one(ir_mode *mode)
605 if (get_mode_n_vector_elems(mode) > 1) {
606 /* vector arithmetic not implemented yet */
610 switch(get_mode_sort(mode))
612 case irms_control_flow:
615 case irms_internal_boolean:
620 case irms_float_number:
621 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
623 case irms_int_number:
625 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
630 tarval *get_tarval_nan(ir_mode *mode)
635 if (get_mode_n_vector_elems(mode) > 1) {
636 /* vector arithmetic not implemented yet */
640 if (get_mode_sort(mode) == irms_float_number) {
641 switch(get_mode_size_bits(mode))
644 fc_get_qnan(8, 23, NULL);
647 fc_get_qnan(11, 52, NULL);
650 fc_get_qnan(15, 64, NULL);
653 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
656 assert(0 && "tarval is not floating point");
661 tarval *get_tarval_plus_inf(ir_mode *mode)
666 if (get_mode_n_vector_elems(mode) > 1) {
667 /* vector arithmetic not implemented yet */
671 if (get_mode_sort(mode) == irms_float_number) {
672 switch(get_mode_size_bits(mode))
675 fc_get_plusinf(8, 23, NULL);
678 fc_get_plusinf(11, 52, NULL);
681 fc_get_plusinf(15, 64, NULL);
684 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
687 assert(0 && "tarval is not floating point");
692 tarval *get_tarval_minus_inf(ir_mode *mode)
697 if (get_mode_n_vector_elems(mode) > 1) {
698 /* vector arithmetic not implemented yet */
702 if (get_mode_sort(mode) == irms_float_number) {
703 switch(get_mode_size_bits(mode))
706 fc_get_minusinf(8, 23, NULL);
709 fc_get_minusinf(11, 52, NULL);
712 fc_get_minusinf(15, 64, NULL);
715 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
718 assert(0 && "tarval is not floating point");
724 * Arithmethic operations on tarvals ========================================
728 * test if negative number, 1 means 'yes'
730 int tarval_is_negative(tarval *a)
735 if (get_mode_n_vector_elems(a->mode) > 1) {
736 /* vector arithmetic not implemented yet */
737 assert(0 && "tarval_is_negative is not allowed for vector modes");
741 switch (get_mode_sort(a->mode))
743 case irms_int_number:
744 if (!mode_is_signed(a->mode)) return 0;
746 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
748 case irms_float_number:
749 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
752 assert(0 && "not implemented");
758 * test if null, 1 means 'yes'
760 int tarval_is_null(tarval *a)
762 ir_mode *m = get_tarval_mode(a);
764 return a == get_tarval_null(m);
768 * test if one, 1 means 'yes'
770 int tarval_is_one(tarval *a)
772 ir_mode *m = get_tarval_mode(a);
774 return a == get_tarval_one(m);
780 pn_Cmp tarval_cmp(tarval *a, tarval *b)
786 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
787 if (a == tarval_undefined || b == tarval_undefined) return pn_Cmp_False;
788 if (a == b) return pn_Cmp_Eq;
789 if (a->mode != b->mode) return pn_Cmp_False;
791 if (get_mode_n_vector_elems(a->mode) > 1) {
792 /* vector arithmetic not implemented yet */
793 assert(0 && "cmp not implemented for vector modes");
796 /* Here the two tarvals are unequal and of the same mode */
797 switch (get_mode_sort(a->mode))
799 case irms_control_flow:
805 case irms_float_number:
806 switch (fc_comp(a->value, b->value)) {
807 case -1: return pn_Cmp_Lt;
808 case 0: assert(0 && "different tarvals compare equal"); return pn_Cmp_Eq;
809 case 1: return pn_Cmp_Gt;
810 case 2: return pn_Cmp_Uo;
811 default: return pn_Cmp_False;
813 case irms_int_number:
815 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
817 case irms_internal_boolean:
818 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
824 * convert to other mode
826 tarval *tarval_convert_to(tarval *src, ir_mode *m)
834 if (src->mode == m) return src;
836 if (get_mode_n_vector_elems(src->mode) > 1) {
837 /* vector arithmetic not implemented yet */
841 switch (get_mode_sort(src->mode))
843 case irms_control_flow:
848 /* cast float to something */
849 case irms_float_number:
850 switch (get_mode_sort(m)) {
851 case irms_float_number:
852 switch (get_mode_size_bits(m))
855 fc_cast(src->value, 8, 23, NULL);
858 fc_cast(src->value, 11, 52, NULL);
861 fc_cast(src->value, 15, 64, NULL);
866 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
868 case irms_int_number:
869 switch (GET_FLOAT_TO_INT_MODE())
872 fc_int(src->value, NULL);
875 fc_rnd(src->value, NULL);
880 /* XXX floating point unit can't produce a value in integer
882 * an intermediate representation is needed here first. */
883 /* return get_tarval(); */
887 /* the rest can't be converted */
892 /* cast int to something */
893 case irms_int_number:
894 switch (get_mode_sort(m)) {
895 case irms_int_number:
897 return get_tarval_overflow(src->value, src->length, m);
899 case irms_internal_boolean:
900 /* XXX C semantics */
901 if (src == get_mode_null(src->mode)) return tarval_b_false;
902 else return tarval_b_true;
904 case irms_float_number:
905 /* XXX floating point unit does not understand internal integer
906 * representation, convert to string first, then create float from
908 buffer = alloca(100);
909 /* decimal string representation because hexadecimal output is
910 * interpreted unsigned by fc_val_from_str, so this is a HACK */
911 snprintf(buffer, 100, "%s",
912 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
913 switch (get_mode_size_bits(m))
916 fc_val_from_str(buffer, 0, 8, 23, NULL);
919 fc_val_from_str(buffer, 0, 11, 52, NULL);
922 fc_val_from_str(buffer, 0, 15, 64, NULL);
925 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
932 case irms_internal_boolean:
933 switch (get_mode_sort(m))
935 case irms_int_number:
936 if (src == tarval_b_true) return get_mode_one(m);
937 else return get_mode_null(m);
956 tarval *tarval_not(tarval *a)
962 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
964 /* works for vector mode without changes */
966 switch (get_mode_sort(a->mode))
968 case irms_int_number:
969 buffer = alloca(sc_get_buffer_length());
970 sc_not(a->value, buffer);
971 return get_tarval(buffer, a->length, a->mode);
979 * arithmetic negation
981 tarval *tarval_neg(tarval *a)
987 assert(mode_is_num(a->mode)); /* negation only for numerical values */
989 /* note: negation is allowed even for unsigned modes. */
991 if (get_mode_n_vector_elems(a->mode) > 1) {
992 /* vector arithmetic not implemented yet */
996 switch (get_mode_sort(a->mode))
998 case irms_int_number:
999 buffer = alloca(sc_get_buffer_length());
1000 sc_neg(a->value, buffer);
1001 return get_tarval_overflow(buffer, a->length, a->mode);
1003 case irms_float_number:
1004 fc_neg(a->value, NULL);
1005 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1015 tarval *tarval_add(tarval *a, tarval *b)
1022 assert(a->mode == b->mode);
1024 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1025 /* vector arithmetic not implemented yet */
1029 switch (get_mode_sort(a->mode))
1031 case irms_character:
1032 case irms_int_number:
1033 /* modes of a,b are equal, so result has mode of a as this might be the character */
1034 buffer = alloca(sc_get_buffer_length());
1035 sc_add(a->value, b->value, buffer);
1036 return get_tarval_overflow(buffer, a->length, a->mode);
1038 case irms_float_number:
1039 fc_add(a->value, b->value, NULL);
1040 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1050 tarval *tarval_sub(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 */
1063 switch (get_mode_sort(a->mode))
1065 case irms_character:
1066 case irms_int_number:
1067 /* modes of a,b are equal, so result has mode of a as this might be the character */
1068 buffer = alloca(sc_get_buffer_length());
1069 sc_sub(a->value, b->value, buffer);
1070 return get_tarval_overflow(buffer, a->length, a->mode);
1072 case irms_float_number:
1073 fc_sub(a->value, b->value, NULL);
1074 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1084 tarval *tarval_mul(tarval *a, tarval *b)
1091 assert(a->mode == b->mode);
1093 if (get_mode_n_vector_elems(a->mode) > 1) {
1094 /* vector arithmetic not implemented yet */
1098 switch (get_mode_sort(a->mode))
1100 case irms_int_number:
1101 /* modes of a,b are equal */
1102 buffer = alloca(sc_get_buffer_length());
1103 sc_mul(a->value, b->value, buffer);
1104 return get_tarval_overflow(buffer, a->length, a->mode);
1106 case irms_float_number:
1107 fc_mul(a->value, b->value, NULL);
1108 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1116 * floating point division
1118 tarval *tarval_quo(tarval *a, tarval *b)
1123 assert((a->mode == b->mode) && mode_is_float(a->mode));
1125 if (get_mode_n_vector_elems(a->mode) > 1) {
1126 /* vector arithmetic not implemented yet */
1130 fc_div(a->value, b->value, NULL);
1131 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1136 * overflow is impossible, but look out for division by zero
1138 tarval *tarval_div(tarval *a, tarval *b)
1143 assert((a->mode == b->mode) && mode_is_int(a->mode));
1145 if (get_mode_n_vector_elems(a->mode) > 1) {
1146 /* vector arithmetic not implemented yet */
1151 if (b == get_mode_null(b->mode)) return tarval_bad;
1152 /* modes of a,b are equal */
1153 sc_div(a->value, b->value, NULL);
1154 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1159 * overflow is impossible, but look out for division by zero
1161 tarval *tarval_mod(tarval *a, tarval *b)
1166 assert((a->mode == b->mode) && mode_is_int(a->mode));
1168 if (get_mode_n_vector_elems(a->mode) > 1) {
1169 /* vector arithmetic not implemented yet */
1174 if (b == get_mode_null(b->mode)) return tarval_bad;
1175 /* modes of a,b are equal */
1176 sc_mod(a->value, b->value, NULL);
1177 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1183 tarval *tarval_abs(tarval *a)
1189 assert(mode_is_num(a->mode));
1191 if (get_mode_n_vector_elems(a->mode) > 1) {
1192 /* vector arithmetic not implemented yet */
1196 switch (get_mode_sort(a->mode))
1198 case irms_int_number:
1199 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1201 buffer = alloca(sc_get_buffer_length());
1202 sc_neg(a->value, buffer);
1203 return get_tarval_overflow(buffer, a->length, a->mode);
1207 case irms_float_number:
1208 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1210 fc_neg(a->value, NULL);
1211 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1224 tarval *tarval_and(tarval *a, tarval *b)
1229 assert(a->mode == b->mode);
1231 /* works even for vector modes */
1233 switch(get_mode_sort(a->mode))
1235 case irms_internal_boolean:
1236 return (a == tarval_b_false) ? a : b;
1238 case irms_int_number:
1239 sc_and(a->value, b->value, NULL);
1240 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1243 assert(0 && "operation not defined on mode");
1251 tarval *tarval_or (tarval *a, tarval *b)
1256 assert(a->mode == b->mode);
1258 /* works even for vector modes */
1260 switch (get_mode_sort(a->mode))
1262 case irms_internal_boolean:
1263 return (a == tarval_b_true) ? a : b;
1265 case irms_int_number:
1266 sc_or(a->value, b->value, NULL);
1267 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1270 assert(0 && "operation not defined on mode");
1276 * bitwise exclusive or (xor)
1278 tarval *tarval_eor(tarval *a, tarval *b)
1283 assert((a->mode == b->mode));
1285 /* works even for vector modes */
1287 switch (get_mode_sort(a->mode))
1289 case irms_internal_boolean:
1290 return (a == b)? tarval_b_false : tarval_b_true;
1292 case irms_int_number:
1293 sc_xor(a->value, b->value, NULL);
1294 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1297 assert(0 && "operation not defined on mode");
1303 * bitwise left shift
1305 tarval *tarval_shl(tarval *a, tarval *b)
1307 char *temp_val = NULL;
1311 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1313 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1314 /* vector arithmetic not implemented yet */
1318 if (get_mode_modulo_shift(a->mode) != 0)
1320 temp_val = alloca(sc_get_buffer_length());
1322 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1323 sc_mod(b->value, temp_val, temp_val);
1326 temp_val = (char*)b->value;
1328 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1329 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1333 * bitwise unsigned right shift
1335 tarval *tarval_shr(tarval *a, tarval *b)
1337 char *temp_val = NULL;
1341 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1343 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1344 /* vector arithmetic not implemented yet */
1348 if (get_mode_modulo_shift(a->mode) != 0)
1350 temp_val = alloca(sc_get_buffer_length());
1352 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1353 sc_mod(b->value, temp_val, temp_val);
1356 temp_val = (char*)b->value;
1358 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1359 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1363 * bitwise signed right shift
1365 tarval *tarval_shrs(tarval *a, tarval *b)
1367 char *temp_val = NULL;
1371 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1373 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1374 /* vector arithmetic not implemented yet */
1378 if (get_mode_modulo_shift(a->mode) != 0)
1380 temp_val = alloca(sc_get_buffer_length());
1382 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1383 sc_mod(b->value, temp_val, temp_val);
1386 temp_val = (char*)b->value;
1388 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1389 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1395 tarval *tarval_rot(tarval *a, tarval *b)
1397 char *temp_val = NULL;
1401 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1403 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1404 /* vector arithmetic not implemented yet */
1408 if (get_mode_modulo_shift(a->mode) != 0)
1410 temp_val = alloca(sc_get_buffer_length());
1412 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1413 sc_mod(b->value, temp_val, temp_val);
1416 temp_val = (char*)b->value;
1418 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1419 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1423 * carry flag of the last operation
1425 int tarval_carry(void)
1427 return sc_had_carry();
1433 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1435 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1439 const tarval_mode_info *mode_info;
1440 const char *prefix, *suffix;
1444 mode_info = tv->mode->tv_priv;
1446 mode_info = &default_info;
1447 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1448 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1450 switch (get_mode_sort(tv->mode))
1452 case irms_int_number:
1453 case irms_character:
1454 switch (mode_info->mode_output) {
1457 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1461 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1467 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1470 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1472 case irms_float_number:
1473 switch (mode_info->mode_output) {
1475 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1478 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1483 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1487 case irms_reference:
1488 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1489 if (tv->value != NULL){
1490 if (len > tv->length) {
1491 memcpy(buf, tv->value, tv->length);
1492 buf[tv->length] = '\0';
1496 memcpy(buf, tv->value, len-1);
1502 return snprintf(buf, len, "void");
1504 case irms_internal_boolean:
1505 switch (mode_info->mode_output) {
1511 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1515 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1518 case irms_control_flow:
1520 case irms_auxiliary:
1521 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1529 * Output of tarvals to stdio.
1531 int tarval_printf(tarval *tv) {
1535 res = tarval_snprintf(buf, sizeof(buf), tv);
1536 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1542 char *get_tarval_bitpattern(tarval *tv)
1545 int n = get_mode_size_bits(tv->mode);
1546 int bytes = (n + 7) / 8;
1547 char *res = malloc((n + 1) * sizeof(char));
1550 for(i = 0; i < bytes; i++) {
1551 byte = get_tarval_sub_bits(tv, i);
1552 for(j = 1; j < 256; j <<= 1)
1554 res[pos++] = j & byte ? '1' : '0';
1563 * access to the bitpattern
1565 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1567 switch (get_mode_sort(tv->mode)) {
1568 case irms_int_number:
1569 case irms_character:
1570 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1572 case irms_float_number:
1573 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1581 * Specify the output options of one mode.
1583 * This functions stores the modinfo, so DO NOT DESTROY it.
1585 * Returns zero on success.
1587 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1591 mode->tv_priv = modeinfo;
1596 * Returns the output options of one mode.
1598 * This functions returns the mode info of a given mode.
1600 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1604 return mode->tv_priv;
1608 * Identifying tarvals values for algebraic simplifications.
1611 * - TV_CLASSIFY_NULL for additive neutral,
1612 * - TV_CLASSIFY_ONE for multiplicative neutral,
1613 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1614 * - TV_CLASSIFY_OTHER else
1616 tarval_classification_t classify_tarval(tarval *tv)
1619 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1621 if (tv == get_mode_null(tv->mode))
1622 return TV_CLASSIFY_NULL;
1623 else if (tv == get_mode_one(tv->mode))
1624 return TV_CLASSIFY_ONE;
1625 else if ((get_mode_sort(tv->mode) == irms_int_number)
1626 && (tv == new_tarval_from_long(-1, tv->mode)))
1627 return TV_CLASSIFY_ALL_ONE;
1629 return TV_CLASSIFY_OTHER;
1633 * Sets the overflow mode for integer operations.
1635 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1636 int_overflow_mode = ov_mode;
1640 * Get the overflow mode for integer operations.
1642 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1643 return int_overflow_mode;
1647 * default mode_info for output as HEX
1649 static const tarval_mode_info hex_output = {
1656 * default mode_info for output as reference
1658 static const tarval_mode_info reference_output = {
1666 * Initialization of the tarval module: called before init_mode()
1668 void init_tarval_1(void)
1671 /* initialize the sets holding the tarvals with a comparison function and
1672 * an initial size, which is the expected number of constants */
1673 tarvals = new_set(memcmp, N_CONSTANTS);
1674 values = new_set(memcmp, N_CONSTANTS);
1675 /* init strcalc with precision of 68 to support floating point values with 64
1676 * bit mantissa (needs extra bits for rounding and overflow) */
1682 * Initialization of the tarval module: called after init_mode()
1684 void init_tarval_2(void)
1688 tarval_bad->mode = mode_BAD;
1689 tarval_undefined->mode = mode_ANY;
1690 tarval_b_true->mode = mode_b;
1691 tarval_b_false->mode = mode_b;
1692 tarval_P_void->mode = mode_P;
1695 * assign output modes that are compatible with the
1696 * old implementation: Hex output
1698 set_tarval_mode_output_option(mode_U, &hex_output);
1699 set_tarval_mode_output_option(mode_C, &hex_output);
1700 set_tarval_mode_output_option(mode_Bs, &hex_output);
1701 set_tarval_mode_output_option(mode_Bu, &hex_output);
1702 set_tarval_mode_output_option(mode_Hs, &hex_output);
1703 set_tarval_mode_output_option(mode_Hu, &hex_output);
1704 set_tarval_mode_output_option(mode_Is, &hex_output);
1705 set_tarval_mode_output_option(mode_Iu, &hex_output);
1706 set_tarval_mode_output_option(mode_Ls, &hex_output);
1707 set_tarval_mode_output_option(mode_Lu, &hex_output);
1708 set_tarval_mode_output_option(mode_P, &reference_output);
1711 /* free all memory occupied by tarval. */
1712 void finish_tarval(void) {
1715 del_set(tarvals); tarvals = NULL;
1716 del_set(values); values = NULL;
1719 /****************************************************************************
1721 ****************************************************************************/