3 * File name: ir/tv/tv.c
4 * Purpose: Representation of and static computations on target machine
10 * Copyright: (c) 2003-2007 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 */
42 #include "set.h" /* to store tarvals in */
43 #include "entity_t.h" /* needed to store pointers to entities */
45 #include "irnode.h" /* defines boolean return values (pnc_number)*/
50 #include "firm_common.h"
52 /** Size of hash tables. Should correspond to average number of distinct constant
54 #define N_CONSTANTS 2048
56 /* get the integer overflow mode */
57 #define GET_OVERFLOW_MODE() int_overflow_mode
59 /* unused, float to int doesn't work yet */
60 enum float_to_int_mode {
65 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
67 #define SWITCH_NOINFINITY 0
68 #define SWITCH_NODENORMALS 0
70 /****************************************************************************
71 * local definitions and macros
72 ****************************************************************************/
74 # define TARVAL_VERIFY(a) tarval_verify((a))
76 # define TARVAL_VERIFY(a) ((void)0)
79 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
80 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
82 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
83 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
85 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
87 /****************************************************************************
89 ****************************************************************************/
90 static struct set *tarvals = NULL; /* container for tarval structs */
91 static struct set *values = NULL; /* container for values */
92 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
94 /****************************************************************************
96 ****************************************************************************/
98 static int hash_val(const void *value, unsigned int length);
99 static int hash_tv(tarval *tv);
100 static void _fail_verify(tarval *tv, const char* file, int line)
102 /* print a memory image of the tarval and throw an assertion */
104 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
106 printf("%s:%d: Invalid tarval (null)", file, line);
110 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
113 INLINE static void tarval_verify(tarval *tv)
119 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
120 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
122 if (!FIND_TARVAL(tv)) fail_verify(tv);
123 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
129 /** Hash a tarval. */
130 static int hash_tv(tarval *tv) {
131 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
134 /** Hash a value. Treat it as a byte array. */
135 static int hash_val(const void *value, unsigned int length) {
137 unsigned int hash = 0;
139 /* scramble the byte - array */
140 for (i = 0; i < length; ++i) {
141 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
142 hash += (hash << 11) ^ (hash >> 17);
148 /** finds tarval with value/mode or creates new tarval */
149 static tarval *get_tarval(const void *value, int length, ir_mode *mode) {
156 /* if there already is such a value, it is returned, else value
157 * is copied into the set */
158 tv.value = INSERT_VALUE(value, length);
162 /* if there is such a tarval, it is returned, else tv is copied
164 return (tarval *)INSERT_TARVAL(&tv);
170 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
172 switch (get_mode_sort(mode)) {
173 case irms_int_number:
174 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
175 switch (GET_OVERFLOW_MODE()) {
176 case TV_OVERFLOW_SATURATE:
177 return get_mode_max(mode);
178 case TV_OVERFLOW_WRAP:
180 char *temp = alloca(sc_get_buffer_length());
181 sc_val_from_ulong(-1, temp);
182 sc_and(temp, value, temp);
183 /* the sc_ module expects that all bits are set ... */
184 sign_extend(temp, mode);
185 return get_tarval(temp, length, mode);
187 case TV_OVERFLOW_BAD:
190 return get_tarval(value, length, mode);
193 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
194 switch (GET_OVERFLOW_MODE()) {
195 case TV_OVERFLOW_SATURATE:
196 return get_mode_min(mode);
197 case TV_OVERFLOW_WRAP: {
198 char *temp = alloca(sc_get_buffer_length());
199 sc_val_from_ulong(-1, temp);
200 sc_and(temp, value, temp);
201 return get_tarval(temp, length, mode);
203 case TV_OVERFLOW_BAD:
206 return get_tarval(value, length, mode);
211 case irms_float_number:
212 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
213 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
216 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
217 return get_mode_null(mode);
224 return get_tarval(value, length, mode);
228 * public variables declared in tv.h
230 static tarval reserved_tv[4];
232 tarval *tarval_bad = &reserved_tv[0];
233 tarval *tarval_undefined = &reserved_tv[1];
234 tarval *tarval_b_false = &reserved_tv[2];
235 tarval *tarval_b_true = &reserved_tv[3];
238 * public functions declared in tv.h
242 * Constructors =============================================================
244 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
250 switch (get_mode_sort(mode)) {
251 case irms_control_flow:
257 case irms_internal_boolean:
258 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
259 if (strcasecmp(str, "true")) return tarval_b_true;
260 else if (strcasecmp(str, "false")) return tarval_b_true;
262 /* XXX This is C semantics */
263 return atoi(str) ? tarval_b_true : tarval_b_false;
265 case irms_float_number:
266 switch(get_mode_size_bits(mode)) {
268 fc_val_from_str(str, len, 8, 23, NULL);
271 fc_val_from_str(str, len, 11, 52, NULL);
274 fc_val_from_str(str, len, 15, 64, NULL);
277 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
280 /* same as integer modes */
281 case irms_int_number:
283 sc_val_from_str(str, len, NULL, mode);
284 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
287 assert(0); /* can't be reached, can it? */
292 * helper function, create a tarval from long
294 tarval *new_tarval_from_long(long l, ir_mode *mode) {
297 switch(get_mode_sort(mode)) {
298 case irms_internal_boolean:
299 /* XXX C semantics ! */
300 return l ? tarval_b_true : tarval_b_false ;
303 /* same as integer modes */
304 case irms_int_number:
306 sc_val_from_long(l, NULL);
307 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
309 case irms_float_number:
310 return new_tarval_from_double((long double)l, mode);
313 assert(0 && "unsupported mode sort");
318 /* returns non-zero if can be converted to long */
319 int tarval_is_long(tarval *tv) {
320 mode_sort sort = get_mode_sort(tv->mode);
322 if (sort != irms_int_number && sort != irms_character) return 0;
324 if (get_mode_size_bits(tv->mode) > (sizeof(long) << 3)) {
325 /* the value might be too big to fit in a long */
326 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
327 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
328 /* really doesn't fit */
335 /* this might overflow the machine's long, so use only with small values */
336 long get_tarval_long(tarval* tv) {
337 assert(tarval_is_long(tv) && "tarval too big to fit in long");
339 return sc_val_to_long(tv->value);
342 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
343 assert(mode && (get_mode_sort(mode) == irms_float_number));
345 switch (get_mode_size_bits(mode)) {
347 fc_val_from_float(d, 8, 23, NULL);
350 fc_val_from_float(d, 11, 52, NULL);
353 fc_val_from_float(d, 15, 64, NULL);
356 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
359 /* returns non-zero if can be converted to double */
360 int tarval_is_double(tarval *tv) {
363 return (get_mode_sort(tv->mode) == irms_float_number);
366 long double get_tarval_double(tarval *tv) {
367 assert(tarval_is_double(tv));
369 return fc_val_to_float(tv->value);
374 * Access routines for tarval fields ========================================
377 /* get the mode of the tarval */
378 ir_mode *(get_tarval_mode)(const tarval *tv) {
379 return _get_tarval_mode(tv);
383 * Special value query functions ============================================
385 * These functions calculate and return a tarval representing the requested
387 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
388 * functions, but these are stored on initialization of the irmode module and
389 * therefore the irmode functions should be prefered to the functions below.
392 tarval *(get_tarval_bad)(void) {
393 return _get_tarval_bad();
396 tarval *(get_tarval_undefined)(void) {
397 return _get_tarval_undefined();
400 tarval *(get_tarval_b_false)(void) {
401 return _get_tarval_b_false();
404 tarval *(get_tarval_b_true)(void) {
405 return _get_tarval_b_true();
408 tarval *get_tarval_max(ir_mode *mode) {
411 if (get_mode_n_vector_elems(mode) > 1) {
412 /* vector arithmetic not implemented yet */
416 switch(get_mode_sort(mode)) {
418 case irms_control_flow:
424 case irms_internal_boolean:
425 return tarval_b_true;
427 case irms_float_number:
428 switch(get_mode_size_bits(mode)) {
430 fc_get_max(8, 23, NULL);
433 fc_get_max(11, 52, NULL);
436 fc_get_max(15, 64, NULL);
439 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
441 case irms_int_number:
443 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
444 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
449 tarval *get_tarval_min(ir_mode *mode) {
452 if (get_mode_n_vector_elems(mode) > 1) {
453 /* vector arithmetic not implemented yet */
457 switch(get_mode_sort(mode)) {
459 case irms_control_flow:
465 case irms_internal_boolean:
466 return tarval_b_false;
468 case irms_float_number:
469 switch(get_mode_size_bits(mode)) {
471 fc_get_min(8, 23, NULL);
474 fc_get_min(11, 52, NULL);
477 fc_get_min(15, 64, NULL);
480 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
482 case irms_int_number:
484 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
485 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
490 /** The bit pattern for the pointer NULL */
491 static long _null_value;
493 tarval *get_tarval_null(ir_mode *mode) {
496 if (get_mode_n_vector_elems(mode) > 1) {
497 /* vector arithmetic not implemented yet */
501 switch(get_mode_sort(mode)) {
502 case irms_control_flow:
505 case irms_internal_boolean:
509 case irms_float_number:
510 return new_tarval_from_double(0.0, mode);
512 case irms_int_number:
514 return new_tarval_from_long(0l, mode);
517 return new_tarval_from_long(_null_value, mode);
522 tarval *get_tarval_one(ir_mode *mode) {
525 if (get_mode_n_vector_elems(mode) > 1) {
526 /* vector arithmetic not implemented yet */
530 switch(get_mode_sort(mode)) {
531 case irms_control_flow:
534 case irms_internal_boolean:
539 case irms_float_number:
540 return new_tarval_from_double(1.0, mode);
542 case irms_int_number:
544 return new_tarval_from_long(1l, mode);
550 tarval *get_tarval_minus_one(ir_mode *mode) {
553 if (get_mode_n_vector_elems(mode) > 1) {
554 /* vector arithmetic not implemented yet */
558 switch(get_mode_sort(mode)) {
559 case irms_control_flow:
562 case irms_internal_boolean:
567 case irms_float_number:
568 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
570 case irms_int_number:
572 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
577 tarval *get_tarval_nan(ir_mode *mode) {
580 if (get_mode_n_vector_elems(mode) > 1) {
581 /* vector arithmetic not implemented yet */
585 if (get_mode_sort(mode) == irms_float_number) {
586 switch(get_mode_size_bits(mode)) {
588 fc_get_qnan(8, 23, NULL);
591 fc_get_qnan(11, 52, NULL);
594 fc_get_qnan(15, 64, NULL);
597 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
599 assert(0 && "tarval is not floating point");
604 tarval *get_tarval_plus_inf(ir_mode *mode) {
607 if (get_mode_n_vector_elems(mode) > 1) {
608 /* vector arithmetic not implemented yet */
612 if (get_mode_sort(mode) == irms_float_number) {
613 switch(get_mode_size_bits(mode)) {
615 fc_get_plusinf(8, 23, NULL);
618 fc_get_plusinf(11, 52, NULL);
621 fc_get_plusinf(15, 64, NULL);
624 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
626 assert(0 && "tarval is not floating point");
631 tarval *get_tarval_minus_inf(ir_mode *mode) {
634 if (get_mode_n_vector_elems(mode) > 1) {
635 /* vector arithmetic not implemented yet */
639 if (get_mode_sort(mode) == irms_float_number) {
640 switch(get_mode_size_bits(mode)) {
642 fc_get_minusinf(8, 23, NULL);
645 fc_get_minusinf(11, 52, NULL);
648 fc_get_minusinf(15, 64, NULL);
651 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
653 assert(0 && "tarval is not floating point");
659 * Arithmethic operations on tarvals ========================================
663 * test if negative number, 1 means 'yes'
665 int tarval_is_negative(tarval *a) {
668 if (get_mode_n_vector_elems(a->mode) > 1) {
669 /* vector arithmetic not implemented yet */
670 assert(0 && "tarval_is_negative is not allowed for vector modes");
674 switch (get_mode_sort(a->mode)) {
675 case irms_int_number:
676 if (!mode_is_signed(a->mode)) return 0;
678 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
680 case irms_float_number:
681 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
684 assert(0 && "not implemented");
690 * test if null, 1 means 'yes'
692 int tarval_is_null(tarval *a) {
693 ir_mode *m = get_tarval_mode(a);
695 return a == get_tarval_null(m);
699 * test if one, 1 means 'yes'
701 int tarval_is_one(tarval *a) {
702 ir_mode *m = get_tarval_mode(a);
704 return a == get_tarval_one(m);
710 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
714 if (a == tarval_bad || b == tarval_bad) {
715 assert(0 && "Comparison with tarval_bad");
719 if (a == tarval_undefined || b == tarval_undefined)
722 if (a->mode != b->mode)
725 if (get_mode_n_vector_elems(a->mode) > 1) {
726 /* vector arithmetic not implemented yet */
727 assert(0 && "cmp not implemented for vector modes");
730 /* Here the two tarvals are unequal and of the same mode */
731 switch (get_mode_sort(a->mode)) {
732 case irms_control_flow:
740 case irms_float_number:
742 * BEWARE: we cannot compare a == b here, because
743 * a NaN is always Unordered to any other value, even to itself!
745 switch (fc_comp(a->value, b->value)) {
746 case -1: return pn_Cmp_Lt;
747 case 0: return pn_Cmp_Eq;
748 case 1: return pn_Cmp_Gt;
749 case 2: return pn_Cmp_Uo;
750 default: return pn_Cmp_False;
752 case irms_int_number:
756 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
758 case irms_internal_boolean:
761 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
767 * convert to other mode
769 tarval *tarval_convert_to(tarval *src, ir_mode *m) {
775 if (src->mode == m) return src;
777 if (get_mode_n_vector_elems(src->mode) > 1) {
778 /* vector arithmetic not implemented yet */
782 switch (get_mode_sort(src->mode)) {
783 case irms_control_flow:
788 /* cast float to something */
789 case irms_float_number:
790 switch (get_mode_sort(m)) {
791 case irms_float_number:
792 switch (get_mode_size_bits(m)) {
794 fc_cast(src->value, 8, 23, NULL);
797 fc_cast(src->value, 11, 52, NULL);
800 fc_cast(src->value, 15, 64, NULL);
805 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
807 case irms_int_number:
808 switch (GET_FLOAT_TO_INT_MODE()) {
810 fc_int(src->value, NULL);
813 fc_rnd(src->value, NULL);
819 /* FIXME: floating point unit can't produce a value in integer
821 * an intermediate representation is needed here first. */
822 /* return get_tarval(); */
826 /* the rest can't be converted */
831 /* cast int/characters to something */
832 case irms_int_number:
834 switch (get_mode_sort(m)) {
835 case irms_int_number:
837 buffer = alloca(sc_get_buffer_length());
838 memcpy(buffer, src->value, sc_get_buffer_length());
839 sign_extend(buffer, src->mode);
840 return get_tarval_overflow(buffer, src->length, m);
842 case irms_internal_boolean:
843 /* XXX C semantics */
844 if (src == get_mode_null(src->mode)) return tarval_b_false;
845 else return tarval_b_true;
847 case irms_float_number:
848 /* XXX floating point unit does not understand internal integer
849 * representation, convert to string first, then create float from
851 buffer = alloca(100);
852 /* decimal string representation because hexadecimal output is
853 * interpreted unsigned by fc_val_from_str, so this is a HACK */
854 snprintf(buffer, 100, "%s",
855 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
856 buffer[100 - 1] = '\0';
857 switch (get_mode_size_bits(m)) {
859 fc_val_from_str(buffer, 0, 8, 23, NULL);
862 fc_val_from_str(buffer, 0, 11, 52, NULL);
865 fc_val_from_str(buffer, 0, 15, 64, NULL);
868 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
871 /* allow 0 to be casted */
872 if (src == get_mode_null(src->mode))
873 return get_mode_null(m);
881 case irms_internal_boolean:
882 switch (get_mode_sort(m)) {
883 case irms_int_number:
884 if (src == tarval_b_true) return get_mode_one(m);
885 else return get_mode_null(m);
902 tarval *tarval_not(tarval *a) {
907 /* works for vector mode without changes */
909 switch (get_mode_sort(a->mode)) {
910 case irms_int_number:
911 buffer = alloca(sc_get_buffer_length());
912 sc_not(a->value, buffer);
913 return get_tarval(buffer, a->length, a->mode);
915 case irms_internal_boolean:
916 if (a == tarval_b_true)
917 return tarval_b_false;
918 if (a == tarval_b_false)
919 return tarval_b_true;
923 assert(0 && "bitwise negation is only allowed for integer and boolean");
929 * arithmetic negation
931 tarval *tarval_neg(tarval *a) {
935 assert(mode_is_num(a->mode)); /* negation only for numerical values */
937 /* note: negation is allowed even for unsigned modes. */
939 if (get_mode_n_vector_elems(a->mode) > 1) {
940 /* vector arithmetic not implemented yet */
944 switch (get_mode_sort(a->mode)) {
945 case irms_int_number:
946 buffer = alloca(sc_get_buffer_length());
947 sc_neg(a->value, buffer);
948 return get_tarval_overflow(buffer, a->length, a->mode);
950 case irms_float_number:
951 fc_neg(a->value, NULL);
952 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
962 tarval *tarval_add(tarval *a, tarval *b) {
967 assert(a->mode == b->mode);
969 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
970 /* vector arithmetic not implemented yet */
974 switch (get_mode_sort(a->mode)) {
976 case irms_int_number:
977 /* modes of a,b are equal, so result has mode of a as this might be the character */
978 buffer = alloca(sc_get_buffer_length());
979 sc_add(a->value, b->value, buffer);
980 return get_tarval_overflow(buffer, a->length, a->mode);
982 case irms_float_number:
983 fc_add(a->value, b->value, NULL);
984 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
994 tarval *tarval_sub(tarval *a, tarval *b) {
999 assert(a->mode == b->mode);
1001 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1002 /* vector arithmetic not implemented yet */
1005 switch (get_mode_sort(a->mode)) {
1006 case irms_character:
1007 case irms_int_number:
1008 /* modes of a,b are equal, so result has mode of a as this might be the character */
1009 buffer = alloca(sc_get_buffer_length());
1010 sc_sub(a->value, b->value, buffer);
1011 return get_tarval_overflow(buffer, a->length, a->mode);
1013 case irms_float_number:
1014 fc_sub(a->value, b->value, NULL);
1015 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1025 tarval *tarval_mul(tarval *a, tarval *b) {
1030 assert(a->mode == b->mode);
1032 if (get_mode_n_vector_elems(a->mode) > 1) {
1033 /* vector arithmetic not implemented yet */
1037 switch (get_mode_sort(a->mode)) {
1038 case irms_int_number:
1039 /* modes of a,b are equal */
1040 buffer = alloca(sc_get_buffer_length());
1041 sc_mul(a->value, b->value, buffer);
1042 return get_tarval_overflow(buffer, a->length, a->mode);
1044 case irms_float_number:
1045 fc_mul(a->value, b->value, NULL);
1046 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1054 * floating point division
1056 tarval *tarval_quo(tarval *a, tarval *b) {
1059 assert((a->mode == b->mode) && mode_is_float(a->mode));
1061 if (get_mode_n_vector_elems(a->mode) > 1) {
1062 /* vector arithmetic not implemented yet */
1066 fc_div(a->value, b->value, NULL);
1067 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1072 * overflow is impossible, but look out for division by zero
1074 tarval *tarval_div(tarval *a, tarval *b) {
1077 assert((a->mode == b->mode) && mode_is_int(a->mode));
1079 if (get_mode_n_vector_elems(a->mode) > 1) {
1080 /* vector arithmetic not implemented yet */
1085 if (b == get_mode_null(b->mode)) return tarval_bad;
1086 /* modes of a,b are equal */
1087 sc_div(a->value, b->value, NULL);
1088 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1093 * overflow is impossible, but look out for division by zero
1095 tarval *tarval_mod(tarval *a, tarval *b) {
1098 assert((a->mode == b->mode) && mode_is_int(a->mode));
1100 if (get_mode_n_vector_elems(a->mode) > 1) {
1101 /* vector arithmetic not implemented yet */
1106 if (b == get_mode_null(b->mode)) return tarval_bad;
1107 /* modes of a,b are equal */
1108 sc_mod(a->value, b->value, NULL);
1109 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1115 tarval *tarval_abs(tarval *a) {
1119 assert(mode_is_num(a->mode));
1121 if (get_mode_n_vector_elems(a->mode) > 1) {
1122 /* vector arithmetic not implemented yet */
1126 switch (get_mode_sort(a->mode)) {
1127 case irms_int_number:
1128 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1129 buffer = alloca(sc_get_buffer_length());
1130 sc_neg(a->value, buffer);
1131 return get_tarval_overflow(buffer, a->length, a->mode);
1135 case irms_float_number:
1136 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1137 fc_neg(a->value, NULL);
1138 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1151 tarval *tarval_and(tarval *a, tarval *b) {
1154 assert(a->mode == b->mode);
1156 /* works even for vector modes */
1158 switch(get_mode_sort(a->mode)) {
1159 case irms_internal_boolean:
1160 return (a == tarval_b_false) ? a : b;
1162 case irms_int_number:
1163 sc_and(a->value, b->value, NULL);
1164 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1167 assert(0 && "operation not defined on mode");
1175 tarval *tarval_or (tarval *a, tarval *b) {
1178 assert(a->mode == b->mode);
1180 /* works even for vector modes */
1182 switch (get_mode_sort(a->mode)) {
1183 case irms_internal_boolean:
1184 return (a == tarval_b_true) ? a : b;
1186 case irms_int_number:
1187 sc_or(a->value, b->value, NULL);
1188 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1191 assert(0 && "operation not defined on mode");
1197 * bitwise exclusive or (xor)
1199 tarval *tarval_eor(tarval *a, tarval *b) {
1202 assert((a->mode == b->mode));
1204 /* works even for vector modes */
1206 switch (get_mode_sort(a->mode)) {
1207 case irms_internal_boolean:
1208 return (a == b)? tarval_b_false : tarval_b_true;
1210 case irms_int_number:
1211 sc_xor(a->value, b->value, NULL);
1212 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1215 assert(0 && "operation not defined on mode");
1221 * bitwise left shift
1223 tarval *tarval_shl(tarval *a, tarval *b) {
1224 char *temp_val = NULL;
1228 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1230 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1231 /* vector arithmetic not implemented yet */
1235 if (get_mode_modulo_shift(a->mode) != 0) {
1236 temp_val = alloca(sc_get_buffer_length());
1238 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1239 sc_mod(b->value, temp_val, temp_val);
1241 temp_val = (char*)b->value;
1243 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1244 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1248 * bitwise unsigned right shift
1250 tarval *tarval_shr(tarval *a, tarval *b) {
1251 char *temp_val = NULL;
1255 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1257 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1258 /* vector arithmetic not implemented yet */
1262 if (get_mode_modulo_shift(a->mode) != 0) {
1263 temp_val = alloca(sc_get_buffer_length());
1265 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1266 sc_mod(b->value, temp_val, temp_val);
1268 temp_val = (char*)b->value;
1270 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1271 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1275 * bitwise signed right shift
1277 tarval *tarval_shrs(tarval *a, tarval *b) {
1278 char *temp_val = NULL;
1282 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1284 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1285 /* vector arithmetic not implemented yet */
1289 if (get_mode_modulo_shift(a->mode) != 0) {
1290 temp_val = alloca(sc_get_buffer_length());
1292 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1293 sc_mod(b->value, temp_val, temp_val);
1295 temp_val = (char*)b->value;
1297 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1298 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1304 tarval *tarval_rot(tarval *a, tarval *b) {
1305 char *temp_val = NULL;
1309 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1311 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1312 /* vector arithmetic not implemented yet */
1316 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);
1322 temp_val = (char*)b->value;
1324 sc_rot(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 * carry flag of the last operation
1331 int tarval_carry(void) {
1332 return sc_had_carry();
1338 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1339 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1343 const tarval_mode_info *mode_info;
1344 const char *prefix, *suffix;
1346 mode_info = tv->mode->tv_priv;
1348 mode_info = &default_info;
1349 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1350 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1352 switch (get_mode_sort(tv->mode)) {
1353 case irms_reference:
1354 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1356 case irms_int_number:
1357 case irms_character:
1358 switch (mode_info->mode_output) {
1361 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1365 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1371 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1374 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1376 case irms_float_number:
1377 switch (mode_info->mode_output) {
1379 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1382 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1387 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1391 case irms_internal_boolean:
1392 switch (mode_info->mode_output) {
1398 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1402 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1405 case irms_control_flow:
1407 case irms_auxiliary:
1408 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1415 * Output of tarvals to stdio.
1417 int tarval_printf(tarval *tv) {
1421 res = tarval_snprintf(buf, sizeof(buf), tv);
1422 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1427 char *get_tarval_bitpattern(tarval *tv) {
1429 int n = get_mode_size_bits(tv->mode);
1430 int bytes = (n + 7) / 8;
1431 char *res = xmalloc((n + 1) * sizeof(char));
1434 for(i = 0; i < bytes; i++) {
1435 byte = get_tarval_sub_bits(tv, i);
1436 for(j = 1; j < 256; j <<= 1)
1438 res[pos++] = j & byte ? '1' : '0';
1447 * access to the bitpattern
1449 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1450 switch (get_mode_sort(tv->mode)) {
1451 case irms_int_number:
1452 case irms_character:
1453 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1455 case irms_float_number:
1456 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1464 * Specify the output options of one mode.
1466 * This functions stores the modinfo, so DO NOT DESTROY it.
1468 * Returns zero on success.
1470 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1473 mode->tv_priv = modeinfo;
1478 * Returns the output options of one mode.
1480 * This functions returns the mode info of a given mode.
1482 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1485 return mode->tv_priv;
1489 * Identifying tarvals values for algebraic simplifications.
1492 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1493 * - TV_CLASSIFY_ONE for multiplicative neutral,
1494 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1495 * - TV_CLASSIFY_OTHER else
1497 tarval_classification_t classify_tarval(tarval *tv) {
1498 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1500 if (tv == get_mode_null(tv->mode))
1501 return TV_CLASSIFY_NULL;
1502 else if (tv == get_mode_one(tv->mode))
1503 return TV_CLASSIFY_ONE;
1504 else if ((get_mode_sort(tv->mode) == irms_int_number)
1505 && (tv == new_tarval_from_long(-1, tv->mode)))
1506 return TV_CLASSIFY_ALL_ONE;
1508 return TV_CLASSIFY_OTHER;
1512 * Returns non-zero if a given (integer) tarval has only one single bit
1515 int is_single_bit_tarval(tarval *tv) {
1519 if (!tv || tv == tarval_bad) return 0;
1520 if (! mode_is_int(tv->mode)) return 0;
1522 l = get_mode_size_bytes(tv->mode);
1523 for (bits = 0, i = l - 1; i >= 0; --i) {
1524 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1526 /* check for more than one bit in these */
1538 * Sets the overflow mode for integer operations.
1540 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1541 int_overflow_mode = ov_mode;
1545 * Get the overflow mode for integer operations.
1547 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1548 return int_overflow_mode;
1552 * default mode_info for output as HEX
1554 static const tarval_mode_info hex_output = {
1561 * Initialization of the tarval module: called before init_mode()
1563 void init_tarval_1(long null_value) {
1564 _null_value = null_value;
1566 /* initialize the sets holding the tarvals with a comparison function and
1567 * an initial size, which is the expected number of constants */
1568 tarvals = new_set(memcmp, N_CONSTANTS);
1569 values = new_set(memcmp, N_CONSTANTS);
1570 /* init strcalc with precision of 68 to support floating point values with 64
1571 * bit mantissa (needs extra bits for rounding and overflow) */
1577 * Initialization of the tarval module: called after init_mode()
1579 void init_tarval_2(void) {
1580 tarval_bad->kind = k_tarval;
1581 tarval_bad->mode = mode_BAD;
1582 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1584 tarval_undefined->kind = k_tarval;
1585 tarval_undefined->mode = mode_ANY;
1586 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1588 tarval_b_true->kind = k_tarval;
1589 tarval_b_true->mode = mode_b;
1590 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1592 tarval_b_false->kind = k_tarval;
1593 tarval_b_false->mode = mode_b;
1594 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1597 * assign output modes that are compatible with the
1598 * old implementation: Hex output
1600 set_tarval_mode_output_option(mode_U, &hex_output);
1601 set_tarval_mode_output_option(mode_C, &hex_output);
1602 set_tarval_mode_output_option(mode_Bs, &hex_output);
1603 set_tarval_mode_output_option(mode_Bu, &hex_output);
1604 set_tarval_mode_output_option(mode_Hs, &hex_output);
1605 set_tarval_mode_output_option(mode_Hu, &hex_output);
1606 set_tarval_mode_output_option(mode_Is, &hex_output);
1607 set_tarval_mode_output_option(mode_Iu, &hex_output);
1608 set_tarval_mode_output_option(mode_Ls, &hex_output);
1609 set_tarval_mode_output_option(mode_Lu, &hex_output);
1610 set_tarval_mode_output_option(mode_P, &hex_output);
1613 /* free all memory occupied by tarval. */
1614 void finish_tarval(void) {
1617 del_set(tarvals); tarvals = NULL;
1618 del_set(values); values = NULL;
1621 /****************************************************************************
1623 ****************************************************************************/