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() */
31 #include <string.h> /* nice things for strings */
33 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
44 #include "set.h" /* to store tarvals in */
45 /* #include "tune.h" */ /* some constants */
46 #include "entity_t.h" /* needed to store pointers to entities */
48 #include "irnode.h" /* defines boolean return values (pnc_number)*/
53 /** Size of hash tables. Should correspond to average number of distinct constant
55 #define N_CONSTANTS 2048
57 /* XXX hack until theres's a proper interface */
61 #define GET_OVERFLOW_MODE() BAD
63 /* unused, float to int doesn't work yet */
66 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
68 #define SWITCH_NOINFINITY 0
69 #define SWITCH_NODENORMALS 0
71 /****************************************************************************
72 * local definitions and macros
73 ****************************************************************************/
75 # define TARVAL_VERIFY(a) tarval_verify((a))
77 # define TARVAL_VERIFY(a) ((void)0)
80 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
81 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
83 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
84 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
86 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
88 static long long count = 0;
89 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
91 # define ANNOUNCE() ((void)0)
93 /****************************************************************************
95 ****************************************************************************/
96 static struct set *tarvals; /* container for tarval structs */
97 static struct set *values; /* container for values */
99 /****************************************************************************
101 ****************************************************************************/
103 static int hash_val(const void *value, unsigned int length);
104 static int hash_tv(tarval *tv);
105 static void _fail_verify(tarval *tv, const char* file, int line)
107 /* print a memory image of the tarval and throw an assertion */
109 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
111 printf("%s:%d: Invalid tarval (null)", file, line);
115 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
118 INLINE static void tarval_verify(tarval *tv)
124 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
125 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
127 if (!FIND_TARVAL(tv)) fail_verify(tv);
128 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
134 static int hash_tv(tarval *tv)
136 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
139 static int hash_val(const void *value, unsigned int length)
142 unsigned int hash = 0;
144 /* scramble the byte - array */
145 for (i = 0; i < length; i++)
147 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
148 hash += (hash << 11) ^ (hash >> 17);
154 /* finds tarval with value/mode or creates new tarval */
155 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
162 /* if there already is such a value, it is returned, else value
163 * is copied into the set */
164 tv.value = INSERT_VALUE(value, length);
168 /* if there is such a tarval, it is returned, else tv is copied
170 return (tarval *)INSERT_TARVAL(&tv);
173 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
175 switch (get_mode_sort(mode))
177 case irms_int_number:
178 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
179 switch (GET_OVERFLOW_MODE()) {
181 return get_mode_max(mode);
184 char *temp = alloca(sc_get_buffer_length());
185 char *diff = alloca(sc_get_buffer_length());
186 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
187 sc_val_from_ulong(1, temp);
188 sc_add(diff, temp, diff);
189 sc_sub(value, diff, temp);
190 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
191 sc_sub(temp, diff, temp);
192 return get_tarval(temp, length, mode);
197 return get_tarval(value, length, mode);
200 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
201 switch (GET_OVERFLOW_MODE()) {
203 return get_mode_min(mode);
206 char *temp = alloca(sc_get_buffer_length());
207 char *diff = alloca(sc_get_buffer_length());
208 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
209 sc_val_from_ulong(1, temp);
210 sc_add(diff, temp, diff);
211 sc_add(value, diff, temp);
212 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
213 sc_add(temp, diff, temp);
214 return get_tarval(temp, length, mode);
219 return get_tarval(value, length, mode);
224 case irms_float_number:
225 if (SWITCH_NOINFINITY && fc_is_inf(value))
227 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
230 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
232 return get_mode_null(mode);
238 return get_tarval(value, length, mode);
243 * public variables declared in tv.h
245 static tarval reserved_tv[5];
247 tarval *tarval_bad = &reserved_tv[0];
248 tarval *tarval_undefined = &reserved_tv[1];
249 tarval *tarval_b_false = &reserved_tv[2];
250 tarval *tarval_b_true = &reserved_tv[3];
251 tarval *tarval_P_void = &reserved_tv[4];
254 * public functions declared in tv.h
258 * Constructors =============================================================
260 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
267 switch (get_mode_sort(mode))
269 case irms_control_flow:
275 case irms_internal_boolean:
276 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
277 if (strcasecmp(str, "true")) return tarval_b_true;
278 else if (strcasecmp(str, "false")) return tarval_b_true;
280 /* XXX This is C semantics */
281 return atoi(str) ? tarval_b_true : tarval_b_false;
283 case irms_float_number:
284 switch(get_mode_size_bits(mode)) {
286 fc_val_from_str(str, len, 8, 23, NULL);
289 fc_val_from_str(str, len, 11, 52, NULL);
292 fc_val_from_str(str, len, 15, 64, NULL);
295 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
297 case irms_int_number:
299 sc_val_from_str(str, len, NULL);
300 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
303 return get_tarval(str, len, mode);
306 assert(0); /* can't be reached, can it? */
311 * helper function, create a tarval from long
313 tarval *new_tarval_from_long(long l, ir_mode *mode)
316 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
318 switch(get_mode_sort(mode))
320 case irms_internal_boolean:
321 /* XXX C semantics ! */
322 return l ? tarval_b_true : tarval_b_false ;
324 case irms_int_number:
326 sc_val_from_long(l, NULL);
327 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
329 case irms_float_number:
330 return new_tarval_from_double((long double)l, mode);
333 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
341 /* returns non-zero if can be converted to long */
342 int tarval_is_long(tarval *tv)
345 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
347 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
349 /* the value might be too big to fit in a long */
350 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
351 if (sc_comp(sc_get_buffer(), tv->value) == -1)
353 /* really doesn't fit */
360 /* this might overflow the machine's long, so use only with small values */
361 long get_tarval_long(tarval* tv)
364 assert(tarval_is_long(tv) && "tarval too big to fit in long");
366 return sc_val_to_long(tv->value);
369 tarval *new_tarval_from_double(long double d, ir_mode *mode)
372 assert(mode && (get_mode_sort(mode) == irms_float_number));
374 switch (get_mode_size_bits(mode)) {
376 fc_val_from_float(d, 8, 23, NULL);
379 fc_val_from_float(d, 11, 52, NULL);
382 fc_val_from_float(d, 15, 64, NULL);
385 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
388 /* returns non-zero if can be converted to double */
389 int tarval_is_double(tarval *tv)
394 return (get_mode_sort(tv->mode) == irms_float_number);
397 long double get_tarval_double(tarval *tv)
400 assert(tarval_is_double(tv));
402 return fc_val_to_float(tv->value);
407 * Access routines for tarval fields ========================================
409 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
417 * Special value query functions ============================================
419 * These functions calculate and return a tarval representing the requested
421 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
422 * functions, but these are stored on initialization of the irmode module and
423 * therefore the irmode functions should be prefered to the functions below.
426 tarval *get_tarval_bad(void)
431 tarval *get_tarval_undefined(void)
434 return tarval_undefined;
436 tarval *get_tarval_b_false(void)
439 return tarval_b_false;
441 tarval *get_tarval_b_true(void)
444 return tarval_b_true;
446 tarval *get_tarval_P_void(void)
449 return tarval_P_void;
452 tarval *get_tarval_max(ir_mode *mode)
457 if (get_mode_n_vector_elems(mode) > 1) {
458 /* vector arithmetic not implemented yet */
462 switch(get_mode_sort(mode))
465 case irms_control_flow:
471 case irms_internal_boolean:
472 return tarval_b_true;
474 case irms_float_number:
475 switch(get_mode_size_bits(mode))
478 fc_get_max(8, 23, NULL);
481 fc_get_max(11, 52, NULL);
484 fc_get_max(15, 64, NULL);
487 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
489 case irms_int_number:
491 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
492 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
497 tarval *get_tarval_min(ir_mode *mode)
502 if (get_mode_n_vector_elems(mode) > 1) {
503 /* vector arithmetic not implemented yet */
507 switch(get_mode_sort(mode))
510 case irms_control_flow:
516 case irms_internal_boolean:
517 return tarval_b_false;
519 case irms_float_number:
520 switch(get_mode_size_bits(mode))
523 fc_get_min(8, 23, NULL);
526 fc_get_min(11, 52, NULL);
529 fc_get_min(15, 64, NULL);
532 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
534 case irms_int_number:
536 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
537 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
542 tarval *get_tarval_null(ir_mode *mode)
547 if (get_mode_n_vector_elems(mode) > 1) {
548 /* vector arithmetic not implemented yet */
552 switch(get_mode_sort(mode))
554 case irms_control_flow:
557 case irms_internal_boolean:
561 case irms_float_number:
562 return new_tarval_from_double(0.0, mode);
564 case irms_int_number:
566 return new_tarval_from_long(0l, mode);
569 return tarval_P_void;
574 tarval *get_tarval_one(ir_mode *mode)
579 if (get_mode_n_vector_elems(mode) > 1) {
580 /* vector arithmetic not implemented yet */
584 switch(get_mode_sort(mode))
586 case irms_control_flow:
589 case irms_internal_boolean:
594 case irms_float_number:
595 return new_tarval_from_double(1.0, mode);
597 case irms_int_number:
599 return new_tarval_from_long(1l, mode);
605 tarval *get_tarval_nan(ir_mode *mode)
610 if (get_mode_n_vector_elems(mode) > 1) {
611 /* vector arithmetic not implemented yet */
615 if (get_mode_sort(mode) == irms_float_number) {
616 switch(get_mode_size_bits(mode))
619 fc_get_qnan(8, 23, NULL);
622 fc_get_qnan(11, 52, NULL);
625 fc_get_qnan(15, 64, NULL);
628 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
631 assert(0 && "tarval is not floating point");
636 tarval *get_tarval_inf(ir_mode *mode)
641 if (get_mode_n_vector_elems(mode) > 1) {
642 /* vector arithmetic not implemented yet */
646 if (get_mode_sort(mode) == irms_float_number) {
647 switch(get_mode_size_bits(mode))
650 fc_get_plusinf(8, 23, NULL);
653 fc_get_plusinf(11, 52, NULL);
656 fc_get_plusinf(15, 64, NULL);
659 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
662 assert(0 && "tarval is not floating point");
668 * Arithmethic operations on tarvals ========================================
672 * test if negative number, 1 means 'yes'
674 int tarval_is_negative(tarval *a)
679 if (get_mode_n_vector_elems(a->mode) > 1) {
680 /* vector arithmetic not implemented yet */
681 assert(0 && "tarval_is_negative is not allowed for vector modes");
685 switch (get_mode_sort(a->mode))
687 case irms_int_number:
688 if (!mode_is_signed(a->mode)) return 0;
690 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
692 case irms_float_number:
693 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
696 assert(0 && "not implemented");
702 * test if null, 1 means 'yes'
704 int tarval_is_null(tarval *a)
706 ir_mode *m = get_tarval_mode(a);
708 return a == get_tarval_null(m);
712 * test if one, 1 means 'yes'
714 int tarval_is_one(tarval *a)
716 ir_mode *m = get_tarval_mode(a);
718 return a == get_tarval_one(m);
724 pnc_number tarval_cmp(tarval *a, tarval *b)
730 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
731 if (a == tarval_undefined || b == tarval_undefined) return False;
732 if (a == b) return Eq;
733 if (a->mode != b->mode) return False;
735 if (get_mode_n_vector_elems(a->mode) > 1) {
736 /* vector arithmetic not implemented yet */
737 assert(0 && "cmp not implemented for vector modes");
740 /* Here the two tarvals are unequal and of the same mode */
741 switch (get_mode_sort(a->mode))
743 case irms_control_flow:
749 case irms_float_number:
750 switch (fc_comp(a->value, b->value)) {
752 case 0: assert(0 && "different tarvals compare equal"); return Eq;
755 default: return False;
757 case irms_int_number:
759 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
761 case irms_internal_boolean:
762 return (a == tarval_b_true)?(Gt):(Lt);
768 * convert to other mode
770 tarval *tarval_convert_to(tarval *src, ir_mode *m)
778 if (src->mode == m) return src;
780 if (get_mode_n_vector_elems(src->mode) > 1) {
781 /* vector arithmetic not implemented yet */
785 switch (get_mode_sort(src->mode))
787 case irms_control_flow:
792 /* cast float to something */
793 case irms_float_number:
794 switch (get_mode_sort(m)) {
795 case irms_float_number:
796 switch (get_mode_size_bits(m))
799 fc_cast(src->value, 8, 23, NULL);
802 fc_cast(src->value, 11, 52, NULL);
805 fc_cast(src->value, 15, 64, NULL);
810 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
813 case irms_int_number:
814 switch (GET_FLOAT_TO_INT_MODE())
817 fc_int(src->value, NULL);
820 fc_rnd(src->value, NULL);
825 /* XXX floating point unit can't produce a value in integer
827 * an intermediate representation is needed here first. */
828 /* return get_tarval(); */
833 /* the rest can't be converted */
838 /* cast int to something */
839 case irms_int_number:
840 switch (get_mode_sort(m)) {
841 case irms_int_number:
843 return get_tarval_overflow(src->value, src->length, m);
845 case irms_internal_boolean:
846 /* XXX C semantics */
847 if (src == get_mode_null(src->mode)) return tarval_b_false;
848 else return tarval_b_true;
850 case irms_float_number:
851 /* XXX floating point unit does not understand internal integer
852 * representation, convert to string first, then create float from
854 buffer = alloca(100);
855 /* decimal string representation because hexadecimal output is
856 * interpreted unsigned by fc_val_from_str, so this is a HACK */
857 snprintf(buffer, 100, "%s",
858 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
859 switch (get_mode_size_bits(m))
862 fc_val_from_str(buffer, 0, 8, 23, NULL);
865 fc_val_from_str(buffer, 0, 11, 52, NULL);
868 fc_val_from_str(buffer, 0, 15, 64, NULL);
871 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
878 case irms_internal_boolean:
879 switch (get_mode_sort(m))
881 case irms_int_number:
882 if (src == tarval_b_true) return get_mode_one(m);
883 else return get_mode_null(m);
902 tarval *tarval_not(tarval *a)
908 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
910 /* works for vector mode without changes */
912 switch (get_mode_sort(a->mode))
914 case irms_int_number:
915 buffer = alloca(sc_get_buffer_length());
916 sc_not(a->value, buffer);
917 return get_tarval(buffer, a->length, a->mode);
925 * arithmetic negation
927 tarval *tarval_neg(tarval *a)
933 assert(mode_is_num(a->mode)); /* negation only for numerical values */
934 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
936 if (get_mode_n_vector_elems(a->mode) > 1) {
937 /* vector arithmetic not implemented yet */
941 switch (get_mode_sort(a->mode))
943 case irms_int_number:
944 buffer = alloca(sc_get_buffer_length());
945 sc_neg(a->value, buffer);
946 return get_tarval_overflow(buffer, a->length, a->mode);
948 case irms_float_number:
949 fc_neg(a->value, NULL);
950 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
960 tarval *tarval_add(tarval *a, tarval *b)
967 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
969 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
970 /* vector arithmetic not implemented yet */
974 switch (get_mode_sort(a->mode))
977 case irms_int_number:
978 /* modes of a,b are equal, so result has mode of a as this might be the character */
979 buffer = alloca(sc_get_buffer_length());
980 sc_add(a->value, b->value, buffer);
981 return get_tarval_overflow(buffer, a->length, a->mode);
983 case irms_float_number:
984 fc_add(a->value, b->value, NULL);
985 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
995 tarval *tarval_sub(tarval *a, tarval *b)
1002 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
1004 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1005 /* vector arithmetic not implemented yet */
1008 switch (get_mode_sort(a->mode))
1010 case irms_character:
1011 case irms_int_number:
1012 /* modes of a,b are equal, so result has mode of a as this might be the character */
1013 buffer = alloca(sc_get_buffer_length());
1014 sc_sub(a->value, b->value, buffer);
1015 return get_tarval_overflow(buffer, a->length, a->mode);
1017 case irms_float_number:
1018 fc_sub(a->value, b->value, NULL);
1019 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1029 tarval *tarval_mul(tarval *a, tarval *b)
1036 assert((a->mode == b->mode) && mode_is_num(a->mode));
1038 if (get_mode_n_vector_elems(a->mode) > 1) {
1039 /* vector arithmetic not implemented yet */
1043 switch (get_mode_sort(a->mode))
1045 case irms_int_number:
1046 /* modes of a,b are equal */
1047 buffer = alloca(sc_get_buffer_length());
1048 sc_mul(a->value, b->value, buffer);
1049 return get_tarval_overflow(buffer, a->length, a->mode);
1051 case irms_float_number:
1052 fc_mul(a->value, b->value, NULL);
1053 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1061 * floating point division
1063 tarval *tarval_quo(tarval *a, tarval *b)
1068 assert((a->mode == b->mode) && mode_is_float(a->mode));
1070 if (get_mode_n_vector_elems(a->mode) > 1) {
1071 /* vector arithmetic not implemented yet */
1075 fc_div(a->value, b->value, NULL);
1076 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1081 * overflow is impossible, but look out for division by zero
1083 tarval *tarval_div(tarval *a, tarval *b)
1088 assert((a->mode == b->mode) && mode_is_int(a->mode));
1090 if (get_mode_n_vector_elems(a->mode) > 1) {
1091 /* vector arithmetic not implemented yet */
1096 if (b == get_mode_null(b->mode)) return tarval_bad;
1097 /* modes of a,b are equal */
1098 sc_div(a->value, b->value, NULL);
1099 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1104 * overflow is impossible, but look out for division by zero
1106 tarval *tarval_mod(tarval *a, tarval *b)
1111 assert((a->mode == b->mode) && mode_is_int(a->mode));
1113 if (get_mode_n_vector_elems(a->mode) > 1) {
1114 /* vector arithmetic not implemented yet */
1119 if (b == get_mode_null(b->mode)) return tarval_bad;
1120 /* modes of a,b are equal */
1121 sc_mod(a->value, b->value, NULL);
1122 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1128 tarval *tarval_abs(tarval *a)
1134 assert(mode_is_num(a->mode));
1136 if (get_mode_n_vector_elems(a->mode) > 1) {
1137 /* vector arithmetic not implemented yet */
1141 switch (get_mode_sort(a->mode))
1143 case irms_int_number:
1144 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1146 buffer = alloca(sc_get_buffer_length());
1147 sc_neg(a->value, buffer);
1148 return get_tarval_overflow(buffer, a->length, a->mode);
1152 case irms_float_number:
1153 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1155 fc_neg(a->value, NULL);
1156 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1169 tarval *tarval_and(tarval *a, tarval *b)
1174 assert(a->mode == b->mode);
1176 /* works even for vector modes */
1178 switch(get_mode_sort(a->mode))
1180 case irms_internal_boolean:
1181 return (a == tarval_b_false) ? a : b;
1183 case irms_int_number:
1184 sc_and(a->value, b->value, NULL);
1185 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1188 assert(0 && "operation not defined on mode");
1196 tarval *tarval_or (tarval *a, tarval *b)
1201 assert(a->mode == b->mode);
1203 /* works even for vector modes */
1205 switch (get_mode_sort(a->mode))
1207 case irms_internal_boolean:
1208 return (a == tarval_b_true) ? a : b;
1210 case irms_int_number:
1211 sc_or(a->value, b->value, NULL);
1212 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1215 assert(0 && "operation not defined on mode");
1221 * bitwise exclusive or (xor)
1223 tarval *tarval_eor(tarval *a, tarval *b)
1228 assert((a->mode == b->mode));
1230 /* works even for vector modes */
1232 switch (get_mode_sort(a->mode))
1234 case irms_internal_boolean:
1235 return (a == b)? tarval_b_false : tarval_b_true;
1237 case irms_int_number:
1238 sc_xor(a->value, b->value, NULL);
1239 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1242 assert(0 && "operation not defined on mode");
1248 * bitwise left shift
1250 tarval *tarval_shl(tarval *a, tarval *b)
1252 char *temp_val = NULL;
1256 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1258 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1259 /* vector arithmetic not implemented yet */
1263 if (get_mode_modulo_shift(a->mode) != 0)
1265 temp_val = alloca(sc_get_buffer_length());
1267 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1268 sc_mod(b->value, temp_val, temp_val);
1271 temp_val = (char*)b->value;
1273 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1274 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1278 * bitwise unsigned right shift
1280 tarval *tarval_shr(tarval *a, tarval *b)
1282 char *temp_val = NULL;
1286 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1288 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1289 /* vector arithmetic not implemented yet */
1293 if (get_mode_modulo_shift(a->mode) != 0)
1295 temp_val = alloca(sc_get_buffer_length());
1297 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1298 sc_mod(b->value, temp_val, temp_val);
1301 temp_val = (char*)b->value;
1303 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1304 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1308 * bitwise signed right shift
1310 tarval *tarval_shrs(tarval *a, tarval *b)
1312 char *temp_val = NULL;
1316 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1318 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1319 /* vector arithmetic not implemented yet */
1323 if (get_mode_modulo_shift(a->mode) != 0)
1325 temp_val = alloca(sc_get_buffer_length());
1327 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1328 sc_mod(b->value, temp_val, temp_val);
1331 temp_val = (char*)b->value;
1333 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1334 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1340 tarval *tarval_rot(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_rot(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);
1371 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1373 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1377 const tarval_mode_info *mode_info;
1378 const char *prefix, *suffix;
1382 mode_info = tv->mode->tv_priv;
1384 mode_info = &default_info;
1385 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1386 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1388 switch (get_mode_sort(tv->mode))
1390 case irms_int_number:
1391 case irms_character:
1392 switch (mode_info->mode_output) {
1395 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1399 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1405 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1408 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1410 case irms_float_number:
1411 switch (mode_info->mode_output) {
1413 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1416 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1421 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1425 case irms_reference:
1426 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1427 if (tv->value != NULL){
1428 if (len > tv->length) {
1429 memcpy(buf, tv->value, tv->length);
1430 buf[tv->length] = '\0';
1434 memcpy(buf, tv->value, len-1);
1440 return snprintf(buf, len, "void");
1442 case irms_internal_boolean:
1443 switch (mode_info->mode_output) {
1449 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1453 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1456 case irms_control_flow:
1458 case irms_auxiliary:
1459 return snprintf(buf, len, "<BAD>");
1467 * Output of tarvals to stdio.
1469 int tarval_printf(tarval *tv) {
1473 res = tarval_snprintf(buf, sizeof(buf), tv);
1474 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1480 char *get_tarval_bitpattern(tarval *tv)
1483 int n = get_mode_size_bits(tv->mode);
1484 int bytes = n / 8 + ((n & 7) != 0);
1485 char *res = malloc((n + 1) * sizeof(char));
1488 for(i = 0; i < bytes; i++) {
1489 byte = get_tarval_sub_bits(tv, i);
1490 for(j = 1; j < 256; j <<= 1)
1492 res[pos++] = j & byte ? '1' : '0';
1501 * access to the bitpattern
1503 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1505 switch (get_mode_sort(tv->mode)) {
1506 case irms_int_number:
1507 case irms_character:
1508 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1510 case irms_float_number:
1511 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1519 * Specify the output options of one mode.
1521 * This functions stores the modinfo, so DO NOT DESTROY it.
1523 * Returns zero on success.
1525 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1529 mode->tv_priv = modeinfo;
1534 * Returns the output options of one mode.
1536 * This functions returns the modinfo of a given mode.
1538 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1542 return mode->tv_priv;
1546 * Identifying tarvals values for algebraic simplifications.
1549 * - TV_CLASSIFY_NULL for additive neutral,
1550 * - TV_CLASSIFY_ONE for multiplicative neutral,
1551 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1552 * - TV_CLASSIFY_OTHER else
1554 tarval_classification_t classify_tarval(tarval *tv)
1557 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1559 if (tv == get_mode_null(tv->mode))
1560 return TV_CLASSIFY_NULL;
1561 else if (tv == get_mode_one(tv->mode))
1562 return TV_CLASSIFY_ONE;
1563 else if ((get_mode_sort(tv->mode) == irms_int_number)
1564 && (tv == new_tarval_from_long(-1, tv->mode)))
1565 return TV_CLASSIFY_ALL_ONE;
1567 return TV_CLASSIFY_OTHER;
1571 * default mode_info for output as HEX
1573 static const tarval_mode_info hex_output = {
1580 * default mode_info for output as reference
1582 static const tarval_mode_info reference_output = {
1590 * Initialization of the tarval module: called before init_mode()
1592 void init_tarval_1(void)
1595 /* initialize the sets holding the tarvals with a comparison function and
1596 * an initial size, which is the expected number of constants */
1597 tarvals = new_set(memcmp, N_CONSTANTS);
1598 values = new_set(memcmp, N_CONSTANTS);
1599 /* init strcalc with precision of 68 to support floating point values with 64
1600 * bit mantissa (needs extra bits for rounding and overflow) */
1606 * Initialization of the tarval module: called after init_mode()
1608 void init_tarval_2(void)
1612 tarval_bad->mode = mode_BAD;
1613 tarval_undefined->mode = mode_ANY;
1614 tarval_b_true->mode = mode_b;
1615 tarval_b_false->mode = mode_b;
1616 tarval_P_void->mode = mode_P;
1619 * assign output modes that are compatible with the
1620 * old implementation: Hex output
1622 set_tarval_mode_output_option(mode_U, &hex_output);
1623 set_tarval_mode_output_option(mode_C, &hex_output);
1624 set_tarval_mode_output_option(mode_Bs, &hex_output);
1625 set_tarval_mode_output_option(mode_Bu, &hex_output);
1626 set_tarval_mode_output_option(mode_Hs, &hex_output);
1627 set_tarval_mode_output_option(mode_Hu, &hex_output);
1628 set_tarval_mode_output_option(mode_Is, &hex_output);
1629 set_tarval_mode_output_option(mode_Iu, &hex_output);
1630 set_tarval_mode_output_option(mode_Ls, &hex_output);
1631 set_tarval_mode_output_option(mode_Lu, &hex_output);
1632 set_tarval_mode_output_option(mode_P, &reference_output);
1635 /* free all memory occupied by tarval. */
1636 void finish_tarval(void) {
1639 del_set(tarvals); tarvals = NULL;
1640 del_set(values); values = NULL;
1643 /****************************************************************************
1645 ****************************************************************************/