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() */
32 # include <string.h> /* nice things for strings */
35 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
48 #include "set.h" /* to store tarvals in */
49 #include "entity_t.h" /* needed to store pointers to entities */
51 #include "irnode.h" /* defines boolean return values (pnc_number)*/
56 #include "firm_common.h"
58 /** Size of hash tables. Should correspond to average number of distinct constant
60 #define N_CONSTANTS 2048
62 /* get the integer overflow mode */
63 #define GET_OVERFLOW_MODE() int_overflow_mode
65 /* unused, float to int doesn't work yet */
66 enum float_to_int_mode {
71 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
73 #define SWITCH_NOINFINITY 0
74 #define SWITCH_NODENORMALS 0
76 /****************************************************************************
77 * local definitions and macros
78 ****************************************************************************/
80 # define TARVAL_VERIFY(a) tarval_verify((a))
82 # define TARVAL_VERIFY(a) ((void)0)
85 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
86 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
88 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
89 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
91 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
93 static long long count = 0;
94 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
96 # define ANNOUNCE() ((void)0)
98 /****************************************************************************
100 ****************************************************************************/
101 static struct set *tarvals = NULL; /* container for tarval structs */
102 static struct set *values = NULL; /* container for values */
103 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
105 /****************************************************************************
107 ****************************************************************************/
109 static int hash_val(const void *value, unsigned int length);
110 static int hash_tv(tarval *tv);
111 static void _fail_verify(tarval *tv, const char* file, int line)
113 /* print a memory image of the tarval and throw an assertion */
115 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
117 printf("%s:%d: Invalid tarval (null)", file, line);
121 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
124 INLINE static void tarval_verify(tarval *tv)
130 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
131 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
133 if (!FIND_TARVAL(tv)) fail_verify(tv);
134 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
140 /** Hash a tarval. */
141 static int hash_tv(tarval *tv) {
142 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
145 /** Hash a value. Treat it as a byte array. */
146 static int hash_val(const void *value, unsigned int length) {
148 unsigned int hash = 0;
150 /* scramble the byte - array */
151 for (i = 0; i < length; i++)
153 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
154 hash += (hash << 11) ^ (hash >> 17);
160 /** finds tarval with value/mode or creates new tarval */
161 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
169 /* if there already is such a value, it is returned, else value
170 * is copied into the set */
171 tv.value = INSERT_VALUE(value, length);
175 /* if there is such a tarval, it is returned, else tv is copied
177 return (tarval *)INSERT_TARVAL(&tv);
183 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
185 switch (get_mode_sort(mode))
187 case irms_int_number:
188 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
189 switch (GET_OVERFLOW_MODE()) {
190 case TV_OVERFLOW_SATURATE:
191 return get_mode_max(mode);
192 case TV_OVERFLOW_WRAP:
194 char *temp = alloca(sc_get_buffer_length());
195 sc_val_from_ulong(-1, temp);
196 sc_and(temp, value, temp);
197 /* the sc_ module expects that all bits are set ... */
198 sign_extend(temp, mode);
199 return get_tarval(temp, length, mode);
201 case TV_OVERFLOW_BAD:
204 return get_tarval(value, length, mode);
207 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
208 switch (GET_OVERFLOW_MODE()) {
209 case TV_OVERFLOW_SATURATE:
210 return get_mode_min(mode);
211 case TV_OVERFLOW_WRAP:
213 char *temp = alloca(sc_get_buffer_length());
214 sc_val_from_ulong(-1, temp);
215 sc_and(temp, value, temp);
216 return get_tarval(temp, length, mode);
218 case TV_OVERFLOW_BAD:
221 return get_tarval(value, length, mode);
226 case irms_float_number:
227 if (SWITCH_NOINFINITY && fc_is_inf(value))
229 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
232 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
234 return get_mode_null(mode);
241 return get_tarval(value, length, mode);
245 * public variables declared in tv.h
247 static tarval reserved_tv[4];
249 tarval *tarval_bad = &reserved_tv[0];
250 tarval *tarval_undefined = &reserved_tv[1];
251 tarval *tarval_b_false = &reserved_tv[2];
252 tarval *tarval_b_true = &reserved_tv[3];
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);
299 /* same as integer modes */
300 case irms_int_number:
302 sc_val_from_str(str, len, NULL, mode);
303 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), 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)
318 switch(get_mode_sort(mode))
320 case irms_internal_boolean:
321 /* XXX C semantics ! */
322 return l ? tarval_b_true : tarval_b_false ;
325 /* same as integer modes */
326 case irms_int_number:
328 sc_val_from_long(l, NULL);
329 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
331 case irms_float_number:
332 return new_tarval_from_double((long double)l, mode);
335 assert(0 && "unsupported mode sort");
340 /* returns non-zero if can be converted to long */
341 int tarval_is_long(tarval *tv)
343 mode_sort sort = get_mode_sort(tv->mode);
346 if (sort != irms_int_number && sort != irms_character) 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 get_tarval_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 get_tarval_double(tarval *tv)
401 assert(tarval_is_double(tv));
403 return fc_val_to_float(tv->value);
408 * Access routines for tarval fields ========================================
411 /* get the mode of the tarval */
412 ir_mode *(get_tarval_mode)(const tarval *tv) {
413 return _get_tarval_mode(tv);
417 * Special value query functions ============================================
419 * These functions calculate and return a tarval representing the requested
421 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
422 * functions, but these are stored on initialization of the irmode module and
423 * therefore the irmode functions should be prefered to the functions below.
426 tarval *(get_tarval_bad)(void) {
427 return _get_tarval_bad();
430 tarval *(get_tarval_undefined)(void) {
431 return _get_tarval_undefined();
434 tarval *(get_tarval_b_false)(void) {
435 return _get_tarval_b_false();
438 tarval *(get_tarval_b_true)(void) {
439 return _get_tarval_b_true();
442 tarval *get_tarval_max(ir_mode *mode)
447 if (get_mode_n_vector_elems(mode) > 1) {
448 /* vector arithmetic not implemented yet */
452 switch(get_mode_sort(mode))
455 case irms_control_flow:
461 case irms_internal_boolean:
462 return tarval_b_true;
464 case irms_float_number:
465 switch(get_mode_size_bits(mode))
468 fc_get_max(8, 23, NULL);
471 fc_get_max(11, 52, NULL);
474 fc_get_max(15, 64, NULL);
477 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
479 case irms_int_number:
481 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
482 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
487 tarval *get_tarval_min(ir_mode *mode)
492 if (get_mode_n_vector_elems(mode) > 1) {
493 /* vector arithmetic not implemented yet */
497 switch(get_mode_sort(mode))
500 case irms_control_flow:
506 case irms_internal_boolean:
507 return tarval_b_false;
509 case irms_float_number:
510 switch(get_mode_size_bits(mode))
513 fc_get_min(8, 23, NULL);
516 fc_get_min(11, 52, NULL);
519 fc_get_min(15, 64, NULL);
522 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
524 case irms_int_number:
526 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
527 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
532 static long _null_value;
534 tarval *get_tarval_null(ir_mode *mode)
539 if (get_mode_n_vector_elems(mode) > 1) {
540 /* vector arithmetic not implemented yet */
544 switch(get_mode_sort(mode))
546 case irms_control_flow:
549 case irms_internal_boolean:
553 case irms_float_number:
554 return new_tarval_from_double(0.0, mode);
556 case irms_int_number:
558 return new_tarval_from_long(0l, mode);
561 return new_tarval_from_long(_null_value, mode);
566 tarval *get_tarval_one(ir_mode *mode)
571 if (get_mode_n_vector_elems(mode) > 1) {
572 /* vector arithmetic not implemented yet */
576 switch(get_mode_sort(mode))
578 case irms_control_flow:
581 case irms_internal_boolean:
586 case irms_float_number:
587 return new_tarval_from_double(1.0, mode);
589 case irms_int_number:
591 return new_tarval_from_long(1l, mode);
597 tarval *get_tarval_minus_one(ir_mode *mode)
602 if (get_mode_n_vector_elems(mode) > 1) {
603 /* vector arithmetic not implemented yet */
607 switch(get_mode_sort(mode))
609 case irms_control_flow:
612 case irms_internal_boolean:
617 case irms_float_number:
618 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
620 case irms_int_number:
622 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
627 tarval *get_tarval_nan(ir_mode *mode)
632 if (get_mode_n_vector_elems(mode) > 1) {
633 /* vector arithmetic not implemented yet */
637 if (get_mode_sort(mode) == irms_float_number) {
638 switch(get_mode_size_bits(mode))
641 fc_get_qnan(8, 23, NULL);
644 fc_get_qnan(11, 52, NULL);
647 fc_get_qnan(15, 64, NULL);
650 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
653 assert(0 && "tarval is not floating point");
658 tarval *get_tarval_plus_inf(ir_mode *mode)
663 if (get_mode_n_vector_elems(mode) > 1) {
664 /* vector arithmetic not implemented yet */
668 if (get_mode_sort(mode) == irms_float_number) {
669 switch(get_mode_size_bits(mode))
672 fc_get_plusinf(8, 23, NULL);
675 fc_get_plusinf(11, 52, NULL);
678 fc_get_plusinf(15, 64, NULL);
681 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
684 assert(0 && "tarval is not floating point");
689 tarval *get_tarval_minus_inf(ir_mode *mode)
694 if (get_mode_n_vector_elems(mode) > 1) {
695 /* vector arithmetic not implemented yet */
699 if (get_mode_sort(mode) == irms_float_number) {
700 switch(get_mode_size_bits(mode))
703 fc_get_minusinf(8, 23, NULL);
706 fc_get_minusinf(11, 52, NULL);
709 fc_get_minusinf(15, 64, NULL);
712 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
715 assert(0 && "tarval is not floating point");
721 * Arithmethic operations on tarvals ========================================
725 * test if negative number, 1 means 'yes'
727 int tarval_is_negative(tarval *a)
732 if (get_mode_n_vector_elems(a->mode) > 1) {
733 /* vector arithmetic not implemented yet */
734 assert(0 && "tarval_is_negative is not allowed for vector modes");
738 switch (get_mode_sort(a->mode))
740 case irms_int_number:
741 if (!mode_is_signed(a->mode)) return 0;
743 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
745 case irms_float_number:
746 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
749 assert(0 && "not implemented");
755 * test if null, 1 means 'yes'
757 int tarval_is_null(tarval *a)
759 ir_mode *m = get_tarval_mode(a);
761 return a == get_tarval_null(m);
765 * test if one, 1 means 'yes'
767 int tarval_is_one(tarval *a)
769 ir_mode *m = get_tarval_mode(a);
771 return a == get_tarval_one(m);
777 pn_Cmp tarval_cmp(tarval *a, tarval *b)
783 if (a == tarval_bad || b == tarval_bad) {
784 assert(0 && "Comparison with tarval_bad");
788 if (a == tarval_undefined || b == tarval_undefined)
791 if (a->mode != b->mode)
794 if (get_mode_n_vector_elems(a->mode) > 1) {
795 /* vector arithmetic not implemented yet */
796 assert(0 && "cmp not implemented for vector modes");
799 /* Here the two tarvals are unequal and of the same mode */
800 switch (get_mode_sort(a->mode))
802 case irms_control_flow:
810 case irms_float_number:
812 * BEWARE: we cannot compare a == b here, because
813 * a NaN is always Unordered to any other value, even to itself!
815 switch (fc_comp(a->value, b->value)) {
816 case -1: return pn_Cmp_Lt;
817 case 0: return pn_Cmp_Eq;
818 case 1: return pn_Cmp_Gt;
819 case 2: return pn_Cmp_Uo;
820 default: return pn_Cmp_False;
822 case irms_int_number:
826 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
828 case irms_internal_boolean:
831 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
837 * convert to other mode
839 tarval *tarval_convert_to(tarval *src, ir_mode *m)
847 if (src->mode == m) return src;
849 if (get_mode_n_vector_elems(src->mode) > 1) {
850 /* vector arithmetic not implemented yet */
854 switch (get_mode_sort(src->mode))
856 case irms_control_flow:
861 /* cast float to something */
862 case irms_float_number:
863 switch (get_mode_sort(m)) {
864 case irms_float_number:
865 switch (get_mode_size_bits(m))
868 fc_cast(src->value, 8, 23, NULL);
871 fc_cast(src->value, 11, 52, NULL);
874 fc_cast(src->value, 15, 64, NULL);
879 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
881 case irms_int_number:
882 switch (GET_FLOAT_TO_INT_MODE())
885 fc_int(src->value, NULL);
888 fc_rnd(src->value, NULL);
894 /* FIXME: floating point unit can't produce a value in integer
896 * an intermediate representation is needed here first. */
897 /* return get_tarval(); */
901 /* the rest can't be converted */
906 /* cast int to something */
907 case irms_int_number:
908 switch (get_mode_sort(m)) {
909 case irms_int_number:
911 return get_tarval_overflow(src->value, src->length, m);
913 case irms_internal_boolean:
914 /* XXX C semantics */
915 if (src == get_mode_null(src->mode)) return tarval_b_false;
916 else return tarval_b_true;
918 case irms_float_number:
919 /* XXX floating point unit does not understand internal integer
920 * representation, convert to string first, then create float from
922 buffer = alloca(100);
923 /* decimal string representation because hexadecimal output is
924 * interpreted unsigned by fc_val_from_str, so this is a HACK */
925 snprintf(buffer, 100, "%s",
926 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
927 buffer[100 - 1] = '\0';
928 switch (get_mode_size_bits(m))
931 fc_val_from_str(buffer, 0, 8, 23, NULL);
934 fc_val_from_str(buffer, 0, 11, 52, NULL);
937 fc_val_from_str(buffer, 0, 15, 64, NULL);
940 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
943 /* allow 0 to be casted */
944 if (src == get_mode_null(src->mode))
945 return get_mode_null(m);
953 case irms_internal_boolean:
954 switch (get_mode_sort(m))
956 case irms_int_number:
957 if (src == tarval_b_true) return get_mode_one(m);
958 else return get_mode_null(m);
977 tarval *tarval_not(tarval *a)
984 /* works for vector mode without changes */
986 switch (get_mode_sort(a->mode))
988 case irms_int_number:
989 buffer = alloca(sc_get_buffer_length());
990 sc_not(a->value, buffer);
991 return get_tarval(buffer, a->length, a->mode);
993 case irms_internal_boolean:
994 if (a == tarval_b_true)
995 return tarval_b_false;
996 if (a == tarval_b_false)
997 return tarval_b_true;
1001 assert(0 && "bitwise negation is only allowed for integer and boolean");
1007 * arithmetic negation
1009 tarval *tarval_neg(tarval *a)
1015 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1017 /* note: negation is allowed even for unsigned modes. */
1019 if (get_mode_n_vector_elems(a->mode) > 1) {
1020 /* vector arithmetic not implemented yet */
1024 switch (get_mode_sort(a->mode))
1026 case irms_int_number:
1027 buffer = alloca(sc_get_buffer_length());
1028 sc_neg(a->value, buffer);
1029 return get_tarval_overflow(buffer, a->length, a->mode);
1031 case irms_float_number:
1032 fc_neg(a->value, NULL);
1033 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1043 tarval *tarval_add(tarval *a, tarval *b)
1050 assert(a->mode == b->mode);
1052 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1053 /* vector arithmetic not implemented yet */
1057 switch (get_mode_sort(a->mode))
1059 case irms_character:
1060 case irms_int_number:
1061 /* modes of a,b are equal, so result has mode of a as this might be the character */
1062 buffer = alloca(sc_get_buffer_length());
1063 sc_add(a->value, b->value, buffer);
1064 return get_tarval_overflow(buffer, a->length, a->mode);
1066 case irms_float_number:
1067 fc_add(a->value, b->value, NULL);
1068 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1078 tarval *tarval_sub(tarval *a, tarval *b)
1085 assert(a->mode == b->mode);
1087 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1088 /* vector arithmetic not implemented yet */
1091 switch (get_mode_sort(a->mode))
1093 case irms_character:
1094 case irms_int_number:
1095 /* modes of a,b are equal, so result has mode of a as this might be the character */
1096 buffer = alloca(sc_get_buffer_length());
1097 sc_sub(a->value, b->value, buffer);
1098 return get_tarval_overflow(buffer, a->length, a->mode);
1100 case irms_float_number:
1101 fc_sub(a->value, b->value, NULL);
1102 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1112 tarval *tarval_mul(tarval *a, tarval *b)
1119 assert(a->mode == b->mode);
1121 if (get_mode_n_vector_elems(a->mode) > 1) {
1122 /* vector arithmetic not implemented yet */
1126 switch (get_mode_sort(a->mode))
1128 case irms_int_number:
1129 /* modes of a,b are equal */
1130 buffer = alloca(sc_get_buffer_length());
1131 sc_mul(a->value, b->value, buffer);
1132 return get_tarval_overflow(buffer, a->length, a->mode);
1134 case irms_float_number:
1135 fc_mul(a->value, b->value, NULL);
1136 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1144 * floating point division
1146 tarval *tarval_quo(tarval *a, tarval *b)
1151 assert((a->mode == b->mode) && mode_is_float(a->mode));
1153 if (get_mode_n_vector_elems(a->mode) > 1) {
1154 /* vector arithmetic not implemented yet */
1158 fc_div(a->value, b->value, NULL);
1159 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1164 * overflow is impossible, but look out for division by zero
1166 tarval *tarval_div(tarval *a, tarval *b)
1171 assert((a->mode == b->mode) && mode_is_int(a->mode));
1173 if (get_mode_n_vector_elems(a->mode) > 1) {
1174 /* vector arithmetic not implemented yet */
1179 if (b == get_mode_null(b->mode)) return tarval_bad;
1180 /* modes of a,b are equal */
1181 sc_div(a->value, b->value, NULL);
1182 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1187 * overflow is impossible, but look out for division by zero
1189 tarval *tarval_mod(tarval *a, tarval *b)
1194 assert((a->mode == b->mode) && mode_is_int(a->mode));
1196 if (get_mode_n_vector_elems(a->mode) > 1) {
1197 /* vector arithmetic not implemented yet */
1202 if (b == get_mode_null(b->mode)) return tarval_bad;
1203 /* modes of a,b are equal */
1204 sc_mod(a->value, b->value, NULL);
1205 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1211 tarval *tarval_abs(tarval *a)
1217 assert(mode_is_num(a->mode));
1219 if (get_mode_n_vector_elems(a->mode) > 1) {
1220 /* vector arithmetic not implemented yet */
1224 switch (get_mode_sort(a->mode))
1226 case irms_int_number:
1227 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1229 buffer = alloca(sc_get_buffer_length());
1230 sc_neg(a->value, buffer);
1231 return get_tarval_overflow(buffer, a->length, a->mode);
1235 case irms_float_number:
1236 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1238 fc_neg(a->value, NULL);
1239 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1252 tarval *tarval_and(tarval *a, tarval *b)
1257 assert(a->mode == b->mode);
1259 /* works even for vector modes */
1261 switch(get_mode_sort(a->mode))
1263 case irms_internal_boolean:
1264 return (a == tarval_b_false) ? a : b;
1266 case irms_int_number:
1267 sc_and(a->value, b->value, NULL);
1268 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1271 assert(0 && "operation not defined on mode");
1279 tarval *tarval_or (tarval *a, tarval *b)
1284 assert(a->mode == b->mode);
1286 /* works even for vector modes */
1288 switch (get_mode_sort(a->mode))
1290 case irms_internal_boolean:
1291 return (a == tarval_b_true) ? a : b;
1293 case irms_int_number:
1294 sc_or(a->value, b->value, NULL);
1295 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1298 assert(0 && "operation not defined on mode");
1304 * bitwise exclusive or (xor)
1306 tarval *tarval_eor(tarval *a, tarval *b)
1311 assert((a->mode == b->mode));
1313 /* works even for vector modes */
1315 switch (get_mode_sort(a->mode))
1317 case irms_internal_boolean:
1318 return (a == b)? tarval_b_false : tarval_b_true;
1320 case irms_int_number:
1321 sc_xor(a->value, b->value, NULL);
1322 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1325 assert(0 && "operation not defined on mode");
1331 * bitwise left shift
1333 tarval *tarval_shl(tarval *a, tarval *b)
1335 char *temp_val = NULL;
1339 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1341 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1342 /* vector arithmetic not implemented yet */
1346 if (get_mode_modulo_shift(a->mode) != 0)
1348 temp_val = alloca(sc_get_buffer_length());
1350 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1351 sc_mod(b->value, temp_val, temp_val);
1354 temp_val = (char*)b->value;
1356 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1357 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1361 * bitwise unsigned right shift
1363 tarval *tarval_shr(tarval *a, tarval *b)
1365 char *temp_val = NULL;
1369 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1371 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1372 /* vector arithmetic not implemented yet */
1376 if (get_mode_modulo_shift(a->mode) != 0)
1378 temp_val = alloca(sc_get_buffer_length());
1380 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1381 sc_mod(b->value, temp_val, temp_val);
1384 temp_val = (char*)b->value;
1386 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1387 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1391 * bitwise signed right shift
1393 tarval *tarval_shrs(tarval *a, tarval *b)
1395 char *temp_val = NULL;
1399 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1401 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1402 /* vector arithmetic not implemented yet */
1406 if (get_mode_modulo_shift(a->mode) != 0)
1408 temp_val = alloca(sc_get_buffer_length());
1410 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1411 sc_mod(b->value, temp_val, temp_val);
1414 temp_val = (char*)b->value;
1416 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1417 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1423 tarval *tarval_rot(tarval *a, tarval *b)
1425 char *temp_val = NULL;
1429 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1431 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1432 /* vector arithmetic not implemented yet */
1436 if (get_mode_modulo_shift(a->mode) != 0)
1438 temp_val = alloca(sc_get_buffer_length());
1440 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1441 sc_mod(b->value, temp_val, temp_val);
1444 temp_val = (char*)b->value;
1446 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1447 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1451 * carry flag of the last operation
1453 int tarval_carry(void)
1455 return sc_had_carry();
1461 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1463 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1467 const tarval_mode_info *mode_info;
1468 const char *prefix, *suffix;
1472 mode_info = tv->mode->tv_priv;
1474 mode_info = &default_info;
1475 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1476 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1478 switch (get_mode_sort(tv->mode))
1480 case irms_reference:
1481 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1483 case irms_int_number:
1484 case irms_character:
1485 switch (mode_info->mode_output) {
1488 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1492 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1498 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1501 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1503 case irms_float_number:
1504 switch (mode_info->mode_output) {
1506 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1509 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1514 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1518 case irms_internal_boolean:
1519 switch (mode_info->mode_output) {
1525 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1529 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1532 case irms_control_flow:
1534 case irms_auxiliary:
1535 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1543 * Output of tarvals to stdio.
1545 int tarval_printf(tarval *tv) {
1549 res = tarval_snprintf(buf, sizeof(buf), tv);
1550 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1556 char *get_tarval_bitpattern(tarval *tv)
1559 int n = get_mode_size_bits(tv->mode);
1560 int bytes = (n + 7) / 8;
1561 char *res = xmalloc((n + 1) * sizeof(char));
1564 for(i = 0; i < bytes; i++) {
1565 byte = get_tarval_sub_bits(tv, i);
1566 for(j = 1; j < 256; j <<= 1)
1568 res[pos++] = j & byte ? '1' : '0';
1577 * access to the bitpattern
1579 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1581 switch (get_mode_sort(tv->mode)) {
1582 case irms_int_number:
1583 case irms_character:
1584 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1586 case irms_float_number:
1587 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1595 * Specify the output options of one mode.
1597 * This functions stores the modinfo, so DO NOT DESTROY it.
1599 * Returns zero on success.
1601 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1605 mode->tv_priv = modeinfo;
1610 * Returns the output options of one mode.
1612 * This functions returns the mode info of a given mode.
1614 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1618 return mode->tv_priv;
1622 * Identifying tarvals values for algebraic simplifications.
1625 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1626 * - TV_CLASSIFY_ONE for multiplicative neutral,
1627 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1628 * - TV_CLASSIFY_OTHER else
1630 tarval_classification_t classify_tarval(tarval *tv)
1633 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1635 if (tv == get_mode_null(tv->mode))
1636 return TV_CLASSIFY_NULL;
1637 else if (tv == get_mode_one(tv->mode))
1638 return TV_CLASSIFY_ONE;
1639 else if ((get_mode_sort(tv->mode) == irms_int_number)
1640 && (tv == new_tarval_from_long(-1, tv->mode)))
1641 return TV_CLASSIFY_ALL_ONE;
1643 return TV_CLASSIFY_OTHER;
1647 * Returns non-zero if a given (integer) tarval has only one single bit
1650 int is_single_bit_tarval(tarval *tv) {
1654 if (!tv || tv == tarval_bad) return 0;
1655 if (! mode_is_int(tv->mode)) return 0;
1657 l = get_mode_size_bytes(tv->mode);
1658 for (bits = 0, i = l - 1; i >= 0; --i) {
1659 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1661 /* check for more than one bit in these */
1673 * Sets the overflow mode for integer operations.
1675 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1676 int_overflow_mode = ov_mode;
1680 * Get the overflow mode for integer operations.
1682 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1683 return int_overflow_mode;
1687 * default mode_info for output as HEX
1689 static const tarval_mode_info hex_output = {
1696 * Initialization of the tarval module: called before init_mode()
1698 void init_tarval_1(long null_value)
1701 _null_value = null_value;
1703 /* initialize the sets holding the tarvals with a comparison function and
1704 * an initial size, which is the expected number of constants */
1705 tarvals = new_set(memcmp, N_CONSTANTS);
1706 values = new_set(memcmp, N_CONSTANTS);
1707 /* init strcalc with precision of 68 to support floating point values with 64
1708 * bit mantissa (needs extra bits for rounding and overflow) */
1714 * Initialization of the tarval module: called after init_mode()
1716 void init_tarval_2(void)
1720 tarval_bad->kind = k_tarval;
1721 tarval_bad->mode = mode_BAD;
1722 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1724 tarval_undefined->kind = k_tarval;
1725 tarval_undefined->mode = mode_ANY;
1726 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1728 tarval_b_true->kind = k_tarval;
1729 tarval_b_true->mode = mode_b;
1730 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1732 tarval_b_false->kind = k_tarval;
1733 tarval_b_false->mode = mode_b;
1734 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1737 * assign output modes that are compatible with the
1738 * old implementation: Hex output
1740 set_tarval_mode_output_option(mode_U, &hex_output);
1741 set_tarval_mode_output_option(mode_C, &hex_output);
1742 set_tarval_mode_output_option(mode_Bs, &hex_output);
1743 set_tarval_mode_output_option(mode_Bu, &hex_output);
1744 set_tarval_mode_output_option(mode_Hs, &hex_output);
1745 set_tarval_mode_output_option(mode_Hu, &hex_output);
1746 set_tarval_mode_output_option(mode_Is, &hex_output);
1747 set_tarval_mode_output_option(mode_Iu, &hex_output);
1748 set_tarval_mode_output_option(mode_Ls, &hex_output);
1749 set_tarval_mode_output_option(mode_Lu, &hex_output);
1750 set_tarval_mode_output_option(mode_P, &hex_output);
1753 /* free all memory occupied by tarval. */
1754 void finish_tarval(void) {
1757 del_set(tarvals); tarvals = NULL;
1758 del_set(values); values = NULL;
1761 /****************************************************************************
1763 ****************************************************************************/