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 sign_extend(sc_get_buffer(), mode);
319 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
321 case irms_float_number:
322 return new_tarval_from_double((long double)l, mode);
325 assert(0 && "unsupported mode sort");
330 /* returns non-zero if can be converted to long */
331 int tarval_is_long(tarval *tv) {
332 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
335 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
336 /* the value might be too big to fit in a long */
337 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
338 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
339 /* really doesn't fit */
346 /* this might overflow the machine's long, so use only with small values */
347 long get_tarval_long(tarval* tv) {
348 assert(tarval_is_long(tv) && "tarval too big to fit in long");
350 return sc_val_to_long(tv->value);
353 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
354 assert(mode && (get_mode_sort(mode) == irms_float_number));
356 switch (get_mode_size_bits(mode)) {
358 fc_val_from_ieee754(d, 8, 23, NULL);
361 fc_val_from_ieee754(d, 11, 52, NULL);
364 fc_val_from_ieee754(d, 15, 64, NULL);
367 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
370 /* returns non-zero if can be converted to double */
371 int tarval_is_double(tarval *tv) {
374 return (get_mode_sort(tv->mode) == irms_float_number);
377 long double get_tarval_double(tarval *tv) {
378 assert(tarval_is_double(tv));
380 return fc_val_to_ieee754(tv->value);
385 * Access routines for tarval fields ========================================
388 /* get the mode of the tarval */
389 ir_mode *(get_tarval_mode)(const tarval *tv) {
390 return _get_tarval_mode(tv);
394 * Special value query functions ============================================
396 * These functions calculate and return a tarval representing the requested
398 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
399 * functions, but these are stored on initialization of the irmode module and
400 * therefore the irmode functions should be preferred to the functions below.
403 tarval *(get_tarval_bad)(void) {
404 return _get_tarval_bad();
407 tarval *(get_tarval_undefined)(void) {
408 return _get_tarval_undefined();
411 tarval *(get_tarval_b_false)(void) {
412 return _get_tarval_b_false();
415 tarval *(get_tarval_b_true)(void) {
416 return _get_tarval_b_true();
419 tarval *get_tarval_max(ir_mode *mode) {
422 if (get_mode_n_vector_elems(mode) > 1) {
423 /* vector arithmetic not implemented yet */
427 switch(get_mode_sort(mode)) {
429 case irms_control_flow:
435 case irms_internal_boolean:
436 return tarval_b_true;
438 case irms_float_number:
439 switch(get_mode_size_bits(mode)) {
441 fc_get_max(8, 23, NULL);
444 fc_get_max(11, 52, NULL);
447 fc_get_max(15, 64, NULL);
450 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
452 case irms_int_number:
453 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
454 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
459 tarval *get_tarval_min(ir_mode *mode) {
462 if (get_mode_n_vector_elems(mode) > 1) {
463 /* vector arithmetic not implemented yet */
467 switch(get_mode_sort(mode)) {
469 case irms_control_flow:
475 case irms_internal_boolean:
476 return tarval_b_false;
478 case irms_float_number:
479 switch(get_mode_size_bits(mode)) {
481 fc_get_min(8, 23, NULL);
484 fc_get_min(11, 52, NULL);
487 fc_get_min(15, 64, NULL);
490 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
492 case irms_int_number:
493 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
494 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
499 /** The bit pattern for the pointer NULL */
500 static long _null_value;
502 tarval *get_tarval_null(ir_mode *mode) {
505 if (get_mode_n_vector_elems(mode) > 1) {
506 /* vector arithmetic not implemented yet */
510 switch(get_mode_sort(mode)) {
511 case irms_control_flow:
514 case irms_internal_boolean:
518 case irms_float_number:
519 return new_tarval_from_double(0.0, mode);
521 case irms_int_number:
522 return new_tarval_from_long(0l, mode);
525 return new_tarval_from_long(_null_value, mode);
530 tarval *get_tarval_one(ir_mode *mode) {
533 if (get_mode_n_vector_elems(mode) > 1) {
534 /* vector arithmetic not implemented yet */
538 switch(get_mode_sort(mode)) {
539 case irms_control_flow:
542 case irms_internal_boolean:
547 case irms_float_number:
548 return new_tarval_from_double(1.0, mode);
550 case irms_int_number:
551 return new_tarval_from_long(1l, mode);
557 tarval *get_tarval_minus_one(ir_mode *mode) {
560 if (get_mode_n_vector_elems(mode) > 1) {
561 /* vector arithmetic not implemented yet */
565 switch(get_mode_sort(mode)) {
566 case irms_control_flow:
569 case irms_internal_boolean:
574 case irms_float_number:
575 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
577 case irms_int_number:
578 return new_tarval_from_long(-1l, mode);
583 tarval *get_tarval_nan(ir_mode *mode) {
586 if (get_mode_n_vector_elems(mode) > 1) {
587 /* vector arithmetic not implemented yet */
591 if (get_mode_sort(mode) == irms_float_number) {
592 switch(get_mode_size_bits(mode)) {
594 fc_get_qnan(8, 23, NULL);
597 fc_get_qnan(11, 52, NULL);
600 fc_get_qnan(15, 64, NULL);
603 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
605 assert(0 && "tarval is not floating point");
610 tarval *get_tarval_plus_inf(ir_mode *mode) {
613 if (get_mode_n_vector_elems(mode) > 1) {
614 /* vector arithmetic not implemented yet */
618 if (get_mode_sort(mode) == irms_float_number) {
619 switch(get_mode_size_bits(mode)) {
621 fc_get_plusinf(8, 23, NULL);
624 fc_get_plusinf(11, 52, NULL);
627 fc_get_plusinf(15, 64, NULL);
630 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
632 assert(0 && "tarval is not floating point");
637 tarval *get_tarval_minus_inf(ir_mode *mode) {
640 if (get_mode_n_vector_elems(mode) > 1) {
641 /* vector arithmetic not implemented yet */
645 if (get_mode_sort(mode) == irms_float_number) {
646 switch(get_mode_size_bits(mode)) {
648 fc_get_minusinf(8, 23, NULL);
651 fc_get_minusinf(11, 52, NULL);
654 fc_get_minusinf(15, 64, NULL);
657 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
659 assert(0 && "tarval is not floating point");
665 * Arithmethic operations on tarvals ========================================
669 * test if negative number, 1 means 'yes'
671 int tarval_is_negative(tarval *a) {
674 if (get_mode_n_vector_elems(a->mode) > 1) {
675 /* vector arithmetic not implemented yet */
676 assert(0 && "tarval_is_negative is not allowed for vector modes");
680 switch (get_mode_sort(a->mode)) {
681 case irms_int_number:
682 if (!mode_is_signed(a->mode)) return 0;
684 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
686 case irms_float_number:
687 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
690 assert(0 && "not implemented");
696 * test if null, 1 means 'yes'
698 int tarval_is_null(tarval *a) {
699 ir_mode *m = get_tarval_mode(a);
701 return a == get_tarval_null(m);
705 * test if one, 1 means 'yes'
707 int tarval_is_one(tarval *a) {
708 ir_mode *m = get_tarval_mode(a);
710 return a == get_tarval_one(m);
716 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
720 if (a == tarval_bad || b == tarval_bad) {
721 assert(0 && "Comparison with tarval_bad");
725 if (a == tarval_undefined || b == tarval_undefined)
728 if (a->mode != b->mode)
731 if (get_mode_n_vector_elems(a->mode) > 1) {
732 /* vector arithmetic not implemented yet */
733 assert(0 && "cmp not implemented for vector modes");
736 /* Here the two tarvals are unequal and of the same mode */
737 switch (get_mode_sort(a->mode)) {
738 case irms_control_flow:
746 case irms_float_number:
750 * BEWARE: we cannot compare a == b here, because
751 * a NaN is always Unordered to any other value, even to itself!
753 switch (fc_comp(a->value, b->value)) {
754 case -1: return pn_Cmp_Lt;
755 case 0: return pn_Cmp_Eq;
756 case 1: return pn_Cmp_Gt;
757 case 2: return pn_Cmp_Uo;
758 default: return pn_Cmp_False;
760 case irms_int_number:
763 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
765 case irms_internal_boolean:
768 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
774 * convert to other mode
776 tarval *tarval_convert_to(tarval *src, ir_mode *m) {
782 if (src->mode == m) return src;
784 if (get_mode_n_vector_elems(src->mode) > 1) {
785 /* vector arithmetic not implemented yet */
789 switch (get_mode_sort(src->mode)) {
790 case irms_control_flow:
795 /* cast float to something */
796 case irms_float_number:
797 switch (get_mode_sort(m)) {
798 case irms_float_number:
799 switch (get_mode_size_bits(m)) {
801 fc_cast(src->value, 8, 23, NULL);
804 fc_cast(src->value, 11, 52, NULL);
807 fc_cast(src->value, 15, 64, NULL);
812 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
814 case irms_int_number:
815 switch (GET_FLOAT_TO_INT_MODE()) {
817 fc_int(src->value, NULL);
820 fc_rnd(src->value, NULL);
826 /* FIXME: floating point unit can't produce a value in integer
828 * an intermediate representation is needed here first. */
829 /* return get_tarval(); */
833 /* the rest can't be converted */
838 /* cast int/characters to something */
839 case irms_int_number:
840 switch (get_mode_sort(m)) {
841 case irms_int_number:
842 buffer = alloca(sc_get_buffer_length());
843 memcpy(buffer, src->value, sc_get_buffer_length());
844 sign_extend(buffer, m);
845 return get_tarval_overflow(buffer, src->length, m);
847 case irms_internal_boolean:
848 /* XXX C semantics */
849 if (src == get_mode_null(src->mode)) return tarval_b_false;
850 else return tarval_b_true;
852 case irms_float_number:
853 /* XXX floating point unit does not understand internal integer
854 * representation, convert to string first, then create float from
856 buffer = alloca(100);
857 /* decimal string representation because hexadecimal output is
858 * interpreted unsigned by fc_val_from_str, so this is a HACK */
859 snprintf(buffer, 100, "%s",
860 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
861 buffer[100 - 1] = '\0';
862 switch (get_mode_size_bits(m)) {
864 fc_val_from_str(buffer, 0, 8, 23, NULL);
867 fc_val_from_str(buffer, 0, 11, 52, NULL);
870 fc_val_from_str(buffer, 0, 15, 64, NULL);
873 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
876 /* allow 0 to be casted */
877 if (src == get_mode_null(src->mode))
878 return get_mode_null(m);
886 case irms_internal_boolean:
887 switch (get_mode_sort(m)) {
888 case irms_int_number:
889 if (src == tarval_b_true) return get_mode_one(m);
890 else return get_mode_null(m);
898 switch(get_mode_sort(m)) {
899 case irms_int_number:
900 buffer = alloca(sc_get_buffer_length());
901 memcpy(buffer, src->value, sc_get_buffer_length());
902 sign_extend(buffer, src->mode);
903 return get_tarval_overflow(buffer, src->length, m);
917 tarval *tarval_not(tarval *a) {
922 /* works for vector mode without changes */
924 switch (get_mode_sort(a->mode)) {
925 case irms_int_number:
926 buffer = alloca(sc_get_buffer_length());
927 sc_not(a->value, buffer);
928 return get_tarval(buffer, a->length, a->mode);
930 case irms_internal_boolean:
931 if (a == tarval_b_true)
932 return tarval_b_false;
933 if (a == tarval_b_false)
934 return tarval_b_true;
938 assert(0 && "bitwise negation is only allowed for integer and boolean");
944 * arithmetic negation
946 tarval *tarval_neg(tarval *a) {
950 assert(mode_is_num(a->mode)); /* negation only for numerical values */
952 /* note: negation is allowed even for unsigned modes. */
954 if (get_mode_n_vector_elems(a->mode) > 1) {
955 /* vector arithmetic not implemented yet */
959 switch (get_mode_sort(a->mode)) {
960 case irms_int_number:
961 buffer = alloca(sc_get_buffer_length());
962 sc_neg(a->value, buffer);
963 return get_tarval_overflow(buffer, a->length, a->mode);
965 case irms_float_number:
969 fc_neg(a->value, NULL);
970 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
980 tarval *tarval_add(tarval *a, tarval *b) {
985 assert(a->mode == b->mode);
987 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
988 /* vector arithmetic not implemented yet */
992 switch (get_mode_sort(a->mode)) {
993 case irms_int_number:
994 /* modes of a,b are equal, so result has mode of a as this might be the character */
995 buffer = alloca(sc_get_buffer_length());
996 sc_add(a->value, b->value, buffer);
997 return get_tarval_overflow(buffer, a->length, a->mode);
999 case irms_float_number:
1003 fc_add(a->value, b->value, NULL);
1004 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1014 tarval *tarval_sub(tarval *a, tarval *b) {
1019 assert(a->mode == b->mode);
1021 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1022 /* vector arithmetic not implemented yet */
1025 switch (get_mode_sort(a->mode)) {
1026 case irms_int_number:
1027 /* modes of a,b are equal, so result has mode of a as this might be the character */
1028 buffer = alloca(sc_get_buffer_length());
1029 sc_sub(a->value, b->value, buffer);
1030 return get_tarval_overflow(buffer, a->length, a->mode);
1032 case irms_float_number:
1036 fc_sub(a->value, b->value, NULL);
1037 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1047 tarval *tarval_mul(tarval *a, tarval *b) {
1052 assert(a->mode == b->mode);
1054 if (get_mode_n_vector_elems(a->mode) > 1) {
1055 /* vector arithmetic not implemented yet */
1059 switch (get_mode_sort(a->mode)) {
1060 case irms_int_number:
1061 /* modes of a,b are equal */
1062 buffer = alloca(sc_get_buffer_length());
1063 sc_mul(a->value, b->value, buffer);
1064 return get_tarval_overflow(buffer, a->length, a->mode);
1066 case irms_float_number:
1070 fc_mul(a->value, b->value, NULL);
1071 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1079 * floating point division
1081 tarval *tarval_quo(tarval *a, tarval *b) {
1084 assert((a->mode == b->mode) && mode_is_float(a->mode));
1089 if (get_mode_n_vector_elems(a->mode) > 1) {
1090 /* vector arithmetic not implemented yet */
1094 fc_div(a->value, b->value, NULL);
1095 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1100 * overflow is impossible, but look out for division by zero
1102 tarval *tarval_div(tarval *a, tarval *b) {
1105 assert((a->mode == b->mode) && mode_is_int(a->mode));
1107 if (get_mode_n_vector_elems(a->mode) > 1) {
1108 /* vector arithmetic not implemented yet */
1113 if (b == get_mode_null(b->mode)) return tarval_bad;
1114 /* modes of a,b are equal */
1115 sc_div(a->value, b->value, NULL);
1116 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1121 * overflow is impossible, but look out for division by zero
1123 tarval *tarval_mod(tarval *a, tarval *b) {
1126 assert((a->mode == b->mode) && mode_is_int(a->mode));
1128 if (get_mode_n_vector_elems(a->mode) > 1) {
1129 /* vector arithmetic not implemented yet */
1134 if (b == get_mode_null(b->mode)) return tarval_bad;
1135 /* modes of a,b are equal */
1136 sc_mod(a->value, b->value, NULL);
1137 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1143 tarval *tarval_abs(tarval *a) {
1147 assert(mode_is_num(a->mode));
1149 if (get_mode_n_vector_elems(a->mode) > 1) {
1150 /* vector arithmetic not implemented yet */
1154 switch (get_mode_sort(a->mode)) {
1155 case irms_int_number:
1156 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1157 buffer = alloca(sc_get_buffer_length());
1158 sc_neg(a->value, buffer);
1159 return get_tarval_overflow(buffer, a->length, a->mode);
1163 case irms_float_number:
1167 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1168 fc_neg(a->value, NULL);
1169 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1182 tarval *tarval_and(tarval *a, tarval *b) {
1185 assert(a->mode == b->mode);
1187 /* works even for vector modes */
1189 switch(get_mode_sort(a->mode)) {
1190 case irms_internal_boolean:
1191 return (a == tarval_b_false) ? a : b;
1193 case irms_int_number:
1194 sc_and(a->value, b->value, NULL);
1195 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1198 assert(0 && "operation not defined on mode");
1206 tarval *tarval_or (tarval *a, tarval *b) {
1209 assert(a->mode == b->mode);
1211 /* works even for vector modes */
1213 switch (get_mode_sort(a->mode)) {
1214 case irms_internal_boolean:
1215 return (a == tarval_b_true) ? a : b;
1217 case irms_int_number:
1218 sc_or(a->value, b->value, NULL);
1219 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1222 assert(0 && "operation not defined on mode");
1228 * bitwise exclusive or (xor)
1230 tarval *tarval_eor(tarval *a, tarval *b) {
1233 assert((a->mode == b->mode));
1235 /* works even for vector modes */
1237 switch (get_mode_sort(a->mode)) {
1238 case irms_internal_boolean:
1239 return (a == b)? tarval_b_false : tarval_b_true;
1241 case irms_int_number:
1242 sc_xor(a->value, b->value, NULL);
1243 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1246 assert(0 && "operation not defined on mode");
1252 * bitwise left shift
1254 tarval *tarval_shl(tarval *a, tarval *b) {
1255 char *temp_val = NULL;
1259 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1261 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1262 /* vector arithmetic not implemented yet */
1266 if (get_mode_modulo_shift(a->mode) != 0) {
1267 temp_val = alloca(sc_get_buffer_length());
1269 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1270 sc_mod(b->value, temp_val, temp_val);
1272 temp_val = (char*)b->value;
1274 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1275 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1279 * bitwise unsigned right shift
1281 tarval *tarval_shr(tarval *a, tarval *b) {
1282 char *temp_val = NULL;
1286 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1288 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1289 /* vector arithmetic not implemented yet */
1293 if (get_mode_modulo_shift(a->mode) != 0) {
1294 temp_val = alloca(sc_get_buffer_length());
1296 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1297 sc_mod(b->value, temp_val, temp_val);
1299 temp_val = (char*)b->value;
1301 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1302 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1306 * bitwise signed right shift
1308 tarval *tarval_shrs(tarval *a, tarval *b) {
1309 char *temp_val = NULL;
1313 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1315 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1316 /* vector arithmetic not implemented yet */
1320 if (get_mode_modulo_shift(a->mode) != 0) {
1321 temp_val = alloca(sc_get_buffer_length());
1323 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1324 sc_mod(b->value, temp_val, temp_val);
1326 temp_val = (char*)b->value;
1328 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1329 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1335 tarval *tarval_rot(tarval *a, tarval *b) {
1336 char *temp_val = NULL;
1340 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1342 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1343 /* vector arithmetic not implemented yet */
1347 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);
1353 temp_val = (char*)b->value;
1355 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1356 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1360 * carry flag of the last operation
1362 int tarval_carry(void) {
1363 return sc_had_carry();
1369 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1370 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1374 const tarval_mode_info *mode_info;
1375 const char *prefix, *suffix;
1377 mode_info = tv->mode->tv_priv;
1379 mode_info = &default_info;
1380 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1381 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1383 switch (get_mode_sort(tv->mode)) {
1384 case irms_reference:
1385 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1387 case irms_int_number:
1388 switch (mode_info->mode_output) {
1391 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1395 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1401 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1404 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1406 case irms_float_number:
1407 switch (mode_info->mode_output) {
1409 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1412 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1417 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1421 case irms_internal_boolean:
1422 switch (mode_info->mode_output) {
1428 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1432 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1435 case irms_control_flow:
1437 case irms_auxiliary:
1438 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1445 * Output of tarvals to stdio.
1447 int tarval_printf(tarval *tv) {
1451 res = tarval_snprintf(buf, sizeof(buf), tv);
1452 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1457 char *get_tarval_bitpattern(tarval *tv) {
1459 int n = get_mode_size_bits(tv->mode);
1460 int bytes = (n + 7) / 8;
1461 char *res = xmalloc((n + 1) * sizeof(char));
1464 for(i = 0; i < bytes; i++) {
1465 byte = get_tarval_sub_bits(tv, i);
1466 for(j = 1; j < 256; j <<= 1)
1468 res[pos++] = j & byte ? '1' : '0';
1477 * access to the bitpattern
1479 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1480 switch (get_mode_arithmetic(tv->mode)) {
1481 case irma_twos_complement:
1482 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1484 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1486 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1491 * Specify the output options of one mode.
1493 * This functions stores the modinfo, so DO NOT DESTROY it.
1495 * Returns zero on success.
1497 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1500 mode->tv_priv = modeinfo;
1505 * Returns the output options of one mode.
1507 * This functions returns the mode info of a given mode.
1509 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1512 return mode->tv_priv;
1516 * Identifying tarvals values for algebraic simplifications.
1519 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1520 * - TV_CLASSIFY_ONE for multiplicative neutral,
1521 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1522 * - TV_CLASSIFY_OTHER else
1524 tarval_classification_t classify_tarval(tarval *tv) {
1525 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1527 if (tv == get_mode_null(tv->mode))
1528 return TV_CLASSIFY_NULL;
1529 else if (tv == get_mode_one(tv->mode))
1530 return TV_CLASSIFY_ONE;
1531 else if ((get_mode_sort(tv->mode) == irms_int_number)
1532 && (tv == get_mode_minus_one(tv->mode)))
1533 return TV_CLASSIFY_ALL_ONE;
1535 return TV_CLASSIFY_OTHER;
1539 * Returns non-zero if a given (integer) tarval has only one single bit
1542 int tarval_is_single_bit(tarval *tv) {
1546 if (!tv || tv == tarval_bad) return 0;
1547 if (! mode_is_int(tv->mode)) return 0;
1549 l = get_mode_size_bytes(tv->mode);
1550 for (bits = 0, i = l - 1; i >= 0; --i) {
1551 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1553 /* check for more than one bit in these */
1565 * Returns non-zero if the mantissa of a floating point IEEE-754
1566 * tarval is zero (i.e. 1.0Exxx)
1568 int tarval_ieee754_zero_mantissa(tarval *tv) {
1569 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1570 return fc_zero_mantissa(tv->value);
1573 /* Returns the exponent of a floating point IEEE-754 tarval. */
1574 int tarval_ieee754_get_exponent(tarval *tv) {
1575 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1576 return fc_get_exponent(tv->value);
1579 /* Set the immediate precision for IEEE-754 results. */
1580 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1581 return fc_set_immediate_precision(bits);
1584 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1585 unsigned tarval_ieee754_get_exact(void) {
1586 return fc_is_exact();
1590 * Sets the overflow mode for integer operations.
1592 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1593 int_overflow_mode = ov_mode;
1596 /* Get the overflow mode for integer operations. */
1597 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1598 return int_overflow_mode;
1601 /* Enable/Disable floating point constant folding. */
1602 int tarval_enable_fp_ops(int enable) {
1603 int old = !no_float;
1610 * default mode_info for output as HEX
1612 static const tarval_mode_info hex_output = {
1619 * Initialization of the tarval module: called before init_mode()
1621 void init_tarval_1(long null_value) {
1622 _null_value = null_value;
1624 /* initialize the sets holding the tarvals with a comparison function and
1625 * an initial size, which is the expected number of constants */
1626 tarvals = new_set(memcmp, N_CONSTANTS);
1627 values = new_set(memcmp, N_CONSTANTS);
1628 /* init strcalc with precision of 68 to support floating point values with 64
1629 * bit mantissa (needs extra bits for rounding and overflow) */
1635 * Initialization of the tarval module: called after init_mode()
1637 void init_tarval_2(void) {
1638 tarval_bad->kind = k_tarval;
1639 tarval_bad->mode = mode_BAD;
1640 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1642 tarval_undefined->kind = k_tarval;
1643 tarval_undefined->mode = mode_ANY;
1644 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1646 tarval_b_true->kind = k_tarval;
1647 tarval_b_true->mode = mode_b;
1648 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1650 tarval_b_false->kind = k_tarval;
1651 tarval_b_false->mode = mode_b;
1652 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1655 * assign output modes that are compatible with the
1656 * old implementation: Hex output
1658 set_tarval_mode_output_option(mode_Bs, &hex_output);
1659 set_tarval_mode_output_option(mode_Bu, &hex_output);
1660 set_tarval_mode_output_option(mode_Hs, &hex_output);
1661 set_tarval_mode_output_option(mode_Hu, &hex_output);
1662 set_tarval_mode_output_option(mode_Is, &hex_output);
1663 set_tarval_mode_output_option(mode_Iu, &hex_output);
1664 set_tarval_mode_output_option(mode_Ls, &hex_output);
1665 set_tarval_mode_output_option(mode_Lu, &hex_output);
1666 set_tarval_mode_output_option(mode_P, &hex_output);
1669 /* free all memory occupied by tarval. */
1670 void finish_tarval(void) {
1673 del_set(tarvals); tarvals = NULL;
1674 del_set(values); values = NULL;
1677 /****************************************************************************
1679 ****************************************************************************/