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 */
47 #include "irmode.h" /* defines modes etc */
49 #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 /* XXX hack until theres's a proper interface */
62 #define GET_OVERFLOW_MODE() BAD
64 /* unused, float to int doesn't work yet */
67 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
69 #define SWITCH_NOINFINITY 0
70 #define SWITCH_NODENORMALS 0
72 /****************************************************************************
73 * local definitions and macros
74 ****************************************************************************/
76 # define TARVAL_VERIFY(a) tarval_verify((a))
78 # define TARVAL_VERIFY(a) ((void)0)
81 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
82 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
84 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
85 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
87 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
89 static long long count = 0;
90 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
92 # define ANNOUNCE() ((void)0)
94 /****************************************************************************
96 ****************************************************************************/
97 static struct set *tarvals; /* container for tarval structs */
98 static struct set *values; /* container for values */
100 /****************************************************************************
102 ****************************************************************************/
104 static int hash_val(const void *value, unsigned int length);
105 static int hash_tv(tarval *tv);
106 static void _fail_verify(tarval *tv, const char* file, int line)
108 /* print a memory image of the tarval and throw an assertion */
110 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
112 printf("%s:%d: Invalid tarval (null)", file, line);
116 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
119 INLINE static void tarval_verify(tarval *tv)
125 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
126 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
128 if (!FIND_TARVAL(tv)) fail_verify(tv);
129 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
135 static int hash_tv(tarval *tv)
137 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
140 static int hash_val(const void *value, unsigned int length)
143 unsigned int hash = 0;
145 /* scramble the byte - array */
146 for (i = 0; i < length; i++)
148 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
149 hash += (hash << 11) ^ (hash >> 17);
155 /* finds tarval with value/mode or creates new tarval */
156 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
163 /* if there already is such a value, it is returned, else value
164 * is copied into the set */
165 tv.value = INSERT_VALUE(value, length);
169 /* if there is such a tarval, it is returned, else tv is copied
171 return (tarval *)INSERT_TARVAL(&tv);
174 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
176 switch (get_mode_sort(mode))
178 case irms_int_number:
179 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
180 switch (GET_OVERFLOW_MODE()) {
182 return get_mode_max(mode);
185 char *temp = alloca(sc_get_buffer_length());
186 char *diff = alloca(sc_get_buffer_length());
187 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
188 sc_val_from_ulong(1, temp);
189 sc_add(diff, temp, diff);
190 sc_sub(value, diff, temp);
191 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
192 sc_sub(temp, diff, temp);
193 return get_tarval(temp, length, mode);
198 return get_tarval(value, length, mode);
201 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
202 switch (GET_OVERFLOW_MODE()) {
204 return get_mode_min(mode);
207 char *temp = alloca(sc_get_buffer_length());
208 char *diff = alloca(sc_get_buffer_length());
209 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
210 sc_val_from_ulong(1, temp);
211 sc_add(diff, temp, diff);
212 sc_add(value, diff, temp);
213 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
214 sc_add(temp, diff, temp);
215 return get_tarval(temp, length, mode);
220 return get_tarval(value, length, mode);
225 case irms_float_number:
226 if (SWITCH_NOINFINITY && fc_is_inf(value))
228 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
231 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
233 return get_mode_null(mode);
239 return get_tarval(value, length, mode);
244 * public variables declared in tv.h
246 static tarval reserved_tv[5];
248 tarval *tarval_bad = &reserved_tv[0];
249 tarval *tarval_undefined = &reserved_tv[1];
250 tarval *tarval_b_false = &reserved_tv[2];
251 tarval *tarval_b_true = &reserved_tv[3];
252 tarval *tarval_P_void = &reserved_tv[4];
255 * public functions declared in tv.h
259 * Constructors =============================================================
261 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
268 switch (get_mode_sort(mode))
270 case irms_control_flow:
276 case irms_internal_boolean:
277 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
278 if (strcasecmp(str, "true")) return tarval_b_true;
279 else if (strcasecmp(str, "false")) return tarval_b_true;
281 /* XXX This is C semantics */
282 return atoi(str) ? tarval_b_true : tarval_b_false;
284 case irms_float_number:
285 switch(get_mode_size_bits(mode)) {
287 fc_val_from_str(str, len, 8, 23, NULL);
290 fc_val_from_str(str, len, 11, 52, NULL);
293 fc_val_from_str(str, len, 15, 64, NULL);
296 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
298 case irms_int_number:
300 sc_val_from_str(str, len, NULL);
301 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
304 return get_tarval(str, len, mode);
307 assert(0); /* can't be reached, can it? */
312 * helper function, create a tarval from long
314 tarval *new_tarval_from_long(long l, ir_mode *mode)
317 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
319 switch(get_mode_sort(mode))
321 case irms_internal_boolean:
322 /* XXX C semantics ! */
323 return l ? tarval_b_true : tarval_b_false ;
325 case irms_int_number:
327 sc_val_from_long(l, NULL);
328 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
330 case irms_float_number:
331 return new_tarval_from_double((long double)l, mode);
334 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
342 /* returns non-zero if can be converted to long */
343 int tarval_is_long(tarval *tv)
346 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
348 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
350 /* the value might be too big to fit in a long */
351 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
352 if (sc_comp(sc_get_buffer(), tv->value) == -1)
354 /* really doesn't fit */
361 /* this might overflow the machine's long, so use only with small values */
362 long tarval_to_long(tarval* tv)
365 assert(tarval_is_long(tv) && "tarval too big to fit in long");
367 return sc_val_to_long(tv->value);
370 tarval *new_tarval_from_double(long double d, ir_mode *mode)
373 assert(mode && (get_mode_sort(mode) == irms_float_number));
375 switch (get_mode_size_bits(mode)) {
377 fc_val_from_float(d, 8, 23, NULL);
380 fc_val_from_float(d, 11, 52, NULL);
383 fc_val_from_float(d, 15, 64, NULL);
386 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
389 /* returns non-zero if can be converted to double */
390 int tarval_is_double(tarval *tv)
395 return (get_mode_sort(tv->mode) == irms_float_number);
398 long double tarval_to_double(tarval *tv)
401 assert(tarval_is_double(tv));
403 return fc_val_to_float(tv->value);
406 /* The tarval represents the address of the entity. As the address must
407 be constant the entity must have as owner the global type. */
408 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
412 assert(mode && (get_mode_sort(mode) == irms_reference));
414 return get_tarval((void *)ent, 0, mode);
416 int tarval_is_entity(tarval *tv)
420 /* tv->value == NULL means dereferencing a null pointer */
421 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
422 && (tv != tarval_P_void));
425 entity *tarval_to_entity(tarval *tv)
430 if (tarval_is_entity(tv))
431 return (entity *)tv->value;
433 assert(0 && "tarval did not represent an entity");
438 void free_tarval_entity(entity *ent) {
439 /* There can be a tarval referencing this entity. Even if the
440 tarval is not used by the code any more, it can still reference
441 the entity as tarvals live indepently of the entity referenced.
442 Further the tarval is hashed into a set. If a hash function
443 evaluation happens to collide with this tarval, we will vrfy that
444 it contains a proper entity and we will crash if the entity is
447 Unluckily, tarvals can neither be changed nor deleted, and to find
448 one, all existing reference modes have to be tried -> a facility
449 to retrieve all modes of a kind is needed. */
454 * Access routines for tarval fields ========================================
456 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
464 * Special value query functions ============================================
466 * These functions calculate and return a tarval representing the requested
468 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
469 * functions, but these are stored on initialization of the irmode module and
470 * therefore the irmode functions should be prefered to the functions below.
473 tarval *get_tarval_bad(void)
478 tarval *get_tarval_undefined(void)
481 return tarval_undefined;
483 tarval *get_tarval_b_false(void)
486 return tarval_b_false;
488 tarval *get_tarval_b_true(void)
491 return tarval_b_true;
493 tarval *get_tarval_P_void(void)
496 return tarval_P_void;
499 tarval *get_tarval_max(ir_mode *mode)
504 if (get_mode_vector_elems(mode) > 1) {
505 /* vector arithmetic not implemented yet */
509 switch(get_mode_sort(mode))
512 case irms_control_flow:
518 case irms_internal_boolean:
519 return tarval_b_true;
521 case irms_float_number:
522 switch(get_mode_size_bits(mode))
525 fc_get_max(8, 23, NULL);
528 fc_get_max(11, 52, NULL);
531 fc_get_max(15, 64, NULL);
534 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
536 case irms_int_number:
538 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
539 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
544 tarval *get_tarval_min(ir_mode *mode)
549 if (get_mode_vector_elems(mode) > 1) {
550 /* vector arithmetic not implemented yet */
554 switch(get_mode_sort(mode))
557 case irms_control_flow:
563 case irms_internal_boolean:
564 return tarval_b_false;
566 case irms_float_number:
567 switch(get_mode_size_bits(mode))
570 fc_get_min(8, 23, NULL);
573 fc_get_min(11, 52, NULL);
576 fc_get_min(15, 64, NULL);
579 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
581 case irms_int_number:
583 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
584 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
589 tarval *get_tarval_null(ir_mode *mode)
594 if (get_mode_vector_elems(mode) > 1) {
595 /* vector arithmetic not implemented yet */
599 switch(get_mode_sort(mode))
601 case irms_control_flow:
604 case irms_internal_boolean:
608 case irms_float_number:
609 return new_tarval_from_double(0.0, mode);
611 case irms_int_number:
613 return new_tarval_from_long(0l, mode);
616 return tarval_P_void;
621 tarval *get_tarval_one(ir_mode *mode)
626 if (get_mode_vector_elems(mode) > 1) {
627 /* vector arithmetic not implemented yet */
631 switch(get_mode_sort(mode))
633 case irms_control_flow:
636 case irms_internal_boolean:
641 case irms_float_number:
642 return new_tarval_from_double(1.0, mode);
644 case irms_int_number:
646 return new_tarval_from_long(1l, mode);
652 tarval *get_tarval_nan(ir_mode *mode)
657 if (get_mode_vector_elems(mode) > 1) {
658 /* vector arithmetic not implemented yet */
662 if (get_mode_sort(mode) == irms_float_number) {
663 switch(get_mode_size_bits(mode))
666 fc_get_qnan(8, 23, NULL);
669 fc_get_qnan(11, 52, NULL);
672 fc_get_qnan(15, 64, NULL);
675 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
678 assert(0 && "tarval is not floating point");
683 tarval *get_tarval_inf(ir_mode *mode)
688 if (get_mode_vector_elems(mode) > 1) {
689 /* vector arithmetic not implemented yet */
693 if (get_mode_sort(mode) == irms_float_number) {
694 switch(get_mode_size_bits(mode))
697 fc_get_plusinf(8, 23, NULL);
700 fc_get_plusinf(11, 52, NULL);
703 fc_get_plusinf(15, 64, NULL);
706 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
709 assert(0 && "tarval is not floating point");
715 * Arithmethic operations on tarvals ========================================
719 * test if negative number, 1 means 'yes'
721 int tarval_is_negative(tarval *a)
726 if (get_mode_vector_elems(a->mode) > 1) {
727 /* vector arithmetic not implemented yet */
728 assert(0 && "tarval_is_negative is not allowed for vector modes");
732 switch (get_mode_sort(a->mode))
734 case irms_int_number:
735 if (!mode_is_signed(a->mode)) return 0;
737 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
739 case irms_float_number:
740 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
743 assert(0 && "not implemented");
749 * test if null, 1 means 'yes'
751 int tarval_is_null(tarval *a)
753 ir_mode *m = get_tarval_mode(a);
755 return a == get_tarval_null(m);
759 * test if one, 1 means 'yes'
761 int tarval_is_one(tarval *a)
763 ir_mode *m = get_tarval_mode(a);
765 return a == get_tarval_one(m);
771 pnc_number tarval_cmp(tarval *a, tarval *b)
777 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
778 if (a == tarval_undefined || b == tarval_undefined) return False;
779 if (a == b) return Eq;
780 if (a->mode != b->mode) return False;
782 if (get_mode_vector_elems(a->mode) > 1) {
783 /* vector arithmetic not implemented yet */
784 assert(0 && "cmp not implemented for vector modes");
787 /* Here the two tarvals are unequal and of the same mode */
788 switch (get_mode_sort(a->mode))
790 case irms_control_flow:
796 case irms_float_number:
797 switch (fc_comp(a->value, b->value)) {
799 case 0: assert(0 && "different tarvals compare equal"); return Eq;
802 default: return False;
804 case irms_int_number:
806 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
808 case irms_internal_boolean:
809 return (a == tarval_b_true)?(Gt):(Lt);
815 * convert to other mode
817 tarval *tarval_convert_to(tarval *src, ir_mode *m)
825 if (src->mode == m) return src;
827 if (get_mode_vector_elems(src->mode) > 1) {
828 /* vector arithmetic not implemented yet */
832 switch (get_mode_sort(src->mode))
834 case irms_control_flow:
839 /* cast float to something */
840 case irms_float_number:
841 switch (get_mode_sort(m)) {
842 case irms_float_number:
843 switch (get_mode_size_bits(m))
846 fc_cast(src->value, 8, 23, NULL);
849 fc_cast(src->value, 11, 52, NULL);
852 fc_cast(src->value, 15, 64, NULL);
857 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
860 case irms_int_number:
861 switch (GET_FLOAT_TO_INT_MODE())
864 fc_int(src->value, NULL);
867 fc_rnd(src->value, NULL);
872 /* XXX floating point unit can't produce a value in integer
874 * an intermediate representation is needed here first. */
875 /* return get_tarval(); */
880 /* the rest can't be converted */
885 /* cast int to something */
886 case irms_int_number:
887 switch (get_mode_sort(m)) {
888 case irms_int_number:
890 return get_tarval_overflow(src->value, src->length, m);
892 case irms_internal_boolean:
893 /* XXX C semantics */
894 if (src == get_mode_null(src->mode)) return tarval_b_false;
895 else return tarval_b_true;
897 case irms_float_number:
898 /* XXX floating point unit does not understand internal integer
899 * representation, convert to string first, then create float from
901 buffer = alloca(100);
902 /* decimal string representation because hexadecimal output is
903 * interpreted unsigned by fc_val_from_str, so this is a HACK */
904 snprintf(buffer, 100, "%s",
905 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
906 switch (get_mode_size_bits(m))
909 fc_val_from_str(buffer, 0, 8, 23, NULL);
912 fc_val_from_str(buffer, 0, 11, 52, NULL);
915 fc_val_from_str(buffer, 0, 15, 64, NULL);
918 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
925 case irms_internal_boolean:
926 switch (get_mode_sort(m))
928 case irms_int_number:
929 if (src == tarval_b_true) return get_mode_one(m);
930 else return get_mode_null(m);
949 tarval *tarval_not(tarval *a)
955 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
957 /* works for vector mode without changes */
959 switch (get_mode_sort(a->mode))
961 case irms_int_number:
962 buffer = alloca(sc_get_buffer_length());
963 sc_not(a->value, buffer);
964 return get_tarval(buffer, a->length, a->mode);
972 * arithmetic negation
974 tarval *tarval_neg(tarval *a)
980 assert(mode_is_num(a->mode)); /* negation only for numerical values */
981 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
983 if (get_mode_vector_elems(a->mode) > 1) {
984 /* vector arithmetic not implemented yet */
988 switch (get_mode_sort(a->mode))
990 case irms_int_number:
991 buffer = alloca(sc_get_buffer_length());
992 sc_neg(a->value, buffer);
993 return get_tarval_overflow(buffer, a->length, a->mode);
995 case irms_float_number:
996 fc_neg(a->value, NULL);
997 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1007 tarval *tarval_add(tarval *a, tarval *b)
1014 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
1016 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(b->mode)) {
1017 /* vector arithmetic not implemented yet */
1021 switch (get_mode_sort(a->mode))
1023 case irms_character:
1024 case irms_int_number:
1025 /* modes of a,b are equal, so result has mode of a as this might be the character */
1026 buffer = alloca(sc_get_buffer_length());
1027 sc_add(a->value, b->value, buffer);
1028 return get_tarval_overflow(buffer, a->length, a->mode);
1030 case irms_float_number:
1031 fc_add(a->value, b->value, NULL);
1032 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1042 tarval *tarval_sub(tarval *a, tarval *b)
1049 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
1051 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(b->mode)) {
1052 /* vector arithmetic not implemented yet */
1055 switch (get_mode_sort(a->mode))
1057 case irms_character:
1058 case irms_int_number:
1059 /* modes of a,b are equal, so result has mode of a as this might be the character */
1060 buffer = alloca(sc_get_buffer_length());
1061 sc_sub(a->value, b->value, buffer);
1062 return get_tarval_overflow(buffer, a->length, a->mode);
1064 case irms_float_number:
1065 fc_sub(a->value, b->value, NULL);
1066 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1076 tarval *tarval_mul(tarval *a, tarval *b)
1083 assert((a->mode == b->mode) && mode_is_num(a->mode));
1085 if (get_mode_vector_elems(a->mode) > 1) {
1086 /* vector arithmetic not implemented yet */
1090 switch (get_mode_sort(a->mode))
1092 case irms_int_number:
1093 /* modes of a,b are equal */
1094 buffer = alloca(sc_get_buffer_length());
1095 sc_mul(a->value, b->value, buffer);
1096 return get_tarval_overflow(buffer, a->length, a->mode);
1098 case irms_float_number:
1099 fc_mul(a->value, b->value, NULL);
1100 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1108 * floating point division
1110 tarval *tarval_quo(tarval *a, tarval *b)
1115 assert((a->mode == b->mode) && mode_is_float(a->mode));
1117 if (get_mode_vector_elems(a->mode) > 1) {
1118 /* vector arithmetic not implemented yet */
1122 fc_div(a->value, b->value, NULL);
1123 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1128 * overflow is impossible, but look out for division by zero
1130 tarval *tarval_div(tarval *a, tarval *b)
1135 assert((a->mode == b->mode) && mode_is_int(a->mode));
1137 if (get_mode_vector_elems(a->mode) > 1) {
1138 /* vector arithmetic not implemented yet */
1143 if (b == get_mode_null(b->mode)) return tarval_bad;
1144 /* modes of a,b are equal */
1145 sc_div(a->value, b->value, NULL);
1146 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1151 * overflow is impossible, but look out for division by zero
1153 tarval *tarval_mod(tarval *a, tarval *b)
1158 assert((a->mode == b->mode) && mode_is_int(a->mode));
1160 if (get_mode_vector_elems(a->mode) > 1) {
1161 /* vector arithmetic not implemented yet */
1166 if (b == get_mode_null(b->mode)) return tarval_bad;
1167 /* modes of a,b are equal */
1168 sc_mod(a->value, b->value, NULL);
1169 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1175 tarval *tarval_abs(tarval *a)
1181 assert(mode_is_num(a->mode));
1183 if (get_mode_vector_elems(a->mode) > 1) {
1184 /* vector arithmetic not implemented yet */
1188 switch (get_mode_sort(a->mode))
1190 case irms_int_number:
1191 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1193 buffer = alloca(sc_get_buffer_length());
1194 sc_neg(a->value, buffer);
1195 return get_tarval_overflow(buffer, a->length, a->mode);
1199 case irms_float_number:
1200 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1202 fc_neg(a->value, NULL);
1203 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1216 tarval *tarval_and(tarval *a, tarval *b)
1221 assert(a->mode == b->mode);
1223 /* works even for vector modes */
1225 switch(get_mode_sort(a->mode))
1227 case irms_internal_boolean:
1228 return (a == tarval_b_false) ? a : b;
1230 case irms_int_number:
1231 sc_and(a->value, b->value, NULL);
1232 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1235 assert(0 && "operation not defined on mode");
1243 tarval *tarval_or (tarval *a, tarval *b)
1248 assert(a->mode == b->mode);
1250 /* works even for vector modes */
1252 switch (get_mode_sort(a->mode))
1254 case irms_internal_boolean:
1255 return (a == tarval_b_true) ? a : b;
1257 case irms_int_number:
1258 sc_or(a->value, b->value, NULL);
1259 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1262 assert(0 && "operation not defined on mode");
1268 * bitwise exclusive or (xor)
1270 tarval *tarval_eor(tarval *a, tarval *b)
1275 assert((a->mode == b->mode));
1277 /* works even for vector modes */
1279 switch (get_mode_sort(a->mode))
1281 case irms_internal_boolean:
1282 return (a == b)? tarval_b_false : tarval_b_true;
1284 case irms_int_number:
1285 sc_xor(a->value, b->value, NULL);
1286 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1289 assert(0 && "operation not defined on mode");
1295 * bitwise left shift
1297 tarval *tarval_shl(tarval *a, tarval *b)
1299 char *temp_val = NULL;
1303 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1305 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1306 /* vector arithmetic not implemented yet */
1310 if (get_mode_modulo_shift(a->mode) != 0)
1312 temp_val = alloca(sc_get_buffer_length());
1314 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1315 sc_mod(b->value, temp_val, temp_val);
1318 temp_val = (char*)b->value;
1320 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1321 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1325 * bitwise unsigned right shift
1327 tarval *tarval_shr(tarval *a, tarval *b)
1329 char *temp_val = NULL;
1333 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1335 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1336 /* vector arithmetic not implemented yet */
1340 if (get_mode_modulo_shift(a->mode) != 0)
1342 temp_val = alloca(sc_get_buffer_length());
1344 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1345 sc_mod(b->value, temp_val, temp_val);
1348 temp_val = (char*)b->value;
1350 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1351 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1355 * bitwise signed right shift
1357 tarval *tarval_shrs(tarval *a, tarval *b)
1359 char *temp_val = NULL;
1363 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1365 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1366 /* vector arithmetic not implemented yet */
1370 if (get_mode_modulo_shift(a->mode) != 0)
1372 temp_val = alloca(sc_get_buffer_length());
1374 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1375 sc_mod(b->value, temp_val, temp_val);
1378 temp_val = (char*)b->value;
1380 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1381 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1387 tarval *tarval_rot(tarval *a, tarval *b)
1389 char *temp_val = NULL;
1393 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1395 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1396 /* vector arithmetic not implemented yet */
1400 if (get_mode_modulo_shift(a->mode) != 0)
1402 temp_val = alloca(sc_get_buffer_length());
1404 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1405 sc_mod(b->value, temp_val, temp_val);
1408 temp_val = (char*)b->value;
1410 sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1411 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1418 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1420 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1424 const tarval_mode_info *mode_info;
1425 const char *prefix, *suffix;
1429 mode_info = tv->mode->tv_priv;
1431 mode_info = &default_info;
1432 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1433 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1435 switch (get_mode_sort(tv->mode))
1437 case irms_int_number:
1438 case irms_character:
1439 switch (mode_info->mode_output) {
1442 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1446 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1452 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1455 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1457 case irms_float_number:
1458 switch (mode_info->mode_output) {
1460 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1463 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1468 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1472 case irms_reference:
1473 if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1474 if (tv->value != NULL)
1475 if (tarval_is_entity(tv)) {
1476 if (get_entity_peculiarity((entity *)tv->value) != peculiarity_description)
1477 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1479 if (mode_info->mode_output == TVO_NATIVE)
1480 return snprintf(buf, len, "NULL");
1482 return snprintf(buf, len, "0");
1486 if (size > tv->length) {
1487 memcpy(buf, tv->value, tv->length);
1488 buf[tv->length] = '\0';
1492 memcpy(buf, tv->value, size-1);
1498 return snprintf(buf, len, "void");
1500 case irms_internal_boolean:
1501 switch (mode_info->mode_output) {
1507 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1511 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1514 case irms_control_flow:
1516 case irms_auxiliary:
1517 return snprintf(buf, len, "<BAD>");
1525 * Output of tarvals to stdio.
1527 int tarval_printf(tarval *tv) {
1531 res = tarval_snprintf(buf, sizeof(buf), tv);
1532 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1538 char *tarval_bitpattern(tarval *tv)
1544 * access to the bitpattern
1546 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1548 switch (get_mode_sort(tv->mode)) {
1549 case irms_int_number:
1550 case irms_character:
1551 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1553 case irms_float_number:
1554 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1562 * Specify the output options of one mode.
1564 * This functions stores the modinfo, so DO NOT DESTROY it.
1566 * Returns zero on success.
1568 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1572 mode->tv_priv = modeinfo;
1577 * Returns the output options of one mode.
1579 * This functions returns the modinfo of a given mode.
1581 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1585 return mode->tv_priv;
1589 * Identifying tarvals values for algebraic simplifications.
1592 * - TV_CLASSIFY_NULL for additive neutral,
1593 * - TV_CLASSIFY_ONE for multiplicative neutral,
1594 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1595 * - TV_CLASSIFY_OTHER else
1597 tarval_classification_t tarval_classify(tarval *tv)
1600 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1602 if (tv == get_mode_null(tv->mode))
1603 return TV_CLASSIFY_NULL;
1604 else if (tv == get_mode_one(tv->mode))
1605 return TV_CLASSIFY_ONE;
1606 else if ((get_mode_sort(tv->mode) == irms_int_number)
1607 && (tv == new_tarval_from_long(-1, tv->mode)))
1608 return TV_CLASSIFY_ALL_ONE;
1610 return TV_CLASSIFY_OTHER;
1614 * default mode_info for output as HEX
1616 static const tarval_mode_info hex_output = {
1623 * default mode_info for output as reference
1625 static const tarval_mode_info reference_output = {
1633 * Initialization of the tarval module: called before init_mode()
1635 void init_tarval_1(void)
1638 /* initialize the sets holding the tarvals with a comparison function and
1639 * an initial size, which is the expected number of constants */
1640 tarvals = new_set(memcmp, N_CONSTANTS);
1641 values = new_set(memcmp, N_CONSTANTS);
1642 /* init strcalc with precision of 68 to support floating point values with 64
1643 * bit mantissa (needs extra bits for rounding and overflow) */
1649 * Initialization of the tarval module: called after init_mode()
1651 void init_tarval_2(void)
1655 tarval_bad->mode = mode_BAD;
1656 tarval_undefined->mode = mode_ANY;
1657 tarval_b_true->mode = mode_b;
1658 tarval_b_false->mode = mode_b;
1659 tarval_P_void->mode = mode_P;
1662 * assign output modes that are compatible with the
1663 * old implementation: Hex output
1665 tarval_set_mode_output_option(mode_U, &hex_output);
1666 tarval_set_mode_output_option(mode_C, &hex_output);
1667 tarval_set_mode_output_option(mode_Bs, &hex_output);
1668 tarval_set_mode_output_option(mode_Bu, &hex_output);
1669 tarval_set_mode_output_option(mode_Hs, &hex_output);
1670 tarval_set_mode_output_option(mode_Hu, &hex_output);
1671 tarval_set_mode_output_option(mode_Is, &hex_output);
1672 tarval_set_mode_output_option(mode_Iu, &hex_output);
1673 tarval_set_mode_output_option(mode_Ls, &hex_output);
1674 tarval_set_mode_output_option(mode_Lu, &hex_output);
1675 tarval_set_mode_output_option(mode_P, &reference_output);
1678 /****************************************************************************
1680 ****************************************************************************/