2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation of and static computations on target machine
25 * @author Mathias Heil
29 * Values are stored in a format depending upon chosen arithmetic
30 * module. Default uses strcalc and fltcalc.
31 * This implementation assumes:
32 * - target has IEEE-754 floating-point arithmetic.
38 #include <assert.h> /* assertions */
39 #include <stdlib.h> /* atoi() */
41 # include <string.h> /* nice things for strings */
44 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
51 #include "set.h" /* to store tarvals in */
52 #include "entity_t.h" /* needed to store pointers to entities */
54 #include "irnode.h" /* defines boolean return values (pnc_number)*/
59 #include "firm_common.h"
62 /** Size of hash tables. Should correspond to average number of distinct constant
64 #define N_CONSTANTS 2048
66 /* get the integer overflow mode */
67 #define GET_OVERFLOW_MODE() int_overflow_mode
69 /* unused, float to int doesn't work yet */
70 enum float_to_int_mode {
75 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
77 #define SWITCH_NOINFINITY 0
78 #define SWITCH_NODENORMALS 0
80 /****************************************************************************
81 * local definitions and macros
82 ****************************************************************************/
84 # define TARVAL_VERIFY(a) tarval_verify((a))
86 # define TARVAL_VERIFY(a) ((void)0)
89 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
90 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
92 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
93 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
95 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
97 /****************************************************************************
99 ****************************************************************************/
100 static struct set *tarvals = NULL; /* container for tarval structs */
101 static struct set *values = NULL; /* container for values */
102 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
104 /** if this is set non-zero, the constant folding for floating point is OFF */
105 static int no_float = 0;
107 /****************************************************************************
109 ****************************************************************************/
111 static int hash_val(const void *value, unsigned int length);
112 static int hash_tv(tarval *tv);
113 static void _fail_verify(tarval *tv, const char* file, int line)
115 /* print a memory image of the tarval and throw an assertion */
117 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
119 printf("%s:%d: Invalid tarval (null)", file, line);
123 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
126 INLINE static void tarval_verify(tarval *tv)
132 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
133 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
135 if (!FIND_TARVAL(tv)) fail_verify(tv);
136 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
142 /** Hash a tarval. */
143 static int hash_tv(tarval *tv) {
144 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
147 /** Hash a value. Treat it as a byte array. */
148 static int hash_val(const void *value, unsigned int length) {
150 unsigned int hash = 0;
152 /* scramble the byte - array */
153 for (i = 0; i < length; ++i) {
154 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
155 hash += (hash << 11) ^ (hash >> 17);
161 /** finds tarval with value/mode or creates new tarval */
162 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)) {
186 case irms_int_number:
187 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
188 switch (GET_OVERFLOW_MODE()) {
189 case TV_OVERFLOW_SATURATE:
190 return get_mode_max(mode);
191 case TV_OVERFLOW_WRAP:
193 char *temp = alloca(sc_get_buffer_length());
194 sc_val_from_ulong(-1, temp);
195 sc_and(temp, value, temp);
196 /* the sc_ module expects that all bits are set ... */
197 sign_extend(temp, mode);
198 return get_tarval(temp, length, mode);
200 case TV_OVERFLOW_BAD:
203 return get_tarval(value, length, mode);
206 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
207 switch (GET_OVERFLOW_MODE()) {
208 case TV_OVERFLOW_SATURATE:
209 return get_mode_min(mode);
210 case TV_OVERFLOW_WRAP: {
211 char *temp = alloca(sc_get_buffer_length());
212 sc_val_from_ulong(-1, temp);
213 sc_and(temp, value, temp);
214 return get_tarval(temp, length, mode);
216 case TV_OVERFLOW_BAD:
219 return get_tarval(value, length, mode);
224 case irms_float_number:
225 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
226 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
229 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
230 return get_mode_null(mode);
237 return get_tarval(value, length, mode);
241 * public variables declared in tv.h
243 static tarval reserved_tv[4];
245 tarval *tarval_bad = &reserved_tv[0];
246 tarval *tarval_undefined = &reserved_tv[1];
247 tarval *tarval_b_false = &reserved_tv[2];
248 tarval *tarval_b_true = &reserved_tv[3];
251 * public functions declared in tv.h
255 * Constructors =============================================================
257 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
263 switch (get_mode_sort(mode)) {
264 case irms_control_flow:
270 case irms_internal_boolean:
271 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
272 if (strcasecmp(str, "true")) return tarval_b_true;
273 else if (strcasecmp(str, "false")) return tarval_b_true;
275 /* XXX This is C semantics */
276 return atoi(str) ? tarval_b_true : tarval_b_false;
278 case irms_float_number:
279 switch (get_mode_size_bits(mode)) {
281 fc_val_from_str(str, len, 8, 23, NULL);
284 fc_val_from_str(str, len, 11, 52, NULL);
287 fc_val_from_str(str, len, 15, 64, NULL);
290 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
293 /* same as integer modes */
294 case irms_int_number:
295 sc_val_from_str(str, len, NULL, mode);
296 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
299 assert(0); /* can't be reached, can it? */
304 * helper function, create a tarval from long
306 tarval *new_tarval_from_long(long l, ir_mode *mode) {
309 switch(get_mode_sort(mode)) {
310 case irms_internal_boolean:
311 /* XXX C semantics ! */
312 return l ? tarval_b_true : tarval_b_false ;
315 /* same as integer modes */
316 case irms_int_number:
317 sc_val_from_long(l, NULL);
318 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
320 case irms_float_number:
321 return new_tarval_from_double((long double)l, mode);
324 assert(0 && "unsupported mode sort");
329 /* returns non-zero if can be converted to long */
330 int tarval_is_long(tarval *tv) {
331 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
334 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
335 /* the value might be too big to fit in a long */
336 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
337 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
338 /* really doesn't fit */
345 /* this might overflow the machine's long, so use only with small values */
346 long get_tarval_long(tarval* tv) {
347 assert(tarval_is_long(tv) && "tarval too big to fit in long");
349 return sc_val_to_long(tv->value);
352 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
353 assert(mode && (get_mode_sort(mode) == irms_float_number));
355 switch (get_mode_size_bits(mode)) {
357 fc_val_from_ieee754(d, 8, 23, NULL);
360 fc_val_from_ieee754(d, 11, 52, NULL);
363 fc_val_from_ieee754(d, 15, 64, NULL);
366 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
369 /* returns non-zero if can be converted to double */
370 int tarval_is_double(tarval *tv) {
373 return (get_mode_sort(tv->mode) == irms_float_number);
376 long double get_tarval_double(tarval *tv) {
377 assert(tarval_is_double(tv));
379 return fc_val_to_ieee754(tv->value);
384 * Access routines for tarval fields ========================================
387 /* get the mode of the tarval */
388 ir_mode *(get_tarval_mode)(const tarval *tv) {
389 return _get_tarval_mode(tv);
393 * Special value query functions ============================================
395 * These functions calculate and return a tarval representing the requested
397 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
398 * functions, but these are stored on initialization of the irmode module and
399 * therefore the irmode functions should be preferred to the functions below.
402 tarval *(get_tarval_bad)(void) {
403 return _get_tarval_bad();
406 tarval *(get_tarval_undefined)(void) {
407 return _get_tarval_undefined();
410 tarval *(get_tarval_b_false)(void) {
411 return _get_tarval_b_false();
414 tarval *(get_tarval_b_true)(void) {
415 return _get_tarval_b_true();
418 tarval *get_tarval_max(ir_mode *mode) {
421 if (get_mode_n_vector_elems(mode) > 1) {
422 /* vector arithmetic not implemented yet */
426 switch(get_mode_sort(mode)) {
428 case irms_control_flow:
434 case irms_internal_boolean:
435 return tarval_b_true;
437 case irms_float_number:
438 switch(get_mode_size_bits(mode)) {
440 fc_get_max(8, 23, NULL);
443 fc_get_max(11, 52, NULL);
446 fc_get_max(15, 64, NULL);
449 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
451 case irms_int_number:
452 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
453 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
458 tarval *get_tarval_min(ir_mode *mode) {
461 if (get_mode_n_vector_elems(mode) > 1) {
462 /* vector arithmetic not implemented yet */
466 switch(get_mode_sort(mode)) {
468 case irms_control_flow:
474 case irms_internal_boolean:
475 return tarval_b_false;
477 case irms_float_number:
478 switch(get_mode_size_bits(mode)) {
480 fc_get_min(8, 23, NULL);
483 fc_get_min(11, 52, NULL);
486 fc_get_min(15, 64, NULL);
489 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
491 case irms_int_number:
492 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
493 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
498 /** The bit pattern for the pointer NULL */
499 static long _null_value;
501 tarval *get_tarval_null(ir_mode *mode) {
504 if (get_mode_n_vector_elems(mode) > 1) {
505 /* vector arithmetic not implemented yet */
509 switch(get_mode_sort(mode)) {
510 case irms_control_flow:
513 case irms_internal_boolean:
517 case irms_float_number:
518 return new_tarval_from_double(0.0, mode);
520 case irms_int_number:
521 return new_tarval_from_long(0l, mode);
524 return new_tarval_from_long(_null_value, mode);
529 tarval *get_tarval_one(ir_mode *mode) {
532 if (get_mode_n_vector_elems(mode) > 1) {
533 /* vector arithmetic not implemented yet */
537 switch(get_mode_sort(mode)) {
538 case irms_control_flow:
541 case irms_internal_boolean:
546 case irms_float_number:
547 return new_tarval_from_double(1.0, mode);
549 case irms_int_number:
550 return new_tarval_from_long(1l, mode);
556 tarval *get_tarval_minus_one(ir_mode *mode) {
559 if (get_mode_n_vector_elems(mode) > 1) {
560 /* vector arithmetic not implemented yet */
564 switch(get_mode_sort(mode)) {
565 case irms_control_flow:
568 case irms_internal_boolean:
573 case irms_float_number:
574 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
576 case irms_int_number:
577 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
582 tarval *get_tarval_nan(ir_mode *mode) {
585 if (get_mode_n_vector_elems(mode) > 1) {
586 /* vector arithmetic not implemented yet */
590 if (get_mode_sort(mode) == irms_float_number) {
591 switch(get_mode_size_bits(mode)) {
593 fc_get_qnan(8, 23, NULL);
596 fc_get_qnan(11, 52, NULL);
599 fc_get_qnan(15, 64, NULL);
602 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
604 assert(0 && "tarval is not floating point");
609 tarval *get_tarval_plus_inf(ir_mode *mode) {
612 if (get_mode_n_vector_elems(mode) > 1) {
613 /* vector arithmetic not implemented yet */
617 if (get_mode_sort(mode) == irms_float_number) {
618 switch(get_mode_size_bits(mode)) {
620 fc_get_plusinf(8, 23, NULL);
623 fc_get_plusinf(11, 52, NULL);
626 fc_get_plusinf(15, 64, NULL);
629 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
631 assert(0 && "tarval is not floating point");
636 tarval *get_tarval_minus_inf(ir_mode *mode) {
639 if (get_mode_n_vector_elems(mode) > 1) {
640 /* vector arithmetic not implemented yet */
644 if (get_mode_sort(mode) == irms_float_number) {
645 switch(get_mode_size_bits(mode)) {
647 fc_get_minusinf(8, 23, NULL);
650 fc_get_minusinf(11, 52, NULL);
653 fc_get_minusinf(15, 64, NULL);
656 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
658 assert(0 && "tarval is not floating point");
664 * Arithmethic operations on tarvals ========================================
668 * test if negative number, 1 means 'yes'
670 int tarval_is_negative(tarval *a) {
673 if (get_mode_n_vector_elems(a->mode) > 1) {
674 /* vector arithmetic not implemented yet */
675 assert(0 && "tarval_is_negative is not allowed for vector modes");
679 switch (get_mode_sort(a->mode)) {
680 case irms_int_number:
681 if (!mode_is_signed(a->mode)) return 0;
683 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
685 case irms_float_number:
686 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
689 assert(0 && "not implemented");
695 * test if null, 1 means 'yes'
697 int tarval_is_null(tarval *a) {
698 ir_mode *m = get_tarval_mode(a);
700 return a == get_tarval_null(m);
704 * test if one, 1 means 'yes'
706 int tarval_is_one(tarval *a) {
707 ir_mode *m = get_tarval_mode(a);
709 return a == get_tarval_one(m);
715 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
719 if (a == tarval_bad || b == tarval_bad) {
720 assert(0 && "Comparison with tarval_bad");
724 if (a == tarval_undefined || b == tarval_undefined)
727 if (a->mode != b->mode)
730 if (get_mode_n_vector_elems(a->mode) > 1) {
731 /* vector arithmetic not implemented yet */
732 assert(0 && "cmp not implemented for vector modes");
735 /* Here the two tarvals are unequal and of the same mode */
736 switch (get_mode_sort(a->mode)) {
737 case irms_control_flow:
745 case irms_float_number:
749 * BEWARE: we cannot compare a == b here, because
750 * a NaN is always Unordered to any other value, even to itself!
752 switch (fc_comp(a->value, b->value)) {
753 case -1: return pn_Cmp_Lt;
754 case 0: return pn_Cmp_Eq;
755 case 1: return pn_Cmp_Gt;
756 case 2: return pn_Cmp_Uo;
757 default: return pn_Cmp_False;
759 case irms_int_number:
762 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
764 case irms_internal_boolean:
767 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
773 * convert to other mode
775 tarval *tarval_convert_to(tarval *src, ir_mode *m) {
781 if (src->mode == m) return src;
783 if (get_mode_n_vector_elems(src->mode) > 1) {
784 /* vector arithmetic not implemented yet */
788 switch (get_mode_sort(src->mode)) {
789 case irms_control_flow:
794 /* cast float to something */
795 case irms_float_number:
796 switch (get_mode_sort(m)) {
797 case irms_float_number:
798 switch (get_mode_size_bits(m)) {
800 fc_cast(src->value, 8, 23, NULL);
803 fc_cast(src->value, 11, 52, NULL);
806 fc_cast(src->value, 15, 64, NULL);
811 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
813 case irms_int_number:
814 switch (GET_FLOAT_TO_INT_MODE()) {
816 fc_int(src->value, NULL);
819 fc_rnd(src->value, NULL);
825 /* FIXME: floating point unit can't produce a value in integer
827 * an intermediate representation is needed here first. */
828 /* return get_tarval(); */
832 /* the rest can't be converted */
837 /* cast int/characters to something */
838 case irms_int_number:
839 switch (get_mode_sort(m)) {
840 case irms_int_number:
841 buffer = alloca(sc_get_buffer_length());
842 memcpy(buffer, src->value, sc_get_buffer_length());
843 sign_extend(buffer, m);
844 return get_tarval_overflow(buffer, src->length, m);
846 case irms_internal_boolean:
847 /* XXX C semantics */
848 if (src == get_mode_null(src->mode)) return tarval_b_false;
849 else return tarval_b_true;
851 case irms_float_number:
852 /* XXX floating point unit does not understand internal integer
853 * representation, convert to string first, then create float from
855 buffer = alloca(100);
856 /* decimal string representation because hexadecimal output is
857 * interpreted unsigned by fc_val_from_str, so this is a HACK */
858 snprintf(buffer, 100, "%s",
859 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
860 buffer[100 - 1] = '\0';
861 switch (get_mode_size_bits(m)) {
863 fc_val_from_str(buffer, 0, 8, 23, NULL);
866 fc_val_from_str(buffer, 0, 11, 52, NULL);
869 fc_val_from_str(buffer, 0, 15, 64, NULL);
872 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
875 /* allow 0 to be casted */
876 if (src == get_mode_null(src->mode))
877 return get_mode_null(m);
885 case irms_internal_boolean:
886 switch (get_mode_sort(m)) {
887 case irms_int_number:
888 if (src == tarval_b_true) return get_mode_one(m);
889 else return get_mode_null(m);
897 switch(get_mode_sort(m)) {
898 case irms_int_number:
899 buffer = alloca(sc_get_buffer_length());
900 memcpy(buffer, src->value, sc_get_buffer_length());
901 sign_extend(buffer, src->mode);
902 return get_tarval_overflow(buffer, src->length, m);
916 tarval *tarval_not(tarval *a) {
921 /* works for vector mode without changes */
923 switch (get_mode_sort(a->mode)) {
924 case irms_int_number:
925 buffer = alloca(sc_get_buffer_length());
926 sc_not(a->value, buffer);
927 return get_tarval(buffer, a->length, a->mode);
929 case irms_internal_boolean:
930 if (a == tarval_b_true)
931 return tarval_b_false;
932 if (a == tarval_b_false)
933 return tarval_b_true;
937 assert(0 && "bitwise negation is only allowed for integer and boolean");
943 * arithmetic negation
945 tarval *tarval_neg(tarval *a) {
949 assert(mode_is_num(a->mode)); /* negation only for numerical values */
951 /* note: negation is allowed even for unsigned modes. */
953 if (get_mode_n_vector_elems(a->mode) > 1) {
954 /* vector arithmetic not implemented yet */
958 switch (get_mode_sort(a->mode)) {
959 case irms_int_number:
960 buffer = alloca(sc_get_buffer_length());
961 sc_neg(a->value, buffer);
962 return get_tarval_overflow(buffer, a->length, a->mode);
964 case irms_float_number:
968 fc_neg(a->value, NULL);
969 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
979 tarval *tarval_add(tarval *a, tarval *b) {
984 assert(a->mode == b->mode);
986 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
987 /* vector arithmetic not implemented yet */
991 switch (get_mode_sort(a->mode)) {
992 case irms_int_number:
993 /* modes of a,b are equal, so result has mode of a as this might be the character */
994 buffer = alloca(sc_get_buffer_length());
995 sc_add(a->value, b->value, buffer);
996 return get_tarval_overflow(buffer, a->length, a->mode);
998 case irms_float_number:
1002 fc_add(a->value, b->value, NULL);
1003 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1013 tarval *tarval_sub(tarval *a, tarval *b) {
1018 assert(a->mode == b->mode);
1020 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1021 /* vector arithmetic not implemented yet */
1024 switch (get_mode_sort(a->mode)) {
1025 case irms_int_number:
1026 /* modes of a,b are equal, so result has mode of a as this might be the character */
1027 buffer = alloca(sc_get_buffer_length());
1028 sc_sub(a->value, b->value, buffer);
1029 return get_tarval_overflow(buffer, a->length, a->mode);
1031 case irms_float_number:
1035 fc_sub(a->value, b->value, NULL);
1036 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1046 tarval *tarval_mul(tarval *a, tarval *b) {
1051 assert(a->mode == b->mode);
1053 if (get_mode_n_vector_elems(a->mode) > 1) {
1054 /* vector arithmetic not implemented yet */
1058 switch (get_mode_sort(a->mode)) {
1059 case irms_int_number:
1060 /* modes of a,b are equal */
1061 buffer = alloca(sc_get_buffer_length());
1062 sc_mul(a->value, b->value, buffer);
1063 return get_tarval_overflow(buffer, a->length, a->mode);
1065 case irms_float_number:
1069 fc_mul(a->value, b->value, NULL);
1070 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1078 * floating point division
1080 tarval *tarval_quo(tarval *a, tarval *b) {
1083 assert((a->mode == b->mode) && mode_is_float(a->mode));
1088 if (get_mode_n_vector_elems(a->mode) > 1) {
1089 /* vector arithmetic not implemented yet */
1093 fc_div(a->value, b->value, NULL);
1094 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1099 * overflow is impossible, but look out for division by zero
1101 tarval *tarval_div(tarval *a, tarval *b) {
1104 assert((a->mode == b->mode) && mode_is_int(a->mode));
1106 if (get_mode_n_vector_elems(a->mode) > 1) {
1107 /* vector arithmetic not implemented yet */
1112 if (b == get_mode_null(b->mode)) return tarval_bad;
1113 /* modes of a,b are equal */
1114 sc_div(a->value, b->value, NULL);
1115 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1120 * overflow is impossible, but look out for division by zero
1122 tarval *tarval_mod(tarval *a, tarval *b) {
1125 assert((a->mode == b->mode) && mode_is_int(a->mode));
1127 if (get_mode_n_vector_elems(a->mode) > 1) {
1128 /* vector arithmetic not implemented yet */
1133 if (b == get_mode_null(b->mode)) return tarval_bad;
1134 /* modes of a,b are equal */
1135 sc_mod(a->value, b->value, NULL);
1136 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1142 tarval *tarval_abs(tarval *a) {
1146 assert(mode_is_num(a->mode));
1148 if (get_mode_n_vector_elems(a->mode) > 1) {
1149 /* vector arithmetic not implemented yet */
1153 switch (get_mode_sort(a->mode)) {
1154 case irms_int_number:
1155 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1156 buffer = alloca(sc_get_buffer_length());
1157 sc_neg(a->value, buffer);
1158 return get_tarval_overflow(buffer, a->length, a->mode);
1162 case irms_float_number:
1166 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1167 fc_neg(a->value, NULL);
1168 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1181 tarval *tarval_and(tarval *a, tarval *b) {
1184 assert(a->mode == b->mode);
1186 /* works even for vector modes */
1188 switch(get_mode_sort(a->mode)) {
1189 case irms_internal_boolean:
1190 return (a == tarval_b_false) ? a : b;
1192 case irms_int_number:
1193 sc_and(a->value, b->value, NULL);
1194 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1197 assert(0 && "operation not defined on mode");
1205 tarval *tarval_or (tarval *a, tarval *b) {
1208 assert(a->mode == b->mode);
1210 /* works even for vector modes */
1212 switch (get_mode_sort(a->mode)) {
1213 case irms_internal_boolean:
1214 return (a == tarval_b_true) ? a : b;
1216 case irms_int_number:
1217 sc_or(a->value, b->value, NULL);
1218 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1221 assert(0 && "operation not defined on mode");
1227 * bitwise exclusive or (xor)
1229 tarval *tarval_eor(tarval *a, tarval *b) {
1232 assert((a->mode == b->mode));
1234 /* works even for vector modes */
1236 switch (get_mode_sort(a->mode)) {
1237 case irms_internal_boolean:
1238 return (a == b)? tarval_b_false : tarval_b_true;
1240 case irms_int_number:
1241 sc_xor(a->value, b->value, NULL);
1242 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1245 assert(0 && "operation not defined on mode");
1251 * bitwise left shift
1253 tarval *tarval_shl(tarval *a, tarval *b) {
1254 char *temp_val = NULL;
1258 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1260 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1261 /* vector arithmetic not implemented yet */
1265 if (get_mode_modulo_shift(a->mode) != 0) {
1266 temp_val = alloca(sc_get_buffer_length());
1268 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1269 sc_mod(b->value, temp_val, temp_val);
1271 temp_val = (char*)b->value;
1273 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1274 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1278 * bitwise unsigned right shift
1280 tarval *tarval_shr(tarval *a, tarval *b) {
1281 char *temp_val = NULL;
1285 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1287 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1288 /* vector arithmetic not implemented yet */
1292 if (get_mode_modulo_shift(a->mode) != 0) {
1293 temp_val = alloca(sc_get_buffer_length());
1295 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1296 sc_mod(b->value, temp_val, temp_val);
1298 temp_val = (char*)b->value;
1300 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1301 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1305 * bitwise signed right shift
1307 tarval *tarval_shrs(tarval *a, tarval *b) {
1308 char *temp_val = NULL;
1312 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1314 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1315 /* vector arithmetic not implemented yet */
1319 if (get_mode_modulo_shift(a->mode) != 0) {
1320 temp_val = alloca(sc_get_buffer_length());
1322 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1323 sc_mod(b->value, temp_val, temp_val);
1325 temp_val = (char*)b->value;
1327 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1328 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1334 tarval *tarval_rot(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) {
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);
1352 temp_val = (char*)b->value;
1354 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1355 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1359 * carry flag of the last operation
1361 int tarval_carry(void) {
1362 return sc_had_carry();
1368 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1369 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1373 const tarval_mode_info *mode_info;
1374 const char *prefix, *suffix;
1376 mode_info = tv->mode->tv_priv;
1378 mode_info = &default_info;
1379 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1380 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1382 switch (get_mode_sort(tv->mode)) {
1383 case irms_reference:
1384 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1386 case irms_int_number:
1387 switch (mode_info->mode_output) {
1390 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1394 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1400 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1403 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1405 case irms_float_number:
1406 switch (mode_info->mode_output) {
1408 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1411 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1416 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1420 case irms_internal_boolean:
1421 switch (mode_info->mode_output) {
1427 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1431 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1434 case irms_control_flow:
1436 case irms_auxiliary:
1437 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1444 * Output of tarvals to stdio.
1446 int tarval_printf(tarval *tv) {
1450 res = tarval_snprintf(buf, sizeof(buf), tv);
1451 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1456 char *get_tarval_bitpattern(tarval *tv) {
1458 int n = get_mode_size_bits(tv->mode);
1459 int bytes = (n + 7) / 8;
1460 char *res = xmalloc((n + 1) * sizeof(char));
1463 for(i = 0; i < bytes; i++) {
1464 byte = get_tarval_sub_bits(tv, i);
1465 for(j = 1; j < 256; j <<= 1)
1467 res[pos++] = j & byte ? '1' : '0';
1476 * access to the bitpattern
1478 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1479 switch (get_mode_arithmetic(tv->mode)) {
1480 case irma_twos_complement:
1481 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1483 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1485 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1490 * Specify the output options of one mode.
1492 * This functions stores the modinfo, so DO NOT DESTROY it.
1494 * Returns zero on success.
1496 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1499 mode->tv_priv = modeinfo;
1504 * Returns the output options of one mode.
1506 * This functions returns the mode info of a given mode.
1508 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1511 return mode->tv_priv;
1515 * Identifying tarvals values for algebraic simplifications.
1518 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1519 * - TV_CLASSIFY_ONE for multiplicative neutral,
1520 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1521 * - TV_CLASSIFY_OTHER else
1523 tarval_classification_t classify_tarval(tarval *tv) {
1524 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1526 if (tv == get_mode_null(tv->mode))
1527 return TV_CLASSIFY_NULL;
1528 else if (tv == get_mode_one(tv->mode))
1529 return TV_CLASSIFY_ONE;
1530 else if ((get_mode_sort(tv->mode) == irms_int_number)
1531 && (tv == new_tarval_from_long(-1, tv->mode)))
1532 return TV_CLASSIFY_ALL_ONE;
1534 return TV_CLASSIFY_OTHER;
1538 * Returns non-zero if a given (integer) tarval has only one single bit
1541 int tarval_is_single_bit(tarval *tv) {
1545 if (!tv || tv == tarval_bad) return 0;
1546 if (! mode_is_int(tv->mode)) return 0;
1548 l = get_mode_size_bytes(tv->mode);
1549 for (bits = 0, i = l - 1; i >= 0; --i) {
1550 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1552 /* check for more than one bit in these */
1564 * Returns non-zero if the mantissa of a floating point IEEE-754
1565 * tarval is zero (i.e. 1.0Exxx)
1567 int tarval_ieee754_zero_mantissa(tarval *tv) {
1568 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1569 return fc_zero_mantissa(tv->value);
1572 /* Returns the exponent of a floating point IEEE-754 tarval. */
1573 int tarval_ieee754_get_exponent(tarval *tv) {
1574 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1575 return fc_get_exponent(tv->value);
1578 /* Set the immediate precision for IEEE-754 results. */
1579 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1580 return fc_set_immediate_precision(bits);
1583 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1584 unsigned tarval_ieee754_get_exact(void) {
1585 return fc_is_exact();
1589 * Sets the overflow mode for integer operations.
1591 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1592 int_overflow_mode = ov_mode;
1595 /* Get the overflow mode for integer operations. */
1596 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1597 return int_overflow_mode;
1600 /* Enable/Disable floating point constant folding. */
1601 int tarval_enable_fp_ops(int enable) {
1602 int old = !no_float;
1609 * default mode_info for output as HEX
1611 static const tarval_mode_info hex_output = {
1618 * Initialization of the tarval module: called before init_mode()
1620 void init_tarval_1(long null_value) {
1621 _null_value = null_value;
1623 /* initialize the sets holding the tarvals with a comparison function and
1624 * an initial size, which is the expected number of constants */
1625 tarvals = new_set(memcmp, N_CONSTANTS);
1626 values = new_set(memcmp, N_CONSTANTS);
1627 /* init strcalc with precision of 68 to support floating point values with 64
1628 * bit mantissa (needs extra bits for rounding and overflow) */
1634 * Initialization of the tarval module: called after init_mode()
1636 void init_tarval_2(void) {
1637 tarval_bad->kind = k_tarval;
1638 tarval_bad->mode = mode_BAD;
1639 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1641 tarval_undefined->kind = k_tarval;
1642 tarval_undefined->mode = mode_ANY;
1643 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1645 tarval_b_true->kind = k_tarval;
1646 tarval_b_true->mode = mode_b;
1647 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1649 tarval_b_false->kind = k_tarval;
1650 tarval_b_false->mode = mode_b;
1651 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1654 * assign output modes that are compatible with the
1655 * old implementation: Hex output
1657 set_tarval_mode_output_option(mode_Bs, &hex_output);
1658 set_tarval_mode_output_option(mode_Bu, &hex_output);
1659 set_tarval_mode_output_option(mode_Hs, &hex_output);
1660 set_tarval_mode_output_option(mode_Hu, &hex_output);
1661 set_tarval_mode_output_option(mode_Is, &hex_output);
1662 set_tarval_mode_output_option(mode_Iu, &hex_output);
1663 set_tarval_mode_output_option(mode_Ls, &hex_output);
1664 set_tarval_mode_output_option(mode_Lu, &hex_output);
1665 set_tarval_mode_output_option(mode_P, &hex_output);
1668 /* free all memory occupied by tarval. */
1669 void finish_tarval(void) {
1672 del_set(tarvals); tarvals = NULL;
1673 del_set(values); values = NULL;
1676 /****************************************************************************
1678 ****************************************************************************/