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 ========================================
411 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
419 void *get_tarval_link (tarval *tv)
428 * Special value query functions ============================================
430 * These functions calculate and return a tarval representing the requested
432 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
433 * functions, but these are stored on initialization of the irmode module and
434 * therefore the irmode functions should be prefered to the functions below.
437 tarval *get_tarval_bad(void)
442 tarval *get_tarval_undefined(void)
445 return tarval_undefined;
447 tarval *get_tarval_b_false(void)
450 return tarval_b_false;
452 tarval *get_tarval_b_true(void)
455 return tarval_b_true;
457 tarval *get_tarval_P_void(void)
460 return tarval_P_void;
463 tarval *get_tarval_max(ir_mode *mode)
468 if (get_mode_n_vector_elems(mode) > 1) {
469 /* vector arithmetic not implemented yet */
473 switch(get_mode_sort(mode))
476 case irms_control_flow:
482 case irms_internal_boolean:
483 return tarval_b_true;
485 case irms_float_number:
486 switch(get_mode_size_bits(mode))
489 fc_get_max(8, 23, NULL);
492 fc_get_max(11, 52, NULL);
495 fc_get_max(15, 64, NULL);
498 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
500 case irms_int_number:
502 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
503 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
508 tarval *get_tarval_min(ir_mode *mode)
513 if (get_mode_n_vector_elems(mode) > 1) {
514 /* vector arithmetic not implemented yet */
518 switch(get_mode_sort(mode))
521 case irms_control_flow:
527 case irms_internal_boolean:
528 return tarval_b_false;
530 case irms_float_number:
531 switch(get_mode_size_bits(mode))
534 fc_get_min(8, 23, NULL);
537 fc_get_min(11, 52, NULL);
540 fc_get_min(15, 64, NULL);
543 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
545 case irms_int_number:
547 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
548 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
553 tarval *get_tarval_null(ir_mode *mode)
558 if (get_mode_n_vector_elems(mode) > 1) {
559 /* vector arithmetic not implemented yet */
563 switch(get_mode_sort(mode))
565 case irms_control_flow:
568 case irms_internal_boolean:
572 case irms_float_number:
573 return new_tarval_from_double(0.0, mode);
575 case irms_int_number:
577 return new_tarval_from_long(0l, mode);
580 return tarval_P_void;
585 tarval *get_tarval_one(ir_mode *mode)
590 if (get_mode_n_vector_elems(mode) > 1) {
591 /* vector arithmetic not implemented yet */
595 switch(get_mode_sort(mode))
597 case irms_control_flow:
600 case irms_internal_boolean:
605 case irms_float_number:
606 return new_tarval_from_double(1.0, mode);
608 case irms_int_number:
610 return new_tarval_from_long(1l, mode);
616 tarval *get_tarval_nan(ir_mode *mode)
621 if (get_mode_n_vector_elems(mode) > 1) {
622 /* vector arithmetic not implemented yet */
626 if (get_mode_sort(mode) == irms_float_number) {
627 switch(get_mode_size_bits(mode))
630 fc_get_qnan(8, 23, NULL);
633 fc_get_qnan(11, 52, NULL);
636 fc_get_qnan(15, 64, NULL);
639 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
642 assert(0 && "tarval is not floating point");
647 tarval *get_tarval_inf(ir_mode *mode)
652 if (get_mode_n_vector_elems(mode) > 1) {
653 /* vector arithmetic not implemented yet */
657 if (get_mode_sort(mode) == irms_float_number) {
658 switch(get_mode_size_bits(mode))
661 fc_get_plusinf(8, 23, NULL);
664 fc_get_plusinf(11, 52, NULL);
667 fc_get_plusinf(15, 64, NULL);
670 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
673 assert(0 && "tarval is not floating point");
679 * Arithmethic operations on tarvals ========================================
683 * test if negative number, 1 means 'yes'
685 int tarval_is_negative(tarval *a)
690 if (get_mode_n_vector_elems(a->mode) > 1) {
691 /* vector arithmetic not implemented yet */
692 assert(0 && "tarval_is_negative is not allowed for vector modes");
696 switch (get_mode_sort(a->mode))
698 case irms_int_number:
699 if (!mode_is_signed(a->mode)) return 0;
701 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
703 case irms_float_number:
704 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
707 assert(0 && "not implemented");
713 * test if null, 1 means 'yes'
715 int tarval_is_null(tarval *a)
717 ir_mode *m = get_tarval_mode(a);
719 return a == get_tarval_null(m);
723 * test if one, 1 means 'yes'
725 int tarval_is_one(tarval *a)
727 ir_mode *m = get_tarval_mode(a);
729 return a == get_tarval_one(m);
735 pnc_number tarval_cmp(tarval *a, tarval *b)
741 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
742 if (a == tarval_undefined || b == tarval_undefined) return False;
743 if (a == b) return Eq;
744 if (a->mode != b->mode) return False;
746 if (get_mode_n_vector_elems(a->mode) > 1) {
747 /* vector arithmetic not implemented yet */
748 assert(0 && "cmp not implemented for vector modes");
751 /* Here the two tarvals are unequal and of the same mode */
752 switch (get_mode_sort(a->mode))
754 case irms_control_flow:
760 case irms_float_number:
761 switch (fc_comp(a->value, b->value)) {
763 case 0: assert(0 && "different tarvals compare equal"); return Eq;
766 default: return False;
768 case irms_int_number:
770 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
772 case irms_internal_boolean:
773 return (a == tarval_b_true)?(Gt):(Lt);
779 * convert to other mode
781 tarval *tarval_convert_to(tarval *src, ir_mode *m)
789 if (src->mode == m) return src;
791 if (get_mode_n_vector_elems(src->mode) > 1) {
792 /* vector arithmetic not implemented yet */
796 switch (get_mode_sort(src->mode))
798 case irms_control_flow:
803 /* cast float to something */
804 case irms_float_number:
805 switch (get_mode_sort(m)) {
806 case irms_float_number:
807 switch (get_mode_size_bits(m))
810 fc_cast(src->value, 8, 23, NULL);
813 fc_cast(src->value, 11, 52, NULL);
816 fc_cast(src->value, 15, 64, NULL);
821 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
823 case irms_int_number:
824 switch (GET_FLOAT_TO_INT_MODE())
827 fc_int(src->value, NULL);
830 fc_rnd(src->value, NULL);
835 /* XXX floating point unit can't produce a value in integer
837 * an intermediate representation is needed here first. */
838 /* return get_tarval(); */
842 /* the rest can't be converted */
847 /* cast int to something */
848 case irms_int_number:
849 switch (get_mode_sort(m)) {
850 case irms_int_number:
852 return get_tarval_overflow(src->value, src->length, m);
854 case irms_internal_boolean:
855 /* XXX C semantics */
856 if (src == get_mode_null(src->mode)) return tarval_b_false;
857 else return tarval_b_true;
859 case irms_float_number:
860 /* XXX floating point unit does not understand internal integer
861 * representation, convert to string first, then create float from
863 buffer = alloca(100);
864 /* decimal string representation because hexadecimal output is
865 * interpreted unsigned by fc_val_from_str, so this is a HACK */
866 snprintf(buffer, 100, "%s",
867 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
868 switch (get_mode_size_bits(m))
871 fc_val_from_str(buffer, 0, 8, 23, NULL);
874 fc_val_from_str(buffer, 0, 11, 52, NULL);
877 fc_val_from_str(buffer, 0, 15, 64, NULL);
880 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
887 case irms_internal_boolean:
888 switch (get_mode_sort(m))
890 case irms_int_number:
891 if (src == tarval_b_true) return get_mode_one(m);
892 else return get_mode_null(m);
911 tarval *tarval_not(tarval *a)
917 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
919 /* works for vector mode without changes */
921 switch (get_mode_sort(a->mode))
923 case irms_int_number:
924 buffer = alloca(sc_get_buffer_length());
925 sc_not(a->value, buffer);
926 return get_tarval(buffer, a->length, a->mode);
934 * arithmetic negation
936 tarval *tarval_neg(tarval *a)
942 assert(mode_is_num(a->mode)); /* negation only for numerical values */
944 /* note: negation is allowed even for unsigned modes. */
946 if (get_mode_n_vector_elems(a->mode) > 1) {
947 /* vector arithmetic not implemented yet */
951 switch (get_mode_sort(a->mode))
953 case irms_int_number:
954 buffer = alloca(sc_get_buffer_length());
955 sc_neg(a->value, buffer);
956 return get_tarval_overflow(buffer, a->length, a->mode);
958 case irms_float_number:
959 fc_neg(a->value, NULL);
960 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
970 tarval *tarval_add(tarval *a, tarval *b)
977 assert(a->mode == b->mode);
979 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
980 /* vector arithmetic not implemented yet */
984 switch (get_mode_sort(a->mode))
987 case irms_int_number:
988 /* modes of a,b are equal, so result has mode of a as this might be the character */
989 buffer = alloca(sc_get_buffer_length());
990 sc_add(a->value, b->value, buffer);
991 return get_tarval_overflow(buffer, a->length, a->mode);
993 case irms_float_number:
994 fc_add(a->value, b->value, NULL);
995 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1005 tarval *tarval_sub(tarval *a, tarval *b)
1012 assert(a->mode == b->mode);
1014 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1015 /* vector arithmetic not implemented yet */
1018 switch (get_mode_sort(a->mode))
1020 case irms_character:
1021 case irms_int_number:
1022 /* modes of a,b are equal, so result has mode of a as this might be the character */
1023 buffer = alloca(sc_get_buffer_length());
1024 sc_sub(a->value, b->value, buffer);
1025 return get_tarval_overflow(buffer, a->length, a->mode);
1027 case irms_float_number:
1028 fc_sub(a->value, b->value, NULL);
1029 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1039 tarval *tarval_mul(tarval *a, tarval *b)
1046 assert(a->mode == b->mode);
1048 if (get_mode_n_vector_elems(a->mode) > 1) {
1049 /* vector arithmetic not implemented yet */
1053 switch (get_mode_sort(a->mode))
1055 case irms_int_number:
1056 /* modes of a,b are equal */
1057 buffer = alloca(sc_get_buffer_length());
1058 sc_mul(a->value, b->value, buffer);
1059 return get_tarval_overflow(buffer, a->length, a->mode);
1061 case irms_float_number:
1062 fc_mul(a->value, b->value, NULL);
1063 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1071 * floating point division
1073 tarval *tarval_quo(tarval *a, tarval *b)
1078 assert((a->mode == b->mode) && mode_is_float(a->mode));
1080 if (get_mode_n_vector_elems(a->mode) > 1) {
1081 /* vector arithmetic not implemented yet */
1085 fc_div(a->value, b->value, NULL);
1086 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1091 * overflow is impossible, but look out for division by zero
1093 tarval *tarval_div(tarval *a, tarval *b)
1098 assert((a->mode == b->mode) && mode_is_int(a->mode));
1100 if (get_mode_n_vector_elems(a->mode) > 1) {
1101 /* vector arithmetic not implemented yet */
1106 if (b == get_mode_null(b->mode)) return tarval_bad;
1107 /* modes of a,b are equal */
1108 sc_div(a->value, b->value, NULL);
1109 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1114 * overflow is impossible, but look out for division by zero
1116 tarval *tarval_mod(tarval *a, tarval *b)
1121 assert((a->mode == b->mode) && mode_is_int(a->mode));
1123 if (get_mode_n_vector_elems(a->mode) > 1) {
1124 /* vector arithmetic not implemented yet */
1129 if (b == get_mode_null(b->mode)) return tarval_bad;
1130 /* modes of a,b are equal */
1131 sc_mod(a->value, b->value, NULL);
1132 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1138 tarval *tarval_abs(tarval *a)
1144 assert(mode_is_num(a->mode));
1146 if (get_mode_n_vector_elems(a->mode) > 1) {
1147 /* vector arithmetic not implemented yet */
1151 switch (get_mode_sort(a->mode))
1153 case irms_int_number:
1154 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1156 buffer = alloca(sc_get_buffer_length());
1157 sc_neg(a->value, buffer);
1158 return get_tarval_overflow(buffer, a->length, a->mode);
1162 case irms_float_number:
1163 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1165 fc_neg(a->value, NULL);
1166 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1179 tarval *tarval_and(tarval *a, tarval *b)
1184 assert(a->mode == b->mode);
1186 /* works even for vector modes */
1188 switch(get_mode_sort(a->mode))
1190 case irms_internal_boolean:
1191 return (a == tarval_b_false) ? a : b;
1193 case irms_int_number:
1194 sc_and(a->value, b->value, NULL);
1195 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1198 assert(0 && "operation not defined on mode");
1206 tarval *tarval_or (tarval *a, tarval *b)
1211 assert(a->mode == b->mode);
1213 /* works even for vector modes */
1215 switch (get_mode_sort(a->mode))
1217 case irms_internal_boolean:
1218 return (a == tarval_b_true) ? a : b;
1220 case irms_int_number:
1221 sc_or(a->value, b->value, NULL);
1222 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1225 assert(0 && "operation not defined on mode");
1231 * bitwise exclusive or (xor)
1233 tarval *tarval_eor(tarval *a, tarval *b)
1238 assert((a->mode == b->mode));
1240 /* works even for vector modes */
1242 switch (get_mode_sort(a->mode))
1244 case irms_internal_boolean:
1245 return (a == b)? tarval_b_false : tarval_b_true;
1247 case irms_int_number:
1248 sc_xor(a->value, b->value, NULL);
1249 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1252 assert(0 && "operation not defined on mode");
1258 * bitwise left shift
1260 tarval *tarval_shl(tarval *a, tarval *b)
1262 char *temp_val = NULL;
1266 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1268 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1269 /* vector arithmetic not implemented yet */
1273 if (get_mode_modulo_shift(a->mode) != 0)
1275 temp_val = alloca(sc_get_buffer_length());
1277 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1278 sc_mod(b->value, temp_val, temp_val);
1281 temp_val = (char*)b->value;
1283 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1284 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1288 * bitwise unsigned right shift
1290 tarval *tarval_shr(tarval *a, tarval *b)
1292 char *temp_val = NULL;
1296 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1298 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1299 /* vector arithmetic not implemented yet */
1303 if (get_mode_modulo_shift(a->mode) != 0)
1305 temp_val = alloca(sc_get_buffer_length());
1307 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1308 sc_mod(b->value, temp_val, temp_val);
1311 temp_val = (char*)b->value;
1313 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1314 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1318 * bitwise signed right shift
1320 tarval *tarval_shrs(tarval *a, tarval *b)
1322 char *temp_val = NULL;
1326 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1328 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1329 /* vector arithmetic not implemented yet */
1333 if (get_mode_modulo_shift(a->mode) != 0)
1335 temp_val = alloca(sc_get_buffer_length());
1337 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1338 sc_mod(b->value, temp_val, temp_val);
1341 temp_val = (char*)b->value;
1343 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1344 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1350 tarval *tarval_rot(tarval *a, tarval *b)
1352 char *temp_val = NULL;
1356 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1358 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1359 /* vector arithmetic not implemented yet */
1363 if (get_mode_modulo_shift(a->mode) != 0)
1365 temp_val = alloca(sc_get_buffer_length());
1367 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1368 sc_mod(b->value, temp_val, temp_val);
1371 temp_val = (char*)b->value;
1373 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1374 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1378 * carry flag of the last operation
1380 int tarval_carry(void)
1382 return sc_had_carry();
1388 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1390 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1394 const tarval_mode_info *mode_info;
1395 const char *prefix, *suffix;
1399 mode_info = tv->mode->tv_priv;
1401 mode_info = &default_info;
1402 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1403 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1405 switch (get_mode_sort(tv->mode))
1407 case irms_int_number:
1408 case irms_character:
1409 switch (mode_info->mode_output) {
1412 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1416 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1422 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1425 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1427 case irms_float_number:
1428 switch (mode_info->mode_output) {
1430 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1433 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1438 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1442 case irms_reference:
1443 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1444 if (tv->value != NULL){
1445 if (len > tv->length) {
1446 memcpy(buf, tv->value, tv->length);
1447 buf[tv->length] = '\0';
1451 memcpy(buf, tv->value, len-1);
1457 return snprintf(buf, len, "void");
1459 case irms_internal_boolean:
1460 switch (mode_info->mode_output) {
1466 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1470 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1473 case irms_control_flow:
1475 case irms_auxiliary:
1476 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1484 * Output of tarvals to stdio.
1486 int tarval_printf(tarval *tv) {
1490 res = tarval_snprintf(buf, sizeof(buf), tv);
1491 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1497 char *get_tarval_bitpattern(tarval *tv)
1500 int n = get_mode_size_bits(tv->mode);
1501 int bytes = (n + 7) / 8;
1502 char *res = malloc((n + 1) * sizeof(char));
1505 for(i = 0; i < bytes; i++) {
1506 byte = get_tarval_sub_bits(tv, i);
1507 for(j = 1; j < 256; j <<= 1)
1509 res[pos++] = j & byte ? '1' : '0';
1518 * access to the bitpattern
1520 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1522 switch (get_mode_sort(tv->mode)) {
1523 case irms_int_number:
1524 case irms_character:
1525 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1527 case irms_float_number:
1528 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1536 * Specify the output options of one mode.
1538 * This functions stores the modinfo, so DO NOT DESTROY it.
1540 * Returns zero on success.
1542 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1546 mode->tv_priv = modeinfo;
1551 * Returns the output options of one mode.
1553 * This functions returns the modinfo of a given mode.
1555 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1559 return mode->tv_priv;
1563 * Identifying tarvals values for algebraic simplifications.
1566 * - TV_CLASSIFY_NULL for additive neutral,
1567 * - TV_CLASSIFY_ONE for multiplicative neutral,
1568 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1569 * - TV_CLASSIFY_OTHER else
1571 tarval_classification_t classify_tarval(tarval *tv)
1574 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1576 if (tv == get_mode_null(tv->mode))
1577 return TV_CLASSIFY_NULL;
1578 else if (tv == get_mode_one(tv->mode))
1579 return TV_CLASSIFY_ONE;
1580 else if ((get_mode_sort(tv->mode) == irms_int_number)
1581 && (tv == new_tarval_from_long(-1, tv->mode)))
1582 return TV_CLASSIFY_ALL_ONE;
1584 return TV_CLASSIFY_OTHER;
1588 * Sets the overflow mode for integer operations.
1590 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1591 int_overflow_mode = ov_mode;
1595 * Get the overflow mode for integer operations.
1597 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1598 return int_overflow_mode;
1602 * default mode_info for output as HEX
1604 static const tarval_mode_info hex_output = {
1611 * default mode_info for output as reference
1613 static const tarval_mode_info reference_output = {
1621 * Initialization of the tarval module: called before init_mode()
1623 void init_tarval_1(void)
1626 /* initialize the sets holding the tarvals with a comparison function and
1627 * an initial size, which is the expected number of constants */
1628 tarvals = new_set(memcmp, N_CONSTANTS);
1629 values = new_set(memcmp, N_CONSTANTS);
1630 /* init strcalc with precision of 68 to support floating point values with 64
1631 * bit mantissa (needs extra bits for rounding and overflow) */
1637 * Initialization of the tarval module: called after init_mode()
1639 void init_tarval_2(void)
1643 tarval_bad->mode = mode_BAD;
1644 tarval_undefined->mode = mode_ANY;
1645 tarval_b_true->mode = mode_b;
1646 tarval_b_false->mode = mode_b;
1647 tarval_P_void->mode = mode_P;
1650 * assign output modes that are compatible with the
1651 * old implementation: Hex output
1653 set_tarval_mode_output_option(mode_U, &hex_output);
1654 set_tarval_mode_output_option(mode_C, &hex_output);
1655 set_tarval_mode_output_option(mode_Bs, &hex_output);
1656 set_tarval_mode_output_option(mode_Bu, &hex_output);
1657 set_tarval_mode_output_option(mode_Hs, &hex_output);
1658 set_tarval_mode_output_option(mode_Hu, &hex_output);
1659 set_tarval_mode_output_option(mode_Is, &hex_output);
1660 set_tarval_mode_output_option(mode_Iu, &hex_output);
1661 set_tarval_mode_output_option(mode_Ls, &hex_output);
1662 set_tarval_mode_output_option(mode_Lu, &hex_output);
1663 set_tarval_mode_output_option(mode_P, &reference_output);
1666 /* free all memory occupied by tarval. */
1667 void finish_tarval(void) {
1670 del_set(tarvals); tarvals = NULL;
1671 del_set(values); values = NULL;
1674 /****************************************************************************
1676 ****************************************************************************/