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 * We no more support this function: Use the new SymConst instead.
410 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
414 assert(mode && (get_mode_sort(mode) == irms_reference));
416 return get_tarval((void *)ent, 0, mode);
420 int tarval_is_entity(tarval *tv)
424 /* tv->value == NULL means dereferencing a null pointer */
425 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
426 && (tv != tarval_P_void));
429 entity *tarval_to_entity(tarval *tv)
434 if (tarval_is_entity(tv))
435 return (entity *)tv->value;
437 assert(0 && "tarval did not represent an entity");
442 void free_tarval_entity(entity *ent) {
443 /* There can be a tarval referencing this entity. Even if the
444 tarval is not used by the code any more, it can still reference
445 the entity as tarvals live indepently of the entity referenced.
446 Further the tarval is hashed into a set. If a hash function
447 evaluation happens to collide with this tarval, we will vrfy that
448 it contains a proper entity and we will crash if the entity is
451 Unluckily, tarvals can neither be changed nor deleted, and to find
452 one, all existing reference modes have to be tried -> a facility
453 to retrieve all modes of a kind is needed. */
458 * Access routines for tarval fields ========================================
460 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
468 * Special value query functions ============================================
470 * These functions calculate and return a tarval representing the requested
472 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
473 * functions, but these are stored on initialization of the irmode module and
474 * therefore the irmode functions should be prefered to the functions below.
477 tarval *get_tarval_bad(void)
482 tarval *get_tarval_undefined(void)
485 return tarval_undefined;
487 tarval *get_tarval_b_false(void)
490 return tarval_b_false;
492 tarval *get_tarval_b_true(void)
495 return tarval_b_true;
497 tarval *get_tarval_P_void(void)
500 return tarval_P_void;
503 tarval *get_tarval_max(ir_mode *mode)
508 if (get_mode_vector_elems(mode) > 1) {
509 /* vector arithmetic not implemented yet */
513 switch(get_mode_sort(mode))
516 case irms_control_flow:
522 case irms_internal_boolean:
523 return tarval_b_true;
525 case irms_float_number:
526 switch(get_mode_size_bits(mode))
529 fc_get_max(8, 23, NULL);
532 fc_get_max(11, 52, NULL);
535 fc_get_max(15, 64, NULL);
538 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
540 case irms_int_number:
542 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
543 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
548 tarval *get_tarval_min(ir_mode *mode)
553 if (get_mode_vector_elems(mode) > 1) {
554 /* vector arithmetic not implemented yet */
558 switch(get_mode_sort(mode))
561 case irms_control_flow:
567 case irms_internal_boolean:
568 return tarval_b_false;
570 case irms_float_number:
571 switch(get_mode_size_bits(mode))
574 fc_get_min(8, 23, NULL);
577 fc_get_min(11, 52, NULL);
580 fc_get_min(15, 64, NULL);
583 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
585 case irms_int_number:
587 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
588 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
593 tarval *get_tarval_null(ir_mode *mode)
598 if (get_mode_vector_elems(mode) > 1) {
599 /* vector arithmetic not implemented yet */
603 switch(get_mode_sort(mode))
605 case irms_control_flow:
608 case irms_internal_boolean:
612 case irms_float_number:
613 return new_tarval_from_double(0.0, mode);
615 case irms_int_number:
617 return new_tarval_from_long(0l, mode);
620 return tarval_P_void;
625 tarval *get_tarval_one(ir_mode *mode)
630 if (get_mode_vector_elems(mode) > 1) {
631 /* vector arithmetic not implemented yet */
635 switch(get_mode_sort(mode))
637 case irms_control_flow:
640 case irms_internal_boolean:
645 case irms_float_number:
646 return new_tarval_from_double(1.0, mode);
648 case irms_int_number:
650 return new_tarval_from_long(1l, mode);
656 tarval *get_tarval_nan(ir_mode *mode)
661 if (get_mode_vector_elems(mode) > 1) {
662 /* vector arithmetic not implemented yet */
666 if (get_mode_sort(mode) == irms_float_number) {
667 switch(get_mode_size_bits(mode))
670 fc_get_qnan(8, 23, NULL);
673 fc_get_qnan(11, 52, NULL);
676 fc_get_qnan(15, 64, NULL);
679 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
682 assert(0 && "tarval is not floating point");
687 tarval *get_tarval_inf(ir_mode *mode)
692 if (get_mode_vector_elems(mode) > 1) {
693 /* vector arithmetic not implemented yet */
697 if (get_mode_sort(mode) == irms_float_number) {
698 switch(get_mode_size_bits(mode))
701 fc_get_plusinf(8, 23, NULL);
704 fc_get_plusinf(11, 52, NULL);
707 fc_get_plusinf(15, 64, NULL);
710 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
713 assert(0 && "tarval is not floating point");
719 * Arithmethic operations on tarvals ========================================
723 * test if negative number, 1 means 'yes'
725 int tarval_is_negative(tarval *a)
730 if (get_mode_vector_elems(a->mode) > 1) {
731 /* vector arithmetic not implemented yet */
732 assert(0 && "tarval_is_negative is not allowed for vector modes");
736 switch (get_mode_sort(a->mode))
738 case irms_int_number:
739 if (!mode_is_signed(a->mode)) return 0;
741 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
743 case irms_float_number:
744 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
747 assert(0 && "not implemented");
753 * test if null, 1 means 'yes'
755 int tarval_is_null(tarval *a)
757 ir_mode *m = get_tarval_mode(a);
759 return a == get_tarval_null(m);
763 * test if one, 1 means 'yes'
765 int tarval_is_one(tarval *a)
767 ir_mode *m = get_tarval_mode(a);
769 return a == get_tarval_one(m);
775 pnc_number tarval_cmp(tarval *a, tarval *b)
781 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
782 if (a == tarval_undefined || b == tarval_undefined) return False;
783 if (a == b) return Eq;
784 if (a->mode != b->mode) return False;
786 if (get_mode_vector_elems(a->mode) > 1) {
787 /* vector arithmetic not implemented yet */
788 assert(0 && "cmp not implemented for vector modes");
791 /* Here the two tarvals are unequal and of the same mode */
792 switch (get_mode_sort(a->mode))
794 case irms_control_flow:
800 case irms_float_number:
801 switch (fc_comp(a->value, b->value)) {
803 case 0: assert(0 && "different tarvals compare equal"); return Eq;
806 default: return False;
808 case irms_int_number:
810 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
812 case irms_internal_boolean:
813 return (a == tarval_b_true)?(Gt):(Lt);
819 * convert to other mode
821 tarval *tarval_convert_to(tarval *src, ir_mode *m)
829 if (src->mode == m) return src;
831 if (get_mode_vector_elems(src->mode) > 1) {
832 /* vector arithmetic not implemented yet */
836 switch (get_mode_sort(src->mode))
838 case irms_control_flow:
843 /* cast float to something */
844 case irms_float_number:
845 switch (get_mode_sort(m)) {
846 case irms_float_number:
847 switch (get_mode_size_bits(m))
850 fc_cast(src->value, 8, 23, NULL);
853 fc_cast(src->value, 11, 52, NULL);
856 fc_cast(src->value, 15, 64, NULL);
861 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
864 case irms_int_number:
865 switch (GET_FLOAT_TO_INT_MODE())
868 fc_int(src->value, NULL);
871 fc_rnd(src->value, NULL);
876 /* XXX floating point unit can't produce a value in integer
878 * an intermediate representation is needed here first. */
879 /* return get_tarval(); */
884 /* the rest can't be converted */
889 /* cast int to something */
890 case irms_int_number:
891 switch (get_mode_sort(m)) {
892 case irms_int_number:
894 return get_tarval_overflow(src->value, src->length, m);
896 case irms_internal_boolean:
897 /* XXX C semantics */
898 if (src == get_mode_null(src->mode)) return tarval_b_false;
899 else return tarval_b_true;
901 case irms_float_number:
902 /* XXX floating point unit does not understand internal integer
903 * representation, convert to string first, then create float from
905 buffer = alloca(100);
906 /* decimal string representation because hexadecimal output is
907 * interpreted unsigned by fc_val_from_str, so this is a HACK */
908 snprintf(buffer, 100, "%s",
909 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
910 switch (get_mode_size_bits(m))
913 fc_val_from_str(buffer, 0, 8, 23, NULL);
916 fc_val_from_str(buffer, 0, 11, 52, NULL);
919 fc_val_from_str(buffer, 0, 15, 64, NULL);
922 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
929 case irms_internal_boolean:
930 switch (get_mode_sort(m))
932 case irms_int_number:
933 if (src == tarval_b_true) return get_mode_one(m);
934 else return get_mode_null(m);
953 tarval *tarval_not(tarval *a)
959 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
961 /* works for vector mode without changes */
963 switch (get_mode_sort(a->mode))
965 case irms_int_number:
966 buffer = alloca(sc_get_buffer_length());
967 sc_not(a->value, buffer);
968 return get_tarval(buffer, a->length, a->mode);
976 * arithmetic negation
978 tarval *tarval_neg(tarval *a)
984 assert(mode_is_num(a->mode)); /* negation only for numerical values */
985 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
987 if (get_mode_vector_elems(a->mode) > 1) {
988 /* vector arithmetic not implemented yet */
992 switch (get_mode_sort(a->mode))
994 case irms_int_number:
995 buffer = alloca(sc_get_buffer_length());
996 sc_neg(a->value, buffer);
997 return get_tarval_overflow(buffer, a->length, a->mode);
999 case irms_float_number:
1000 fc_neg(a->value, NULL);
1001 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1011 tarval *tarval_add(tarval *a, tarval *b)
1018 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
1020 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(b->mode) > 1) {
1021 /* vector arithmetic not implemented yet */
1025 switch (get_mode_sort(a->mode))
1027 case irms_character:
1028 case irms_int_number:
1029 /* modes of a,b are equal, so result has mode of a as this might be the character */
1030 buffer = alloca(sc_get_buffer_length());
1031 sc_add(a->value, b->value, buffer);
1032 return get_tarval_overflow(buffer, a->length, a->mode);
1034 case irms_float_number:
1035 fc_add(a->value, b->value, NULL);
1036 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1046 tarval *tarval_sub(tarval *a, tarval *b)
1053 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
1055 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(b->mode) > 1) {
1056 /* vector arithmetic not implemented yet */
1059 switch (get_mode_sort(a->mode))
1061 case irms_character:
1062 case irms_int_number:
1063 /* modes of a,b are equal, so result has mode of a as this might be the character */
1064 buffer = alloca(sc_get_buffer_length());
1065 sc_sub(a->value, b->value, buffer);
1066 return get_tarval_overflow(buffer, a->length, a->mode);
1068 case irms_float_number:
1069 fc_sub(a->value, b->value, NULL);
1070 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1080 tarval *tarval_mul(tarval *a, tarval *b)
1087 assert((a->mode == b->mode) && mode_is_num(a->mode));
1089 if (get_mode_vector_elems(a->mode) > 1) {
1090 /* vector arithmetic not implemented yet */
1094 switch (get_mode_sort(a->mode))
1096 case irms_int_number:
1097 /* modes of a,b are equal */
1098 buffer = alloca(sc_get_buffer_length());
1099 sc_mul(a->value, b->value, buffer);
1100 return get_tarval_overflow(buffer, a->length, a->mode);
1102 case irms_float_number:
1103 fc_mul(a->value, b->value, NULL);
1104 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1112 * floating point division
1114 tarval *tarval_quo(tarval *a, tarval *b)
1119 assert((a->mode == b->mode) && mode_is_float(a->mode));
1121 if (get_mode_vector_elems(a->mode) > 1) {
1122 /* vector arithmetic not implemented yet */
1126 fc_div(a->value, b->value, NULL);
1127 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1132 * overflow is impossible, but look out for division by zero
1134 tarval *tarval_div(tarval *a, tarval *b)
1139 assert((a->mode == b->mode) && mode_is_int(a->mode));
1141 if (get_mode_vector_elems(a->mode) > 1) {
1142 /* vector arithmetic not implemented yet */
1147 if (b == get_mode_null(b->mode)) return tarval_bad;
1148 /* modes of a,b are equal */
1149 sc_div(a->value, b->value, NULL);
1150 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1155 * overflow is impossible, but look out for division by zero
1157 tarval *tarval_mod(tarval *a, tarval *b)
1162 assert((a->mode == b->mode) && mode_is_int(a->mode));
1164 if (get_mode_vector_elems(a->mode) > 1) {
1165 /* vector arithmetic not implemented yet */
1170 if (b == get_mode_null(b->mode)) return tarval_bad;
1171 /* modes of a,b are equal */
1172 sc_mod(a->value, b->value, NULL);
1173 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1179 tarval *tarval_abs(tarval *a)
1185 assert(mode_is_num(a->mode));
1187 if (get_mode_vector_elems(a->mode) > 1) {
1188 /* vector arithmetic not implemented yet */
1192 switch (get_mode_sort(a->mode))
1194 case irms_int_number:
1195 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1197 buffer = alloca(sc_get_buffer_length());
1198 sc_neg(a->value, buffer);
1199 return get_tarval_overflow(buffer, a->length, a->mode);
1203 case irms_float_number:
1204 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1206 fc_neg(a->value, NULL);
1207 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1220 tarval *tarval_and(tarval *a, tarval *b)
1225 assert(a->mode == b->mode);
1227 /* works even for vector modes */
1229 switch(get_mode_sort(a->mode))
1231 case irms_internal_boolean:
1232 return (a == tarval_b_false) ? a : b;
1234 case irms_int_number:
1235 sc_and(a->value, b->value, NULL);
1236 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1239 assert(0 && "operation not defined on mode");
1247 tarval *tarval_or (tarval *a, tarval *b)
1252 assert(a->mode == b->mode);
1254 /* works even for vector modes */
1256 switch (get_mode_sort(a->mode))
1258 case irms_internal_boolean:
1259 return (a == tarval_b_true) ? a : b;
1261 case irms_int_number:
1262 sc_or(a->value, b->value, NULL);
1263 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1266 assert(0 && "operation not defined on mode");
1272 * bitwise exclusive or (xor)
1274 tarval *tarval_eor(tarval *a, tarval *b)
1279 assert((a->mode == b->mode));
1281 /* works even for vector modes */
1283 switch (get_mode_sort(a->mode))
1285 case irms_internal_boolean:
1286 return (a == b)? tarval_b_false : tarval_b_true;
1288 case irms_int_number:
1289 sc_xor(a->value, b->value, NULL);
1290 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1293 assert(0 && "operation not defined on mode");
1299 * bitwise left shift
1301 tarval *tarval_shl(tarval *a, tarval *b)
1303 char *temp_val = NULL;
1307 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1309 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1310 /* vector arithmetic not implemented yet */
1314 if (get_mode_modulo_shift(a->mode) != 0)
1316 temp_val = alloca(sc_get_buffer_length());
1318 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1319 sc_mod(b->value, temp_val, temp_val);
1322 temp_val = (char*)b->value;
1324 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1325 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1329 * bitwise unsigned right shift
1331 tarval *tarval_shr(tarval *a, tarval *b)
1333 char *temp_val = NULL;
1337 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1339 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1340 /* vector arithmetic not implemented yet */
1344 if (get_mode_modulo_shift(a->mode) != 0)
1346 temp_val = alloca(sc_get_buffer_length());
1348 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1349 sc_mod(b->value, temp_val, temp_val);
1352 temp_val = (char*)b->value;
1354 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1355 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1359 * bitwise signed right shift
1361 tarval *tarval_shrs(tarval *a, tarval *b)
1363 char *temp_val = NULL;
1367 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1369 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1370 /* vector arithmetic not implemented yet */
1374 if (get_mode_modulo_shift(a->mode) != 0)
1376 temp_val = alloca(sc_get_buffer_length());
1378 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1379 sc_mod(b->value, temp_val, temp_val);
1382 temp_val = (char*)b->value;
1384 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1385 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1391 tarval *tarval_rot(tarval *a, tarval *b)
1393 char *temp_val = NULL;
1397 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1399 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1400 /* vector arithmetic not implemented yet */
1404 if (get_mode_modulo_shift(a->mode) != 0)
1406 temp_val = alloca(sc_get_buffer_length());
1408 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1409 sc_mod(b->value, temp_val, temp_val);
1412 temp_val = (char*)b->value;
1414 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1415 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1422 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1424 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1428 const tarval_mode_info *mode_info;
1429 const char *prefix, *suffix;
1433 mode_info = tv->mode->tv_priv;
1435 mode_info = &default_info;
1436 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1437 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1439 switch (get_mode_sort(tv->mode))
1441 case irms_int_number:
1442 case irms_character:
1443 switch (mode_info->mode_output) {
1446 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1450 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1456 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1459 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1461 case irms_float_number:
1462 switch (mode_info->mode_output) {
1464 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1467 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1472 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1476 case irms_reference:
1477 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1478 if (tv->value != NULL)
1479 if (tarval_is_entity(tv)) {
1480 if (get_entity_peculiarity((entity *)tv->value) != peculiarity_description)
1481 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1483 if (mode_info->mode_output == TVO_NATIVE)
1484 return snprintf(buf, len, "NULL");
1486 return snprintf(buf, len, "0");
1490 if (len > tv->length) {
1491 memcpy(buf, tv->value, tv->length);
1492 buf[tv->length] = '\0';
1496 memcpy(buf, tv->value, len-1);
1502 return snprintf(buf, len, "void");
1504 case irms_internal_boolean:
1505 switch (mode_info->mode_output) {
1511 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1515 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1518 case irms_control_flow:
1520 case irms_auxiliary:
1521 return snprintf(buf, len, "<BAD>");
1529 * Output of tarvals to stdio.
1531 int tarval_printf(tarval *tv) {
1535 res = tarval_snprintf(buf, sizeof(buf), tv);
1536 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1542 char *tarval_bitpattern(tarval *tv)
1548 * access to the bitpattern
1550 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1552 switch (get_mode_sort(tv->mode)) {
1553 case irms_int_number:
1554 case irms_character:
1555 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1557 case irms_float_number:
1558 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1566 * Specify the output options of one mode.
1568 * This functions stores the modinfo, so DO NOT DESTROY it.
1570 * Returns zero on success.
1572 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1576 mode->tv_priv = modeinfo;
1581 * Returns the output options of one mode.
1583 * This functions returns the modinfo of a given mode.
1585 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1589 return mode->tv_priv;
1593 * Identifying tarvals values for algebraic simplifications.
1596 * - TV_CLASSIFY_NULL for additive neutral,
1597 * - TV_CLASSIFY_ONE for multiplicative neutral,
1598 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1599 * - TV_CLASSIFY_OTHER else
1601 tarval_classification_t tarval_classify(tarval *tv)
1604 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1606 if (tv == get_mode_null(tv->mode))
1607 return TV_CLASSIFY_NULL;
1608 else if (tv == get_mode_one(tv->mode))
1609 return TV_CLASSIFY_ONE;
1610 else if ((get_mode_sort(tv->mode) == irms_int_number)
1611 && (tv == new_tarval_from_long(-1, tv->mode)))
1612 return TV_CLASSIFY_ALL_ONE;
1614 return TV_CLASSIFY_OTHER;
1618 * default mode_info for output as HEX
1620 static const tarval_mode_info hex_output = {
1627 * default mode_info for output as reference
1629 static const tarval_mode_info reference_output = {
1637 * Initialization of the tarval module: called before init_mode()
1639 void init_tarval_1(void)
1642 /* initialize the sets holding the tarvals with a comparison function and
1643 * an initial size, which is the expected number of constants */
1644 tarvals = new_set(memcmp, N_CONSTANTS);
1645 values = new_set(memcmp, N_CONSTANTS);
1646 /* init strcalc with precision of 68 to support floating point values with 64
1647 * bit mantissa (needs extra bits for rounding and overflow) */
1653 * Initialization of the tarval module: called after init_mode()
1655 void init_tarval_2(void)
1659 tarval_bad->mode = mode_BAD;
1660 tarval_undefined->mode = mode_ANY;
1661 tarval_b_true->mode = mode_b;
1662 tarval_b_false->mode = mode_b;
1663 tarval_P_void->mode = mode_P;
1666 * assign output modes that are compatible with the
1667 * old implementation: Hex output
1669 tarval_set_mode_output_option(mode_U, &hex_output);
1670 tarval_set_mode_output_option(mode_C, &hex_output);
1671 tarval_set_mode_output_option(mode_Bs, &hex_output);
1672 tarval_set_mode_output_option(mode_Bu, &hex_output);
1673 tarval_set_mode_output_option(mode_Hs, &hex_output);
1674 tarval_set_mode_output_option(mode_Hu, &hex_output);
1675 tarval_set_mode_output_option(mode_Is, &hex_output);
1676 tarval_set_mode_output_option(mode_Iu, &hex_output);
1677 tarval_set_mode_output_option(mode_Ls, &hex_output);
1678 tarval_set_mode_output_option(mode_Lu, &hex_output);
1679 tarval_set_mode_output_option(mode_P, &reference_output);
1682 /* free all memory occupied by tarval. */
1683 void finish_tarval(void) {
1686 del_set(tarvals); tarvals = NULL;
1687 del_set(values); values = NULL;
1690 /****************************************************************************
1692 ****************************************************************************/