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 #undef tarval_to_entity
429 entity *tarval_to_entity(tarval *tv) { return get_tarval_entity(tv); }
430 entity *get_tarval_entity(tarval *tv)
435 if (tarval_is_entity(tv))
436 return (entity *)tv->value;
438 assert(0 && "tarval did not represent an entity");
443 void free_tarval_entity(entity *ent) {
444 /* There can be a tarval referencing this entity. Even if the
445 tarval is not used by the code any more, it can still reference
446 the entity as tarvals live indepently of the entity referenced.
447 Further the tarval is hashed into a set. If a hash function
448 evaluation happens to collide with this tarval, we will vrfy that
449 it contains a proper entity and we will crash if the entity is
452 Unluckily, tarvals can neither be changed nor deleted, and to find
453 one, all existing reference modes have to be tried -> a facility
454 to retrieve all modes of a kind is needed. */
459 * Access routines for tarval fields ========================================
461 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
469 * Special value query functions ============================================
471 * These functions calculate and return a tarval representing the requested
473 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
474 * functions, but these are stored on initialization of the irmode module and
475 * therefore the irmode functions should be prefered to the functions below.
478 tarval *get_tarval_bad(void)
483 tarval *get_tarval_undefined(void)
486 return tarval_undefined;
488 tarval *get_tarval_b_false(void)
491 return tarval_b_false;
493 tarval *get_tarval_b_true(void)
496 return tarval_b_true;
498 tarval *get_tarval_P_void(void)
501 return tarval_P_void;
504 tarval *get_tarval_max(ir_mode *mode)
509 if (get_mode_vector_elems(mode) > 1) {
510 /* vector arithmetic not implemented yet */
514 switch(get_mode_sort(mode))
517 case irms_control_flow:
523 case irms_internal_boolean:
524 return tarval_b_true;
526 case irms_float_number:
527 switch(get_mode_size_bits(mode))
530 fc_get_max(8, 23, NULL);
533 fc_get_max(11, 52, NULL);
536 fc_get_max(15, 64, NULL);
539 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
541 case irms_int_number:
543 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
544 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
549 tarval *get_tarval_min(ir_mode *mode)
554 if (get_mode_vector_elems(mode) > 1) {
555 /* vector arithmetic not implemented yet */
559 switch(get_mode_sort(mode))
562 case irms_control_flow:
568 case irms_internal_boolean:
569 return tarval_b_false;
571 case irms_float_number:
572 switch(get_mode_size_bits(mode))
575 fc_get_min(8, 23, NULL);
578 fc_get_min(11, 52, NULL);
581 fc_get_min(15, 64, NULL);
584 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
586 case irms_int_number:
588 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
589 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
594 tarval *get_tarval_null(ir_mode *mode)
599 if (get_mode_vector_elems(mode) > 1) {
600 /* vector arithmetic not implemented yet */
604 switch(get_mode_sort(mode))
606 case irms_control_flow:
609 case irms_internal_boolean:
613 case irms_float_number:
614 return new_tarval_from_double(0.0, mode);
616 case irms_int_number:
618 return new_tarval_from_long(0l, mode);
621 return tarval_P_void;
626 tarval *get_tarval_one(ir_mode *mode)
631 if (get_mode_vector_elems(mode) > 1) {
632 /* vector arithmetic not implemented yet */
636 switch(get_mode_sort(mode))
638 case irms_control_flow:
641 case irms_internal_boolean:
646 case irms_float_number:
647 return new_tarval_from_double(1.0, mode);
649 case irms_int_number:
651 return new_tarval_from_long(1l, mode);
657 tarval *get_tarval_nan(ir_mode *mode)
662 if (get_mode_vector_elems(mode) > 1) {
663 /* vector arithmetic not implemented yet */
667 if (get_mode_sort(mode) == irms_float_number) {
668 switch(get_mode_size_bits(mode))
671 fc_get_qnan(8, 23, NULL);
674 fc_get_qnan(11, 52, NULL);
677 fc_get_qnan(15, 64, NULL);
680 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
683 assert(0 && "tarval is not floating point");
688 tarval *get_tarval_inf(ir_mode *mode)
693 if (get_mode_vector_elems(mode) > 1) {
694 /* vector arithmetic not implemented yet */
698 if (get_mode_sort(mode) == irms_float_number) {
699 switch(get_mode_size_bits(mode))
702 fc_get_plusinf(8, 23, NULL);
705 fc_get_plusinf(11, 52, NULL);
708 fc_get_plusinf(15, 64, NULL);
711 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
714 assert(0 && "tarval is not floating point");
720 * Arithmethic operations on tarvals ========================================
724 * test if negative number, 1 means 'yes'
726 int tarval_is_negative(tarval *a)
731 if (get_mode_vector_elems(a->mode) > 1) {
732 /* vector arithmetic not implemented yet */
733 assert(0 && "tarval_is_negative is not allowed for vector modes");
737 switch (get_mode_sort(a->mode))
739 case irms_int_number:
740 if (!mode_is_signed(a->mode)) return 0;
742 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
744 case irms_float_number:
745 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
748 assert(0 && "not implemented");
754 * test if null, 1 means 'yes'
756 int tarval_is_null(tarval *a)
758 ir_mode *m = get_tarval_mode(a);
760 return a == get_tarval_null(m);
764 * test if one, 1 means 'yes'
766 int tarval_is_one(tarval *a)
768 ir_mode *m = get_tarval_mode(a);
770 return a == get_tarval_one(m);
776 pnc_number tarval_cmp(tarval *a, tarval *b)
782 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
783 if (a == tarval_undefined || b == tarval_undefined) return False;
784 if (a == b) return Eq;
785 if (a->mode != b->mode) return False;
787 if (get_mode_vector_elems(a->mode) > 1) {
788 /* vector arithmetic not implemented yet */
789 assert(0 && "cmp not implemented for vector modes");
792 /* Here the two tarvals are unequal and of the same mode */
793 switch (get_mode_sort(a->mode))
795 case irms_control_flow:
801 case irms_float_number:
802 switch (fc_comp(a->value, b->value)) {
804 case 0: assert(0 && "different tarvals compare equal"); return Eq;
807 default: return False;
809 case irms_int_number:
811 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
813 case irms_internal_boolean:
814 return (a == tarval_b_true)?(Gt):(Lt);
820 * convert to other mode
822 tarval *tarval_convert_to(tarval *src, ir_mode *m)
830 if (src->mode == m) return src;
832 if (get_mode_vector_elems(src->mode) > 1) {
833 /* vector arithmetic not implemented yet */
837 switch (get_mode_sort(src->mode))
839 case irms_control_flow:
844 /* cast float to something */
845 case irms_float_number:
846 switch (get_mode_sort(m)) {
847 case irms_float_number:
848 switch (get_mode_size_bits(m))
851 fc_cast(src->value, 8, 23, NULL);
854 fc_cast(src->value, 11, 52, NULL);
857 fc_cast(src->value, 15, 64, NULL);
862 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
865 case irms_int_number:
866 switch (GET_FLOAT_TO_INT_MODE())
869 fc_int(src->value, NULL);
872 fc_rnd(src->value, NULL);
877 /* XXX floating point unit can't produce a value in integer
879 * an intermediate representation is needed here first. */
880 /* return get_tarval(); */
885 /* the rest can't be converted */
890 /* cast int to something */
891 case irms_int_number:
892 switch (get_mode_sort(m)) {
893 case irms_int_number:
895 return get_tarval_overflow(src->value, src->length, m);
897 case irms_internal_boolean:
898 /* XXX C semantics */
899 if (src == get_mode_null(src->mode)) return tarval_b_false;
900 else return tarval_b_true;
902 case irms_float_number:
903 /* XXX floating point unit does not understand internal integer
904 * representation, convert to string first, then create float from
906 buffer = alloca(100);
907 /* decimal string representation because hexadecimal output is
908 * interpreted unsigned by fc_val_from_str, so this is a HACK */
909 snprintf(buffer, 100, "%s",
910 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
911 switch (get_mode_size_bits(m))
914 fc_val_from_str(buffer, 0, 8, 23, NULL);
917 fc_val_from_str(buffer, 0, 11, 52, NULL);
920 fc_val_from_str(buffer, 0, 15, 64, NULL);
923 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
930 case irms_internal_boolean:
931 switch (get_mode_sort(m))
933 case irms_int_number:
934 if (src == tarval_b_true) return get_mode_one(m);
935 else return get_mode_null(m);
954 tarval *tarval_not(tarval *a)
960 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
962 /* works for vector mode without changes */
964 switch (get_mode_sort(a->mode))
966 case irms_int_number:
967 buffer = alloca(sc_get_buffer_length());
968 sc_not(a->value, buffer);
969 return get_tarval(buffer, a->length, a->mode);
977 * arithmetic negation
979 tarval *tarval_neg(tarval *a)
985 assert(mode_is_num(a->mode)); /* negation only for numerical values */
986 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
988 if (get_mode_vector_elems(a->mode) > 1) {
989 /* vector arithmetic not implemented yet */
993 switch (get_mode_sort(a->mode))
995 case irms_int_number:
996 buffer = alloca(sc_get_buffer_length());
997 sc_neg(a->value, buffer);
998 return get_tarval_overflow(buffer, a->length, a->mode);
1000 case irms_float_number:
1001 fc_neg(a->value, NULL);
1002 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1012 tarval *tarval_add(tarval *a, tarval *b)
1019 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
1021 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(b->mode) > 1) {
1022 /* vector arithmetic not implemented yet */
1026 switch (get_mode_sort(a->mode))
1028 case irms_character:
1029 case irms_int_number:
1030 /* modes of a,b are equal, so result has mode of a as this might be the character */
1031 buffer = alloca(sc_get_buffer_length());
1032 sc_add(a->value, b->value, buffer);
1033 return get_tarval_overflow(buffer, a->length, a->mode);
1035 case irms_float_number:
1036 fc_add(a->value, b->value, NULL);
1037 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1047 tarval *tarval_sub(tarval *a, tarval *b)
1054 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
1056 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(b->mode) > 1) {
1057 /* vector arithmetic not implemented yet */
1060 switch (get_mode_sort(a->mode))
1062 case irms_character:
1063 case irms_int_number:
1064 /* modes of a,b are equal, so result has mode of a as this might be the character */
1065 buffer = alloca(sc_get_buffer_length());
1066 sc_sub(a->value, b->value, buffer);
1067 return get_tarval_overflow(buffer, a->length, a->mode);
1069 case irms_float_number:
1070 fc_sub(a->value, b->value, NULL);
1071 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1081 tarval *tarval_mul(tarval *a, tarval *b)
1088 assert((a->mode == b->mode) && mode_is_num(a->mode));
1090 if (get_mode_vector_elems(a->mode) > 1) {
1091 /* vector arithmetic not implemented yet */
1095 switch (get_mode_sort(a->mode))
1097 case irms_int_number:
1098 /* modes of a,b are equal */
1099 buffer = alloca(sc_get_buffer_length());
1100 sc_mul(a->value, b->value, buffer);
1101 return get_tarval_overflow(buffer, a->length, a->mode);
1103 case irms_float_number:
1104 fc_mul(a->value, b->value, NULL);
1105 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1113 * floating point division
1115 tarval *tarval_quo(tarval *a, tarval *b)
1120 assert((a->mode == b->mode) && mode_is_float(a->mode));
1122 if (get_mode_vector_elems(a->mode) > 1) {
1123 /* vector arithmetic not implemented yet */
1127 fc_div(a->value, b->value, NULL);
1128 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1133 * overflow is impossible, but look out for division by zero
1135 tarval *tarval_div(tarval *a, tarval *b)
1140 assert((a->mode == b->mode) && mode_is_int(a->mode));
1142 if (get_mode_vector_elems(a->mode) > 1) {
1143 /* vector arithmetic not implemented yet */
1148 if (b == get_mode_null(b->mode)) return tarval_bad;
1149 /* modes of a,b are equal */
1150 sc_div(a->value, b->value, NULL);
1151 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1156 * overflow is impossible, but look out for division by zero
1158 tarval *tarval_mod(tarval *a, tarval *b)
1163 assert((a->mode == b->mode) && mode_is_int(a->mode));
1165 if (get_mode_vector_elems(a->mode) > 1) {
1166 /* vector arithmetic not implemented yet */
1171 if (b == get_mode_null(b->mode)) return tarval_bad;
1172 /* modes of a,b are equal */
1173 sc_mod(a->value, b->value, NULL);
1174 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1180 tarval *tarval_abs(tarval *a)
1186 assert(mode_is_num(a->mode));
1188 if (get_mode_vector_elems(a->mode) > 1) {
1189 /* vector arithmetic not implemented yet */
1193 switch (get_mode_sort(a->mode))
1195 case irms_int_number:
1196 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1198 buffer = alloca(sc_get_buffer_length());
1199 sc_neg(a->value, buffer);
1200 return get_tarval_overflow(buffer, a->length, a->mode);
1204 case irms_float_number:
1205 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1207 fc_neg(a->value, NULL);
1208 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1221 tarval *tarval_and(tarval *a, tarval *b)
1226 assert(a->mode == b->mode);
1228 /* works even for vector modes */
1230 switch(get_mode_sort(a->mode))
1232 case irms_internal_boolean:
1233 return (a == tarval_b_false) ? a : b;
1235 case irms_int_number:
1236 sc_and(a->value, b->value, NULL);
1237 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1240 assert(0 && "operation not defined on mode");
1248 tarval *tarval_or (tarval *a, tarval *b)
1253 assert(a->mode == b->mode);
1255 /* works even for vector modes */
1257 switch (get_mode_sort(a->mode))
1259 case irms_internal_boolean:
1260 return (a == tarval_b_true) ? a : b;
1262 case irms_int_number:
1263 sc_or(a->value, b->value, NULL);
1264 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1267 assert(0 && "operation not defined on mode");
1273 * bitwise exclusive or (xor)
1275 tarval *tarval_eor(tarval *a, tarval *b)
1280 assert((a->mode == b->mode));
1282 /* works even for vector modes */
1284 switch (get_mode_sort(a->mode))
1286 case irms_internal_boolean:
1287 return (a == b)? tarval_b_false : tarval_b_true;
1289 case irms_int_number:
1290 sc_xor(a->value, b->value, NULL);
1291 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1294 assert(0 && "operation not defined on mode");
1300 * bitwise left shift
1302 tarval *tarval_shl(tarval *a, tarval *b)
1304 char *temp_val = NULL;
1308 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1310 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1311 /* vector arithmetic not implemented yet */
1315 if (get_mode_modulo_shift(a->mode) != 0)
1317 temp_val = alloca(sc_get_buffer_length());
1319 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1320 sc_mod(b->value, temp_val, temp_val);
1323 temp_val = (char*)b->value;
1325 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1326 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1330 * bitwise unsigned right shift
1332 tarval *tarval_shr(tarval *a, tarval *b)
1334 char *temp_val = NULL;
1338 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1340 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1341 /* vector arithmetic not implemented yet */
1345 if (get_mode_modulo_shift(a->mode) != 0)
1347 temp_val = alloca(sc_get_buffer_length());
1349 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1350 sc_mod(b->value, temp_val, temp_val);
1353 temp_val = (char*)b->value;
1355 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1356 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1360 * bitwise signed right shift
1362 tarval *tarval_shrs(tarval *a, tarval *b)
1364 char *temp_val = NULL;
1368 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1370 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1371 /* vector arithmetic not implemented yet */
1375 if (get_mode_modulo_shift(a->mode) != 0)
1377 temp_val = alloca(sc_get_buffer_length());
1379 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1380 sc_mod(b->value, temp_val, temp_val);
1383 temp_val = (char*)b->value;
1385 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1386 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1392 tarval *tarval_rot(tarval *a, tarval *b)
1394 char *temp_val = NULL;
1398 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1400 if (get_mode_vector_elems(a->mode) > 1 || get_mode_vector_elems(a->mode) > 1) {
1401 /* vector arithmetic not implemented yet */
1405 if (get_mode_modulo_shift(a->mode) != 0)
1407 temp_val = alloca(sc_get_buffer_length());
1409 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1410 sc_mod(b->value, temp_val, temp_val);
1413 temp_val = (char*)b->value;
1415 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1416 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1423 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1425 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1429 const tarval_mode_info *mode_info;
1430 const char *prefix, *suffix;
1434 mode_info = tv->mode->tv_priv;
1436 mode_info = &default_info;
1437 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1438 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1440 switch (get_mode_sort(tv->mode))
1442 case irms_int_number:
1443 case irms_character:
1444 switch (mode_info->mode_output) {
1447 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1451 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1457 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1460 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1462 case irms_float_number:
1463 switch (mode_info->mode_output) {
1465 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1468 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1473 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1477 case irms_reference:
1478 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1479 if (tv->value != NULL)
1480 if (tarval_is_entity(tv)) {
1481 if (get_entity_peculiarity((entity *)tv->value) != peculiarity_description)
1482 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1484 if (mode_info->mode_output == TVO_NATIVE)
1485 return snprintf(buf, len, "NULL");
1487 return snprintf(buf, len, "0");
1491 if (len > tv->length) {
1492 memcpy(buf, tv->value, tv->length);
1493 buf[tv->length] = '\0';
1497 memcpy(buf, tv->value, len-1);
1503 return snprintf(buf, len, "void");
1505 case irms_internal_boolean:
1506 switch (mode_info->mode_output) {
1512 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1516 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1519 case irms_control_flow:
1521 case irms_auxiliary:
1522 return snprintf(buf, len, "<BAD>");
1530 * Output of tarvals to stdio.
1532 int tarval_printf(tarval *tv) {
1536 res = tarval_snprintf(buf, sizeof(buf), tv);
1537 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1543 char *tarval_bitpattern(tarval *tv)
1549 * access to the bitpattern
1551 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1553 switch (get_mode_sort(tv->mode)) {
1554 case irms_int_number:
1555 case irms_character:
1556 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1558 case irms_float_number:
1559 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1567 * Specify the output options of one mode.
1569 * This functions stores the modinfo, so DO NOT DESTROY it.
1571 * Returns zero on success.
1573 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1577 mode->tv_priv = modeinfo;
1582 * Returns the output options of one mode.
1584 * This functions returns the modinfo of a given mode.
1586 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1590 return mode->tv_priv;
1594 * Identifying tarvals values for algebraic simplifications.
1597 * - TV_CLASSIFY_NULL for additive neutral,
1598 * - TV_CLASSIFY_ONE for multiplicative neutral,
1599 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1600 * - TV_CLASSIFY_OTHER else
1602 tarval_classification_t tarval_classify(tarval *tv)
1605 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1607 if (tv == get_mode_null(tv->mode))
1608 return TV_CLASSIFY_NULL;
1609 else if (tv == get_mode_one(tv->mode))
1610 return TV_CLASSIFY_ONE;
1611 else if ((get_mode_sort(tv->mode) == irms_int_number)
1612 && (tv == new_tarval_from_long(-1, tv->mode)))
1613 return TV_CLASSIFY_ALL_ONE;
1615 return TV_CLASSIFY_OTHER;
1619 * default mode_info for output as HEX
1621 static const tarval_mode_info hex_output = {
1628 * default mode_info for output as reference
1630 static const tarval_mode_info reference_output = {
1638 * Initialization of the tarval module: called before init_mode()
1640 void init_tarval_1(void)
1643 /* initialize the sets holding the tarvals with a comparison function and
1644 * an initial size, which is the expected number of constants */
1645 tarvals = new_set(memcmp, N_CONSTANTS);
1646 values = new_set(memcmp, N_CONSTANTS);
1647 /* init strcalc with precision of 68 to support floating point values with 64
1648 * bit mantissa (needs extra bits for rounding and overflow) */
1654 * Initialization of the tarval module: called after init_mode()
1656 void init_tarval_2(void)
1660 tarval_bad->mode = mode_BAD;
1661 tarval_undefined->mode = mode_ANY;
1662 tarval_b_true->mode = mode_b;
1663 tarval_b_false->mode = mode_b;
1664 tarval_P_void->mode = mode_P;
1667 * assign output modes that are compatible with the
1668 * old implementation: Hex output
1670 tarval_set_mode_output_option(mode_U, &hex_output);
1671 tarval_set_mode_output_option(mode_C, &hex_output);
1672 tarval_set_mode_output_option(mode_Bs, &hex_output);
1673 tarval_set_mode_output_option(mode_Bu, &hex_output);
1674 tarval_set_mode_output_option(mode_Hs, &hex_output);
1675 tarval_set_mode_output_option(mode_Hu, &hex_output);
1676 tarval_set_mode_output_option(mode_Is, &hex_output);
1677 tarval_set_mode_output_option(mode_Iu, &hex_output);
1678 tarval_set_mode_output_option(mode_Ls, &hex_output);
1679 tarval_set_mode_output_option(mode_Lu, &hex_output);
1680 tarval_set_mode_output_option(mode_P, &reference_output);
1683 /* free all memory occupied by tarval. */
1684 void finish_tarval(void) {
1687 del_set(tarvals); tarvals = NULL;
1688 del_set(values); values = NULL;
1691 /****************************************************************************
1693 ****************************************************************************/