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 tarval_to_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 tarval_to_double(tarval *tv)
400 assert(tarval_is_double(tv));
402 return fc_val_to_float(tv->value);
405 /* The tarval represents the address of the entity. As the address must
406 be constant the entity must have as owner the global type.
407 * We no more support this function: Use the new SymConst instead.
409 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
413 assert(mode && (get_mode_sort(mode) == irms_reference));
415 return get_tarval((void *)ent, 0, mode);
419 int tarval_is_entity(tarval *tv)
423 /* tv->value == NULL means dereferencing a null pointer */
424 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
425 && (tv != tarval_P_void));
428 entity *tarval_to_entity(tarval *tv)
433 if (tarval_is_entity(tv))
434 return (entity *)tv->value;
436 assert(0 && "tarval did not represent an entity");
441 void free_tarval_entity(entity *ent) {
442 /* There can be a tarval referencing this entity. Even if the
443 tarval is not used by the code any more, it can still reference
444 the entity as tarvals live indepently of the entity referenced.
445 Further the tarval is hashed into a set. If a hash function
446 evaluation happens to collide with this tarval, we will vrfy that
447 it contains a proper entity and we will crash if the entity is
450 Unluckily, tarvals can neither be changed nor deleted, and to find
451 one, all existing reference modes have to be tried -> a facility
452 to retrieve all modes of a kind is needed. */
457 * Access routines for tarval fields ========================================
459 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
467 * Special value query functions ============================================
469 * These functions calculate and return a tarval representing the requested
471 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
472 * functions, but these are stored on initialization of the irmode module and
473 * therefore the irmode functions should be prefered to the functions below.
476 tarval *get_tarval_bad(void)
481 tarval *get_tarval_undefined(void)
484 return tarval_undefined;
486 tarval *get_tarval_b_false(void)
489 return tarval_b_false;
491 tarval *get_tarval_b_true(void)
494 return tarval_b_true;
496 tarval *get_tarval_P_void(void)
499 return tarval_P_void;
502 tarval *get_tarval_max(ir_mode *mode)
507 if (get_mode_vector_elems(mode) > 1) {
508 /* vector arithmetic not implemented yet */
512 switch(get_mode_sort(mode))
515 case irms_control_flow:
521 case irms_internal_boolean:
522 return tarval_b_true;
524 case irms_float_number:
525 switch(get_mode_size_bits(mode))
528 fc_get_max(8, 23, NULL);
531 fc_get_max(11, 52, NULL);
534 fc_get_max(15, 64, NULL);
537 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
539 case irms_int_number:
541 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
542 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
547 tarval *get_tarval_min(ir_mode *mode)
552 if (get_mode_vector_elems(mode) > 1) {
553 /* vector arithmetic not implemented yet */
557 switch(get_mode_sort(mode))
560 case irms_control_flow:
566 case irms_internal_boolean:
567 return tarval_b_false;
569 case irms_float_number:
570 switch(get_mode_size_bits(mode))
573 fc_get_min(8, 23, NULL);
576 fc_get_min(11, 52, NULL);
579 fc_get_min(15, 64, NULL);
582 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
584 case irms_int_number:
586 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
587 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
592 tarval *get_tarval_null(ir_mode *mode)
597 if (get_mode_vector_elems(mode) > 1) {
598 /* vector arithmetic not implemented yet */
602 switch(get_mode_sort(mode))
604 case irms_control_flow:
607 case irms_internal_boolean:
611 case irms_float_number:
612 return new_tarval_from_double(0.0, mode);
614 case irms_int_number:
616 return new_tarval_from_long(0l, mode);
619 return tarval_P_void;
624 tarval *get_tarval_one(ir_mode *mode)
629 if (get_mode_vector_elems(mode) > 1) {
630 /* vector arithmetic not implemented yet */
634 switch(get_mode_sort(mode))
636 case irms_control_flow:
639 case irms_internal_boolean:
644 case irms_float_number:
645 return new_tarval_from_double(1.0, mode);
647 case irms_int_number:
649 return new_tarval_from_long(1l, mode);
655 tarval *get_tarval_nan(ir_mode *mode)
660 if (get_mode_vector_elems(mode) > 1) {
661 /* vector arithmetic not implemented yet */
665 if (get_mode_sort(mode) == irms_float_number) {
666 switch(get_mode_size_bits(mode))
669 fc_get_qnan(8, 23, NULL);
672 fc_get_qnan(11, 52, NULL);
675 fc_get_qnan(15, 64, NULL);
678 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
681 assert(0 && "tarval is not floating point");
686 tarval *get_tarval_inf(ir_mode *mode)
691 if (get_mode_vector_elems(mode) > 1) {
692 /* vector arithmetic not implemented yet */
696 if (get_mode_sort(mode) == irms_float_number) {
697 switch(get_mode_size_bits(mode))
700 fc_get_plusinf(8, 23, NULL);
703 fc_get_plusinf(11, 52, NULL);
706 fc_get_plusinf(15, 64, NULL);
709 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
712 assert(0 && "tarval is not floating point");
718 * Arithmethic operations on tarvals ========================================
722 * test if negative number, 1 means 'yes'
724 int tarval_is_negative(tarval *a)
729 if (get_mode_vector_elems(a->mode) > 1) {
730 /* vector arithmetic not implemented yet */
731 assert(0 && "tarval_is_negative is not allowed for vector modes");
735 switch (get_mode_sort(a->mode))
737 case irms_int_number:
738 if (!mode_is_signed(a->mode)) return 0;
740 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
742 case irms_float_number:
743 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
746 assert(0 && "not implemented");
752 * test if null, 1 means 'yes'
754 int tarval_is_null(tarval *a)
756 ir_mode *m = get_tarval_mode(a);
758 return a == get_tarval_null(m);
762 * test if one, 1 means 'yes'
764 int tarval_is_one(tarval *a)
766 ir_mode *m = get_tarval_mode(a);
768 return a == get_tarval_one(m);
774 pnc_number tarval_cmp(tarval *a, tarval *b)
780 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
781 if (a == tarval_undefined || b == tarval_undefined) return False;
782 if (a == b) return Eq;
783 if (a->mode != b->mode) return False;
785 if (get_mode_vector_elems(a->mode) > 1) {
786 /* vector arithmetic not implemented yet */
787 assert(0 && "cmp not implemented for vector modes");
790 /* Here the two tarvals are unequal and of the same mode */
791 switch (get_mode_sort(a->mode))
793 case irms_control_flow:
799 case irms_float_number:
800 switch (fc_comp(a->value, b->value)) {
802 case 0: assert(0 && "different tarvals compare equal"); return Eq;
805 default: return False;
807 case irms_int_number:
809 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
811 case irms_internal_boolean:
812 return (a == tarval_b_true)?(Gt):(Lt);
818 * convert to other mode
820 tarval *tarval_convert_to(tarval *src, ir_mode *m)
828 if (src->mode == m) return src;
830 if (get_mode_vector_elems(src->mode) > 1) {
831 /* vector arithmetic not implemented yet */
835 switch (get_mode_sort(src->mode))
837 case irms_control_flow:
842 /* cast float to something */
843 case irms_float_number:
844 switch (get_mode_sort(m)) {
845 case irms_float_number:
846 switch (get_mode_size_bits(m))
849 fc_cast(src->value, 8, 23, NULL);
852 fc_cast(src->value, 11, 52, NULL);
855 fc_cast(src->value, 15, 64, NULL);
860 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
863 case irms_int_number:
864 switch (GET_FLOAT_TO_INT_MODE())
867 fc_int(src->value, NULL);
870 fc_rnd(src->value, NULL);
875 /* XXX floating point unit can't produce a value in integer
877 * an intermediate representation is needed here first. */
878 /* return get_tarval(); */
883 /* the rest can't be converted */
888 /* cast int to something */
889 case irms_int_number:
890 switch (get_mode_sort(m)) {
891 case irms_int_number:
893 return get_tarval_overflow(src->value, src->length, m);
895 case irms_internal_boolean:
896 /* XXX C semantics */
897 if (src == get_mode_null(src->mode)) return tarval_b_false;
898 else return tarval_b_true;
900 case irms_float_number:
901 /* XXX floating point unit does not understand internal integer
902 * representation, convert to string first, then create float from
904 buffer = alloca(100);
905 /* decimal string representation because hexadecimal output is
906 * interpreted unsigned by fc_val_from_str, so this is a HACK */
907 snprintf(buffer, 100, "%s",
908 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
909 switch (get_mode_size_bits(m))
912 fc_val_from_str(buffer, 0, 8, 23, NULL);
915 fc_val_from_str(buffer, 0, 11, 52, NULL);
918 fc_val_from_str(buffer, 0, 15, 64, NULL);
921 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
928 case irms_internal_boolean:
929 switch (get_mode_sort(m))
931 case irms_int_number:
932 if (src == tarval_b_true) return get_mode_one(m);
933 else return get_mode_null(m);
952 tarval *tarval_not(tarval *a)
958 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
960 /* works for vector mode without changes */
962 switch (get_mode_sort(a->mode))
964 case irms_int_number:
965 buffer = alloca(sc_get_buffer_length());
966 sc_not(a->value, buffer);
967 return get_tarval(buffer, a->length, a->mode);
975 * arithmetic negation
977 tarval *tarval_neg(tarval *a)
983 assert(mode_is_num(a->mode)); /* negation only for numerical values */
984 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
986 if (get_mode_vector_elems(a->mode) > 1) {
987 /* vector arithmetic not implemented yet */
991 switch (get_mode_sort(a->mode))
993 case irms_int_number:
994 buffer = alloca(sc_get_buffer_length());
995 sc_neg(a->value, buffer);
996 return get_tarval_overflow(buffer, a->length, a->mode);
998 case irms_float_number:
999 fc_neg(a->value, NULL);
1000 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1010 tarval *tarval_add(tarval *a, tarval *b)
1017 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
1019 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(b->mode) > 1) {
1020 /* vector arithmetic not implemented yet */
1024 switch (get_mode_sort(a->mode))
1026 case irms_character:
1027 case irms_int_number:
1028 /* modes of a,b are equal, so result has mode of a as this might be the character */
1029 buffer = alloca(sc_get_buffer_length());
1030 sc_add(a->value, b->value, buffer);
1031 return get_tarval_overflow(buffer, a->length, a->mode);
1033 case irms_float_number:
1034 fc_add(a->value, b->value, NULL);
1035 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1045 tarval *tarval_sub(tarval *a, tarval *b)
1052 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
1054 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(b->mode) > 1) {
1055 /* vector arithmetic not implemented yet */
1058 switch (get_mode_sort(a->mode))
1060 case irms_character:
1061 case irms_int_number:
1062 /* modes of a,b are equal, so result has mode of a as this might be the character */
1063 buffer = alloca(sc_get_buffer_length());
1064 sc_sub(a->value, b->value, buffer);
1065 return get_tarval_overflow(buffer, a->length, a->mode);
1067 case irms_float_number:
1068 fc_sub(a->value, b->value, NULL);
1069 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1079 tarval *tarval_mul(tarval *a, tarval *b)
1086 assert((a->mode == b->mode) && mode_is_num(a->mode));
1088 if (get_mode_vector_elems(a->mode) > 1) {
1089 /* vector arithmetic not implemented yet */
1093 switch (get_mode_sort(a->mode))
1095 case irms_int_number:
1096 /* modes of a,b are equal */
1097 buffer = alloca(sc_get_buffer_length());
1098 sc_mul(a->value, b->value, buffer);
1099 return get_tarval_overflow(buffer, a->length, a->mode);
1101 case irms_float_number:
1102 fc_mul(a->value, b->value, NULL);
1103 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1111 * floating point division
1113 tarval *tarval_quo(tarval *a, tarval *b)
1118 assert((a->mode == b->mode) && mode_is_float(a->mode));
1120 if (get_mode_vector_elems(a->mode) > 1) {
1121 /* vector arithmetic not implemented yet */
1125 fc_div(a->value, b->value, NULL);
1126 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1131 * overflow is impossible, but look out for division by zero
1133 tarval *tarval_div(tarval *a, tarval *b)
1138 assert((a->mode == b->mode) && mode_is_int(a->mode));
1140 if (get_mode_vector_elems(a->mode) > 1) {
1141 /* vector arithmetic not implemented yet */
1146 if (b == get_mode_null(b->mode)) return tarval_bad;
1147 /* modes of a,b are equal */
1148 sc_div(a->value, b->value, NULL);
1149 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1154 * overflow is impossible, but look out for division by zero
1156 tarval *tarval_mod(tarval *a, tarval *b)
1161 assert((a->mode == b->mode) && mode_is_int(a->mode));
1163 if (get_mode_vector_elems(a->mode) > 1) {
1164 /* vector arithmetic not implemented yet */
1169 if (b == get_mode_null(b->mode)) return tarval_bad;
1170 /* modes of a,b are equal */
1171 sc_mod(a->value, b->value, NULL);
1172 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1178 tarval *tarval_abs(tarval *a)
1184 assert(mode_is_num(a->mode));
1186 if (get_mode_vector_elems(a->mode) > 1) {
1187 /* vector arithmetic not implemented yet */
1191 switch (get_mode_sort(a->mode))
1193 case irms_int_number:
1194 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1196 buffer = alloca(sc_get_buffer_length());
1197 sc_neg(a->value, buffer);
1198 return get_tarval_overflow(buffer, a->length, a->mode);
1202 case irms_float_number:
1203 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1205 fc_neg(a->value, NULL);
1206 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1219 tarval *tarval_and(tarval *a, tarval *b)
1224 assert(a->mode == b->mode);
1226 /* works even for vector modes */
1228 switch(get_mode_sort(a->mode))
1230 case irms_internal_boolean:
1231 return (a == tarval_b_false) ? a : b;
1233 case irms_int_number:
1234 sc_and(a->value, b->value, NULL);
1235 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1238 assert(0 && "operation not defined on mode");
1246 tarval *tarval_or (tarval *a, tarval *b)
1251 assert(a->mode == b->mode);
1253 /* works even for vector modes */
1255 switch (get_mode_sort(a->mode))
1257 case irms_internal_boolean:
1258 return (a == tarval_b_true) ? a : b;
1260 case irms_int_number:
1261 sc_or(a->value, b->value, NULL);
1262 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1265 assert(0 && "operation not defined on mode");
1271 * bitwise exclusive or (xor)
1273 tarval *tarval_eor(tarval *a, tarval *b)
1278 assert((a->mode == b->mode));
1280 /* works even for vector modes */
1282 switch (get_mode_sort(a->mode))
1284 case irms_internal_boolean:
1285 return (a == b)? tarval_b_false : tarval_b_true;
1287 case irms_int_number:
1288 sc_xor(a->value, b->value, NULL);
1289 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1292 assert(0 && "operation not defined on mode");
1298 * bitwise left shift
1300 tarval *tarval_shl(tarval *a, tarval *b)
1302 char *temp_val = NULL;
1306 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1308 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1309 /* vector arithmetic not implemented yet */
1313 if (get_mode_modulo_shift(a->mode) != 0)
1315 temp_val = alloca(sc_get_buffer_length());
1317 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1318 sc_mod(b->value, temp_val, temp_val);
1321 temp_val = (char*)b->value;
1323 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1324 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1328 * bitwise unsigned right shift
1330 tarval *tarval_shr(tarval *a, tarval *b)
1332 char *temp_val = NULL;
1336 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1338 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1339 /* vector arithmetic not implemented yet */
1343 if (get_mode_modulo_shift(a->mode) != 0)
1345 temp_val = alloca(sc_get_buffer_length());
1347 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1348 sc_mod(b->value, temp_val, temp_val);
1351 temp_val = (char*)b->value;
1353 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1354 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1358 * bitwise signed right shift
1360 tarval *tarval_shrs(tarval *a, tarval *b)
1362 char *temp_val = NULL;
1366 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1368 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1369 /* vector arithmetic not implemented yet */
1373 if (get_mode_modulo_shift(a->mode) != 0)
1375 temp_val = alloca(sc_get_buffer_length());
1377 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1378 sc_mod(b->value, temp_val, temp_val);
1381 temp_val = (char*)b->value;
1383 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1384 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1390 tarval *tarval_rot(tarval *a, tarval *b)
1392 char *temp_val = NULL;
1396 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1398 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1399 /* vector arithmetic not implemented yet */
1403 if (get_mode_modulo_shift(a->mode) != 0)
1405 temp_val = alloca(sc_get_buffer_length());
1407 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1408 sc_mod(b->value, temp_val, temp_val);
1411 temp_val = (char*)b->value;
1413 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1414 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1421 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1423 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1427 const tarval_mode_info *mode_info;
1428 const char *prefix, *suffix;
1432 mode_info = tv->mode->tv_priv;
1434 mode_info = &default_info;
1435 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1436 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1438 switch (get_mode_sort(tv->mode))
1440 case irms_int_number:
1441 case irms_character:
1442 switch (mode_info->mode_output) {
1445 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1449 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1455 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1458 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1460 case irms_float_number:
1461 switch (mode_info->mode_output) {
1463 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1466 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1471 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1475 case irms_reference:
1476 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1477 if (tv->value != NULL)
1478 if (tarval_is_entity(tv)) {
1479 if (get_entity_peculiarity((entity *)tv->value) != peculiarity_description)
1480 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1482 if (mode_info->mode_output == TVO_NATIVE)
1483 return snprintf(buf, len, "NULL");
1485 return snprintf(buf, len, "0");
1489 if (len > tv->length) {
1490 memcpy(buf, tv->value, tv->length);
1491 buf[tv->length] = '\0';
1495 memcpy(buf, tv->value, len-1);
1501 return snprintf(buf, len, "void");
1503 case irms_internal_boolean:
1504 switch (mode_info->mode_output) {
1510 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1514 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1517 case irms_control_flow:
1519 case irms_auxiliary:
1520 return snprintf(buf, len, "<BAD>");
1528 * Output of tarvals to stdio.
1530 int tarval_printf(tarval *tv) {
1534 res = tarval_snprintf(buf, sizeof(buf), tv);
1535 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1541 char *tarval_bitpattern(tarval *tv)
1547 * access to the bitpattern
1549 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1551 switch (get_mode_sort(tv->mode)) {
1552 case irms_int_number:
1553 case irms_character:
1554 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1556 case irms_float_number:
1557 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1565 * Specify the output options of one mode.
1567 * This functions stores the modinfo, so DO NOT DESTROY it.
1569 * Returns zero on success.
1571 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1575 mode->tv_priv = modeinfo;
1580 * Returns the output options of one mode.
1582 * This functions returns the modinfo of a given mode.
1584 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1588 return mode->tv_priv;
1592 * Identifying tarvals values for algebraic simplifications.
1595 * - TV_CLASSIFY_NULL for additive neutral,
1596 * - TV_CLASSIFY_ONE for multiplicative neutral,
1597 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1598 * - TV_CLASSIFY_OTHER else
1600 tarval_classification_t tarval_classify(tarval *tv)
1603 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1605 if (tv == get_mode_null(tv->mode))
1606 return TV_CLASSIFY_NULL;
1607 else if (tv == get_mode_one(tv->mode))
1608 return TV_CLASSIFY_ONE;
1609 else if ((get_mode_sort(tv->mode) == irms_int_number)
1610 && (tv == new_tarval_from_long(-1, tv->mode)))
1611 return TV_CLASSIFY_ALL_ONE;
1613 return TV_CLASSIFY_OTHER;
1617 * default mode_info for output as HEX
1619 static const tarval_mode_info hex_output = {
1626 * default mode_info for output as reference
1628 static const tarval_mode_info reference_output = {
1636 * Initialization of the tarval module: called before init_mode()
1638 void init_tarval_1(void)
1641 /* initialize the sets holding the tarvals with a comparison function and
1642 * an initial size, which is the expected number of constants */
1643 tarvals = new_set(memcmp, N_CONSTANTS);
1644 values = new_set(memcmp, N_CONSTANTS);
1645 /* init strcalc with precision of 68 to support floating point values with 64
1646 * bit mantissa (needs extra bits for rounding and overflow) */
1652 * Initialization of the tarval module: called after init_mode()
1654 void init_tarval_2(void)
1658 tarval_bad->mode = mode_BAD;
1659 tarval_undefined->mode = mode_ANY;
1660 tarval_b_true->mode = mode_b;
1661 tarval_b_false->mode = mode_b;
1662 tarval_P_void->mode = mode_P;
1665 * assign output modes that are compatible with the
1666 * old implementation: Hex output
1668 tarval_set_mode_output_option(mode_U, &hex_output);
1669 tarval_set_mode_output_option(mode_C, &hex_output);
1670 tarval_set_mode_output_option(mode_Bs, &hex_output);
1671 tarval_set_mode_output_option(mode_Bu, &hex_output);
1672 tarval_set_mode_output_option(mode_Hs, &hex_output);
1673 tarval_set_mode_output_option(mode_Hu, &hex_output);
1674 tarval_set_mode_output_option(mode_Is, &hex_output);
1675 tarval_set_mode_output_option(mode_Iu, &hex_output);
1676 tarval_set_mode_output_option(mode_Ls, &hex_output);
1677 tarval_set_mode_output_option(mode_Lu, &hex_output);
1678 tarval_set_mode_output_option(mode_P, &reference_output);
1681 /* free all memory occupied by tarval. */
1682 void finish_tarval(void) {
1685 del_set(tarvals); tarvals = NULL;
1686 del_set(values); values = NULL;
1689 /****************************************************************************
1691 ****************************************************************************/