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 char *temp = alloca(length);
172 memcpy(temp, value, length);
173 if (get_mode_arithmetic(mode) == irma_twos_complement) {
174 sign_extend(temp, mode);
176 tv.value = INSERT_VALUE(temp, length);
180 /* if there is such a tarval, it is returned, else tv is copied
182 return (tarval *)INSERT_TARVAL(&tv);
188 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
192 switch (get_mode_sort(mode)) {
194 /* addresses always wrap around */
195 temp = alloca(sc_get_buffer_length());
196 memcpy(temp, value, sc_get_buffer_length());
197 sc_truncate(get_mode_size_bits(mode), temp);
198 /* the sc_ module expects that all bits are set ... */
199 sign_extend(temp, mode);
200 return get_tarval(temp, length, mode);
202 case irms_int_number:
203 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
204 switch (GET_OVERFLOW_MODE()) {
205 case TV_OVERFLOW_SATURATE:
206 return get_mode_max(mode);
207 case TV_OVERFLOW_WRAP:
208 temp = alloca(sc_get_buffer_length());
209 memcpy(temp, value, sc_get_buffer_length());
210 sc_truncate(get_mode_size_bits(mode), temp);
211 /* the sc_ module expects that all bits are set ... */
212 sign_extend(temp, mode);
213 return get_tarval(temp, length, mode);
214 case TV_OVERFLOW_BAD:
217 return get_tarval(value, length, mode);
220 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
221 switch (GET_OVERFLOW_MODE()) {
222 case TV_OVERFLOW_SATURATE:
223 return get_mode_min(mode);
224 case TV_OVERFLOW_WRAP: {
225 char *temp = alloca(sc_get_buffer_length());
226 memcpy(temp, value, sc_get_buffer_length());
227 sc_truncate(get_mode_size_bits(mode), temp);
228 return get_tarval(temp, length, mode);
230 case TV_OVERFLOW_BAD:
233 return get_tarval(value, length, mode);
238 case irms_float_number:
239 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
240 /* clip infinity to maximum value */
241 return fc_is_negative(value) ? get_mode_min(mode) : get_mode_max(mode);
244 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
245 /* clip denormals to zero */
246 return get_mode_null(mode);
253 return get_tarval(value, length, mode);
257 * public variables declared in tv.h
259 static tarval reserved_tv[4];
261 tarval *tarval_bad = &reserved_tv[0];
262 tarval *tarval_undefined = &reserved_tv[1];
263 tarval *tarval_b_false = &reserved_tv[2];
264 tarval *tarval_b_true = &reserved_tv[3];
267 * public functions declared in tv.h
271 * Constructors =============================================================
273 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
279 switch (get_mode_sort(mode)) {
280 case irms_control_flow:
286 case irms_internal_boolean:
287 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
288 if (strcasecmp(str, "true")) return tarval_b_true;
289 else if (strcasecmp(str, "false")) return tarval_b_true;
291 /* XXX This is C semantics */
292 return atoi(str) ? tarval_b_true : tarval_b_false;
294 case irms_float_number:
295 switch (get_mode_size_bits(mode)) {
297 fc_val_from_str(str, len, 8, 23, NULL);
300 fc_val_from_str(str, len, 11, 52, NULL);
303 fc_val_from_str(str, len, 15, 64, NULL);
306 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
309 /* same as integer modes */
310 case irms_int_number:
311 sc_val_from_str(str, len, NULL, mode);
312 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
315 assert(0); /* can't be reached, can it? */
320 * helper function, create a tarval from long
322 tarval *new_tarval_from_long(long l, ir_mode *mode) {
325 switch (get_mode_sort(mode)) {
326 case irms_internal_boolean:
327 /* XXX C semantics ! */
328 return l ? tarval_b_true : tarval_b_false ;
331 /* same as integer modes */
332 case irms_int_number:
333 sc_val_from_long(l, NULL);
334 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
336 case irms_float_number:
337 return new_tarval_from_double((long double)l, mode);
340 assert(0 && "unsupported mode sort");
345 /* returns non-zero if can be converted to long */
346 int tarval_is_long(tarval *tv) {
347 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
350 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
351 /* the value might be too big to fit in a long */
352 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
353 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
354 /* really doesn't fit */
361 /* this might overflow the machine's long, so use only with small values */
362 long get_tarval_long(tarval* tv) {
363 assert(tarval_is_long(tv) && "tarval too big to fit in long");
365 return sc_val_to_long(tv->value);
368 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
369 assert(mode && (get_mode_sort(mode) == irms_float_number));
371 switch (get_mode_size_bits(mode)) {
373 fc_val_from_ieee754(d, 8, 23, NULL);
376 fc_val_from_ieee754(d, 11, 52, NULL);
379 fc_val_from_ieee754(d, 15, 64, NULL);
382 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
385 /* returns non-zero if can be converted to double */
386 int tarval_is_double(tarval *tv) {
389 return (get_mode_sort(tv->mode) == irms_float_number);
392 long double get_tarval_double(tarval *tv) {
393 assert(tarval_is_double(tv));
395 return fc_val_to_ieee754(tv->value);
400 * Access routines for tarval fields ========================================
403 /* get the mode of the tarval */
404 ir_mode *(get_tarval_mode)(const tarval *tv) {
405 return _get_tarval_mode(tv);
409 * Special value query functions ============================================
411 * These functions calculate and return a tarval representing the requested
413 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
414 * functions, but these are stored on initialization of the irmode module and
415 * therefore the irmode functions should be preferred to the functions below.
418 tarval *(get_tarval_bad)(void) {
419 return _get_tarval_bad();
422 tarval *(get_tarval_undefined)(void) {
423 return _get_tarval_undefined();
426 tarval *(get_tarval_b_false)(void) {
427 return _get_tarval_b_false();
430 tarval *(get_tarval_b_true)(void) {
431 return _get_tarval_b_true();
434 tarval *get_tarval_max(ir_mode *mode) {
437 if (get_mode_n_vector_elems(mode) > 1) {
438 /* vector arithmetic not implemented yet */
442 switch(get_mode_sort(mode)) {
443 case irms_control_flow:
449 case irms_internal_boolean:
450 return tarval_b_true;
452 case irms_float_number:
453 switch(get_mode_size_bits(mode)) {
455 fc_get_max(8, 23, NULL);
458 fc_get_max(11, 52, NULL);
461 fc_get_max(15, 64, NULL);
464 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
467 case irms_int_number:
468 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
469 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
474 tarval *get_tarval_min(ir_mode *mode) {
477 if (get_mode_n_vector_elems(mode) > 1) {
478 /* vector arithmetic not implemented yet */
482 switch(get_mode_sort(mode)) {
483 case irms_control_flow:
489 case irms_internal_boolean:
490 return tarval_b_false;
492 case irms_float_number:
493 switch(get_mode_size_bits(mode)) {
495 fc_get_min(8, 23, NULL);
498 fc_get_min(11, 52, NULL);
501 fc_get_min(15, 64, NULL);
504 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
507 case irms_int_number:
508 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
509 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
514 /** The bit pattern for the pointer NULL */
515 static long _null_value = 0;
517 tarval *get_tarval_null(ir_mode *mode) {
520 if (get_mode_n_vector_elems(mode) > 1) {
521 /* vector arithmetic not implemented yet */
525 switch(get_mode_sort(mode)) {
526 case irms_control_flow:
532 case irms_float_number:
533 return new_tarval_from_double(0.0, mode);
535 case irms_internal_boolean:
536 case irms_int_number:
537 return new_tarval_from_long(0l, mode);
540 return new_tarval_from_long(_null_value, mode);
545 tarval *get_tarval_one(ir_mode *mode) {
548 if (get_mode_n_vector_elems(mode) > 1) {
549 /* vector arithmetic not implemented yet */
554 switch(get_mode_sort(mode)) {
555 case irms_control_flow:
561 case irms_internal_boolean:
562 return tarval_b_true;
564 case irms_float_number:
565 return new_tarval_from_double(1.0, mode);
568 case irms_int_number:
569 return new_tarval_from_long(1l, mode);
574 tarval *get_tarval_all_one(ir_mode *mode) {
577 if (get_mode_n_vector_elems(mode) > 1) {
578 /* vector arithmetic not implemented yet */
583 switch(get_mode_sort(mode)) {
584 case irms_control_flow:
590 case irms_int_number:
591 case irms_internal_boolean:
593 return tarval_not(get_mode_null(mode));
596 case irms_float_number:
597 return new_tarval_from_double(1.0, mode);
602 tarval *get_tarval_minus_one(ir_mode *mode) {
605 if (get_mode_n_vector_elems(mode) > 1) {
606 /* vector arithmetic not implemented yet */
610 switch(get_mode_sort(mode)) {
611 case irms_control_flow:
614 case irms_internal_boolean:
621 case irms_float_number:
622 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
624 case irms_int_number:
625 return new_tarval_from_long(-1l, mode);
630 tarval *get_tarval_nan(ir_mode *mode) {
633 if (get_mode_n_vector_elems(mode) > 1) {
634 /* vector arithmetic not implemented yet */
638 if (get_mode_sort(mode) == irms_float_number) {
639 switch(get_mode_size_bits(mode)) {
641 fc_get_qnan(8, 23, NULL);
644 fc_get_qnan(11, 52, NULL);
647 fc_get_qnan(15, 64, NULL);
650 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
652 assert(0 && "tarval is not floating point");
657 tarval *get_tarval_plus_inf(ir_mode *mode) {
660 if (get_mode_n_vector_elems(mode) > 1) {
661 /* vector arithmetic not implemented yet */
665 if (get_mode_sort(mode) == irms_float_number) {
666 switch(get_mode_size_bits(mode)) {
668 fc_get_plusinf(8, 23, NULL);
671 fc_get_plusinf(11, 52, NULL);
674 fc_get_plusinf(15, 64, NULL);
677 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
679 assert(0 && "tarval is not floating point");
684 tarval *get_tarval_minus_inf(ir_mode *mode) {
687 if (get_mode_n_vector_elems(mode) > 1) {
688 /* vector arithmetic not implemented yet */
692 if (get_mode_sort(mode) == irms_float_number) {
693 switch(get_mode_size_bits(mode)) {
695 fc_get_minusinf(8, 23, NULL);
698 fc_get_minusinf(11, 52, NULL);
701 fc_get_minusinf(15, 64, NULL);
704 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
706 assert(0 && "tarval is not floating point");
712 * Arithmethic operations on tarvals ========================================
716 * test if negative number, 1 means 'yes'
718 int tarval_is_negative(tarval *a) {
721 if (get_mode_n_vector_elems(a->mode) > 1) {
722 /* vector arithmetic not implemented yet */
723 assert(0 && "tarval_is_negative is not allowed for vector modes");
727 switch (get_mode_sort(a->mode)) {
728 case irms_int_number:
729 if (!mode_is_signed(a->mode)) return 0;
731 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
733 case irms_float_number:
734 return fc_is_negative(a->value);
737 assert(0 && "not implemented");
743 * test if null, 1 means 'yes'
745 int tarval_is_null(tarval *a) {
748 a == get_mode_null(get_tarval_mode(a));
752 * test if one, 1 means 'yes'
754 int tarval_is_one(tarval *a) {
757 a == get_mode_one(get_tarval_mode(a));
760 int tarval_is_all_one(tarval *tv) {
763 tv == get_mode_all_one(get_tarval_mode(tv));
767 * test if one, 1 means 'yes'
769 int tarval_is_minus_one(tarval *a) {
772 a == get_mode_minus_one(get_tarval_mode(a));
778 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
782 if (a == tarval_bad || b == tarval_bad) {
783 assert(0 && "Comparison with tarval_bad");
787 if (a == tarval_undefined || b == tarval_undefined)
790 if (a->mode != b->mode)
793 if (get_mode_n_vector_elems(a->mode) > 1) {
794 /* vector arithmetic not implemented yet */
795 assert(0 && "cmp not implemented for vector modes");
798 /* Here the two tarvals are unequal and of the same mode */
799 switch (get_mode_sort(a->mode)) {
800 case irms_control_flow:
807 case irms_float_number:
811 * BEWARE: we cannot compare a == b here, because
812 * a NaN is always Unordered to any other value, even to itself!
814 switch (fc_comp(a->value, b->value)) {
815 case -1: return pn_Cmp_Lt;
816 case 0: return pn_Cmp_Eq;
817 case 1: return pn_Cmp_Gt;
818 case 2: return pn_Cmp_Uo;
819 default: return pn_Cmp_False;
822 case irms_int_number:
825 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
827 case irms_internal_boolean:
830 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
836 * convert to other mode
838 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
845 if (src->mode == dst_mode) return src;
847 if (get_mode_n_vector_elems(src->mode) > 1) {
848 /* vector arithmetic not implemented yet */
852 switch (get_mode_sort(src->mode)) {
853 case irms_control_flow:
858 /* cast float to something */
859 case irms_float_number:
860 switch (get_mode_sort(dst_mode)) {
861 case irms_float_number:
862 switch (get_mode_size_bits(dst_mode)) {
864 fc_cast(src->value, 8, 23, NULL);
867 fc_cast(src->value, 11, 52, NULL);
870 fc_cast(src->value, 15, 64, NULL);
875 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
877 case irms_int_number:
878 switch (GET_FLOAT_TO_INT_MODE()) {
880 res = fc_int(src->value, NULL);
883 res = fc_rnd(src->value, NULL);
889 buffer = alloca(sc_get_buffer_length());
890 if (! fc_flt2int(res, buffer, dst_mode))
892 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
895 /* the rest can't be converted */
900 /* cast int/characters to something */
901 case irms_int_number:
902 switch (get_mode_sort(dst_mode)) {
905 case irms_int_number:
906 buffer = alloca(sc_get_buffer_length());
907 memcpy(buffer, src->value, sc_get_buffer_length());
908 sign_extend(buffer, dst_mode);
909 return get_tarval_overflow(buffer, src->length, dst_mode);
911 case irms_internal_boolean:
912 /* XXX C semantics */
913 if (src == get_mode_null(src->mode)) return tarval_b_false;
914 else return tarval_b_true;
916 case irms_float_number:
917 /* XXX floating point unit does not understand internal integer
918 * representation, convert to string first, then create float from
920 buffer = alloca(100);
921 /* decimal string representation because hexadecimal output is
922 * interpreted unsigned by fc_val_from_str, so this is a HACK */
923 snprintf(buffer, 100, "%s",
924 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
925 buffer[100 - 1] = '\0';
926 switch (get_mode_size_bits(dst_mode)) {
928 fc_val_from_str(buffer, 0, 8, 23, NULL);
931 fc_val_from_str(buffer, 0, 11, 52, NULL);
934 fc_val_from_str(buffer, 0, 15, 64, NULL);
937 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
944 case irms_internal_boolean:
945 /* beware: this is C semantic for the INTERNAL boolean mode */
946 if (get_mode_sort(dst_mode) == irms_int_number)
947 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
951 if (get_mode_sort(dst_mode) == irms_int_number) {
952 buffer = alloca(sc_get_buffer_length());
953 memcpy(buffer, src->value, sc_get_buffer_length());
954 sign_extend(buffer, src->mode);
955 return get_tarval_overflow(buffer, src->length, dst_mode);
966 tarval *tarval_not(tarval *a) {
971 /* works for vector mode without changes */
973 switch (get_mode_sort(a->mode)) {
975 case irms_int_number:
976 buffer = alloca(sc_get_buffer_length());
977 sc_not(a->value, buffer);
978 return get_tarval(buffer, a->length, a->mode);
980 case irms_internal_boolean:
981 if (a == tarval_b_true)
982 return tarval_b_false;
983 if (a == tarval_b_false)
984 return tarval_b_true;
988 assert(0 && "bitwise negation is only allowed for integer and boolean");
994 * arithmetic negation
996 tarval *tarval_neg(tarval *a) {
1000 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1002 /* note: negation is allowed even for unsigned modes. */
1004 if (get_mode_n_vector_elems(a->mode) > 1) {
1005 /* vector arithmetic not implemented yet */
1009 switch (get_mode_sort(a->mode)) {
1010 case irms_int_number:
1011 buffer = alloca(sc_get_buffer_length());
1012 sc_neg(a->value, buffer);
1013 return get_tarval_overflow(buffer, a->length, a->mode);
1015 case irms_float_number:
1019 fc_neg(a->value, NULL);
1020 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1030 tarval *tarval_add(tarval *a, tarval *b) {
1035 assert(a->mode == b->mode);
1037 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1038 /* vector arithmetic not implemented yet */
1042 switch (get_mode_sort(a->mode)) {
1043 case irms_int_number:
1044 /* modes of a,b are equal, so result has mode of a as this might be the character */
1045 buffer = alloca(sc_get_buffer_length());
1046 sc_add(a->value, b->value, buffer);
1047 return get_tarval_overflow(buffer, a->length, a->mode);
1049 case irms_float_number:
1053 fc_add(a->value, b->value, NULL);
1054 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1064 tarval *tarval_sub(tarval *a, tarval *b) {
1069 assert(a->mode == b->mode);
1071 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1072 /* vector arithmetic not implemented yet */
1075 switch (get_mode_sort(a->mode)) {
1076 case irms_int_number:
1077 /* modes of a,b are equal, so result has mode of a as this might be the character */
1078 buffer = alloca(sc_get_buffer_length());
1079 sc_sub(a->value, b->value, buffer);
1080 return get_tarval_overflow(buffer, a->length, a->mode);
1082 case irms_float_number:
1086 fc_sub(a->value, b->value, NULL);
1087 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1097 tarval *tarval_mul(tarval *a, tarval *b) {
1102 assert(a->mode == b->mode);
1104 if (get_mode_n_vector_elems(a->mode) > 1) {
1105 /* vector arithmetic not implemented yet */
1109 switch (get_mode_sort(a->mode)) {
1110 case irms_int_number:
1111 /* modes of a,b are equal */
1112 buffer = alloca(sc_get_buffer_length());
1113 sc_mul(a->value, b->value, buffer);
1114 return get_tarval_overflow(buffer, a->length, a->mode);
1116 case irms_float_number:
1120 fc_mul(a->value, b->value, NULL);
1121 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1129 * floating point division
1131 tarval *tarval_quo(tarval *a, tarval *b) {
1134 assert((a->mode == b->mode) && mode_is_float(a->mode));
1139 if (get_mode_n_vector_elems(a->mode) > 1) {
1140 /* vector arithmetic not implemented yet */
1144 fc_div(a->value, b->value, NULL);
1145 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1150 * overflow is impossible, but look out for division by zero
1152 tarval *tarval_div(tarval *a, tarval *b) {
1155 assert((a->mode == b->mode) && mode_is_int(a->mode));
1157 if (get_mode_n_vector_elems(a->mode) > 1) {
1158 /* vector arithmetic not implemented yet */
1163 if (b == get_mode_null(b->mode)) return tarval_bad;
1164 /* modes of a,b are equal */
1165 sc_div(a->value, b->value, NULL);
1166 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1171 * overflow is impossible, but look out for division by zero
1173 tarval *tarval_mod(tarval *a, tarval *b) {
1176 assert((a->mode == b->mode) && mode_is_int(a->mode));
1178 if (get_mode_n_vector_elems(a->mode) > 1) {
1179 /* vector arithmetic not implemented yet */
1184 if (b == get_mode_null(b->mode)) return tarval_bad;
1185 /* modes of a,b are equal */
1186 sc_mod(a->value, b->value, NULL);
1187 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1191 * integer division AND remainder
1192 * overflow is impossible, but look out for division by zero
1194 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1195 int len = sc_get_buffer_length();
1196 char *div_res = alloca(len);
1197 char *mod_res = alloca(len);
1201 assert((a->mode == b->mode) && mode_is_int(a->mode));
1203 if (get_mode_n_vector_elems(a->mode) > 1) {
1204 /* vector arithmetic not implemented yet */
1210 if (b == get_mode_null(b->mode)) return tarval_bad;
1211 /* modes of a,b are equal */
1212 sc_divmod(a->value, b->value, div_res, mod_res);
1213 *mod = get_tarval(mod_res, len, a->mode);
1214 return get_tarval(div_res, len, a->mode);
1220 tarval *tarval_abs(tarval *a) {
1224 assert(mode_is_num(a->mode));
1226 if (get_mode_n_vector_elems(a->mode) > 1) {
1227 /* vector arithmetic not implemented yet */
1231 switch (get_mode_sort(a->mode)) {
1232 case irms_int_number:
1233 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1234 buffer = alloca(sc_get_buffer_length());
1235 sc_neg(a->value, buffer);
1236 return get_tarval_overflow(buffer, a->length, a->mode);
1240 case irms_float_number:
1244 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1245 fc_neg(a->value, NULL);
1246 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1259 tarval *tarval_and(tarval *a, tarval *b) {
1262 assert(a->mode == b->mode);
1264 /* works even for vector modes */
1266 switch(get_mode_sort(a->mode)) {
1267 case irms_internal_boolean:
1268 return (a == tarval_b_false) ? a : b;
1270 case irms_int_number:
1271 sc_and(a->value, b->value, NULL);
1272 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1275 assert(0 && "operation not defined on mode");
1283 tarval *tarval_or (tarval *a, tarval *b) {
1286 assert(a->mode == b->mode);
1288 /* works even for vector modes */
1290 switch (get_mode_sort(a->mode)) {
1291 case irms_internal_boolean:
1292 return (a == tarval_b_true) ? a : b;
1294 case irms_int_number:
1295 sc_or(a->value, b->value, NULL);
1296 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1299 assert(0 && "operation not defined on mode");
1305 * bitwise exclusive or (xor)
1307 tarval *tarval_eor(tarval *a, tarval *b) {
1310 assert((a->mode == b->mode));
1312 /* works even for vector modes */
1314 switch (get_mode_sort(a->mode)) {
1315 case irms_internal_boolean:
1316 return (a == b)? tarval_b_false : tarval_b_true;
1318 case irms_int_number:
1319 sc_xor(a->value, b->value, NULL);
1320 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1323 assert(0 && "operation not defined on mode");
1329 * bitwise left shift
1331 tarval *tarval_shl(tarval *a, tarval *b) {
1332 char *temp_val = NULL;
1336 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1338 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1339 /* vector arithmetic not implemented yet */
1343 if (get_mode_modulo_shift(a->mode) != 0) {
1344 temp_val = alloca(sc_get_buffer_length());
1346 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1347 sc_mod(b->value, temp_val, temp_val);
1349 temp_val = (char*)b->value;
1351 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1352 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1356 * bitwise unsigned right shift
1358 tarval *tarval_shr(tarval *a, tarval *b) {
1359 char *temp_val = NULL;
1363 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1365 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1366 /* vector arithmetic not implemented yet */
1370 if (get_mode_modulo_shift(a->mode) != 0) {
1371 temp_val = alloca(sc_get_buffer_length());
1373 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1374 sc_mod(b->value, temp_val, temp_val);
1376 temp_val = (char*)b->value;
1378 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1379 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1383 * bitwise signed right shift
1385 tarval *tarval_shrs(tarval *a, tarval *b) {
1386 char *temp_val = NULL;
1390 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1392 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1393 /* vector arithmetic not implemented yet */
1397 if (get_mode_modulo_shift(a->mode) != 0) {
1398 temp_val = alloca(sc_get_buffer_length());
1400 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1401 sc_mod(b->value, temp_val, temp_val);
1403 temp_val = (char*)b->value;
1405 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1406 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1412 tarval *tarval_rot(tarval *a, tarval *b) {
1413 char *temp_val = NULL;
1417 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1419 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1420 /* vector arithmetic not implemented yet */
1424 if (get_mode_modulo_shift(a->mode) != 0) {
1425 temp_val = alloca(sc_get_buffer_length());
1427 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1428 sc_mod(b->value, temp_val, temp_val);
1430 temp_val = (char*)b->value;
1432 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1433 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1437 * carry flag of the last operation
1439 int tarval_carry(void) {
1440 return sc_had_carry();
1446 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1447 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1451 const tarval_mode_info *mode_info;
1452 const char *prefix, *suffix;
1454 mode_info = tv->mode->tv_priv;
1456 mode_info = &default_info;
1457 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1458 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1460 switch (get_mode_sort(tv->mode)) {
1461 case irms_reference:
1462 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1464 case irms_int_number:
1465 switch (mode_info->mode_output) {
1468 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1472 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1478 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1481 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1483 case irms_float_number:
1484 switch (mode_info->mode_output) {
1486 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1489 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1494 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1498 case irms_internal_boolean:
1499 switch (mode_info->mode_output) {
1505 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1509 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1512 case irms_control_flow:
1514 case irms_auxiliary:
1515 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1522 * Output of tarvals to stdio.
1524 int tarval_printf(tarval *tv) {
1528 res = tarval_snprintf(buf, sizeof(buf), tv);
1529 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1534 char *get_tarval_bitpattern(tarval *tv) {
1536 int n = get_mode_size_bits(tv->mode);
1537 int bytes = (n + 7) / 8;
1538 char *res = xmalloc((n + 1) * sizeof(char));
1541 for(i = 0; i < bytes; i++) {
1542 byte = get_tarval_sub_bits(tv, i);
1543 for(j = 1; j < 256; j <<= 1)
1545 res[pos++] = j & byte ? '1' : '0';
1554 * access to the bitpattern
1556 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1557 switch (get_mode_arithmetic(tv->mode)) {
1558 case irma_twos_complement:
1559 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1561 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1563 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1568 * Specify the output options of one mode.
1570 * This functions stores the modinfo, so DO NOT DESTROY it.
1572 * Returns zero on success.
1574 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1577 mode->tv_priv = modeinfo;
1582 * Returns the output options of one mode.
1584 * This functions returns the mode info of a given mode.
1586 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1589 return mode->tv_priv;
1593 * Returns non-zero if a given (integer) tarval has only one single bit
1596 int tarval_is_single_bit(tarval *tv) {
1600 if (!tv || tv == tarval_bad) return 0;
1601 if (! mode_is_int(tv->mode)) return 0;
1603 l = get_mode_size_bytes(tv->mode);
1604 for (bits = 0, i = l - 1; i >= 0; --i) {
1605 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1607 /* check for more than one bit in these */
1619 * Returns non-zero if the mantissa of a floating point IEEE-754
1620 * tarval is zero (i.e. 1.0Exxx)
1622 int tarval_ieee754_zero_mantissa(tarval *tv) {
1623 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1624 return fc_zero_mantissa(tv->value);
1627 /* Returns the exponent of a floating point IEEE-754 tarval. */
1628 int tarval_ieee754_get_exponent(tarval *tv) {
1629 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1630 return fc_get_exponent(tv->value);
1633 /* Set the immediate precision for IEEE-754 results. */
1634 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1635 return fc_set_immediate_precision(bits);
1638 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1639 unsigned tarval_ieee754_get_exact(void) {
1640 return fc_is_exact();
1643 /* check if its the a floating point NaN */
1644 int tarval_is_NaN(tarval *tv) {
1645 if (! mode_is_float(tv->mode))
1647 return fc_is_nan(tv->value);
1650 /* check if its the a floating point +inf */
1651 int tarval_is_plus_inf(tarval *tv) {
1652 if (! mode_is_float(tv->mode))
1654 return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1657 /* check if its the a floating point -inf */
1658 int tarval_is_minus_inf(tarval *tv) {
1659 if (! mode_is_float(tv->mode))
1661 return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1664 /* check if the tarval represents a finite value */
1665 int tarval_is_finite(tarval *tv) {
1666 if (mode_is_float(tv->mode))
1667 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1672 * Sets the overflow mode for integer operations.
1674 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1675 int_overflow_mode = ov_mode;
1678 /* Get the overflow mode for integer operations. */
1679 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1680 return int_overflow_mode;
1683 /* Enable/Disable floating point constant folding. */
1684 int tarval_enable_fp_ops(int enable) {
1685 int old = !no_float;
1692 * default mode_info for output as HEX
1694 static const tarval_mode_info hex_output = {
1701 * Initialization of the tarval module: called before init_mode()
1703 void init_tarval_1(long null_value) {
1704 _null_value = null_value;
1706 /* initialize the sets holding the tarvals with a comparison function and
1707 * an initial size, which is the expected number of constants */
1708 tarvals = new_set(memcmp, N_CONSTANTS);
1709 values = new_set(memcmp, N_CONSTANTS);
1710 /* init strcalc with precision of 68 to support floating point values with 64
1711 * bit mantissa (needs extra bits for rounding and overflow) */
1717 * Initialization of the tarval module: called after init_mode()
1719 void init_tarval_2(void) {
1720 tarval_bad->kind = k_tarval;
1721 tarval_bad->mode = mode_BAD;
1722 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1724 tarval_undefined->kind = k_tarval;
1725 tarval_undefined->mode = mode_ANY;
1726 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1728 tarval_b_true->kind = k_tarval;
1729 tarval_b_true->mode = mode_b;
1730 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1732 tarval_b_false->kind = k_tarval;
1733 tarval_b_false->mode = mode_b;
1734 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1737 * assign output modes that are compatible with the
1738 * old implementation: Hex output
1740 set_tarval_mode_output_option(mode_Bs, &hex_output);
1741 set_tarval_mode_output_option(mode_Bu, &hex_output);
1742 set_tarval_mode_output_option(mode_Hs, &hex_output);
1743 set_tarval_mode_output_option(mode_Hu, &hex_output);
1744 set_tarval_mode_output_option(mode_Is, &hex_output);
1745 set_tarval_mode_output_option(mode_Iu, &hex_output);
1746 set_tarval_mode_output_option(mode_Ls, &hex_output);
1747 set_tarval_mode_output_option(mode_Lu, &hex_output);
1748 set_tarval_mode_output_option(mode_P, &hex_output);
1751 /* free all memory occupied by tarval. */
1752 void finish_tarval(void) {
1755 del_set(tarvals); tarvals = NULL;
1756 del_set(values); values = NULL;
1759 /****************************************************************************
1761 ****************************************************************************/