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);
304 fc_val_from_str(str, len, 15, 64, NULL);
307 panic("Unsupported mode in new_tarval_from_str()");
309 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
312 /* same as integer modes */
313 case irms_int_number:
314 sc_val_from_str(str, len, NULL, mode);
315 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
318 assert(0); /* can't be reached, can it? */
323 * helper function, create a tarval from long
325 tarval *new_tarval_from_long(long l, ir_mode *mode) {
328 switch (get_mode_sort(mode)) {
329 case irms_internal_boolean:
330 /* XXX C semantics ! */
331 return l ? tarval_b_true : tarval_b_false ;
334 /* same as integer modes */
335 case irms_int_number:
336 sc_val_from_long(l, NULL);
337 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
339 case irms_float_number:
340 return new_tarval_from_double((long double)l, mode);
343 assert(0 && "unsupported mode sort");
348 /* returns non-zero if can be converted to long */
349 int tarval_is_long(tarval *tv) {
350 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
353 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
354 /* the value might be too big to fit in a long */
355 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
356 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
357 /* really doesn't fit */
364 /* this might overflow the machine's long, so use only with small values */
365 long get_tarval_long(tarval* tv) {
366 assert(tarval_is_long(tv) && "tarval too big to fit in long");
368 return sc_val_to_long(tv->value);
371 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
372 assert(mode && (get_mode_sort(mode) == irms_float_number));
374 switch (get_mode_size_bits(mode)) {
376 fc_val_from_ieee754(d, 8, 23, NULL);
379 fc_val_from_ieee754(d, 11, 52, NULL);
383 fc_val_from_ieee754(d, 15, 64, NULL);
386 panic("Unsupported mode in new_tarval_from_double()");
388 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
391 /* returns non-zero if can be converted to double */
392 int tarval_is_double(tarval *tv) {
395 return (get_mode_sort(tv->mode) == irms_float_number);
398 long double get_tarval_double(tarval *tv) {
399 assert(tarval_is_double(tv));
401 return fc_val_to_ieee754(tv->value);
406 * Access routines for tarval fields ========================================
409 /* get the mode of the tarval */
410 ir_mode *(get_tarval_mode)(const tarval *tv) {
411 return _get_tarval_mode(tv);
415 * Special value query functions ============================================
417 * These functions calculate and return a tarval representing the requested
419 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
420 * functions, but these are stored on initialization of the irmode module and
421 * therefore the irmode functions should be preferred to the functions below.
424 tarval *(get_tarval_bad)(void) {
425 return _get_tarval_bad();
428 tarval *(get_tarval_undefined)(void) {
429 return _get_tarval_undefined();
432 tarval *(get_tarval_b_false)(void) {
433 return _get_tarval_b_false();
436 tarval *(get_tarval_b_true)(void) {
437 return _get_tarval_b_true();
440 tarval *get_tarval_max(ir_mode *mode) {
443 if (get_mode_n_vector_elems(mode) > 1) {
444 /* vector arithmetic not implemented yet */
448 switch(get_mode_sort(mode)) {
449 case irms_control_flow:
455 case irms_internal_boolean:
456 return tarval_b_true;
458 case irms_float_number:
459 switch(get_mode_size_bits(mode)) {
461 fc_get_max(8, 23, NULL);
464 fc_get_max(11, 52, NULL);
468 fc_get_max(15, 64, NULL);
471 panic("Unsupported mode in get_tarval_max()");
473 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
476 case irms_int_number:
477 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
478 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
483 tarval *get_tarval_min(ir_mode *mode) {
486 if (get_mode_n_vector_elems(mode) > 1) {
487 /* vector arithmetic not implemented yet */
491 switch(get_mode_sort(mode)) {
492 case irms_control_flow:
498 case irms_internal_boolean:
499 return tarval_b_false;
501 case irms_float_number:
502 switch(get_mode_size_bits(mode)) {
504 fc_get_min(8, 23, NULL);
507 fc_get_min(11, 52, NULL);
511 fc_get_min(15, 64, NULL);
514 panic("Unsupported mode in get_tarval_min()");
516 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
519 case irms_int_number:
520 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
521 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
526 /** The bit pattern for the pointer NULL */
527 static long _null_value = 0;
529 tarval *get_tarval_null(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:
544 case irms_float_number:
545 return new_tarval_from_double(0.0, mode);
547 case irms_internal_boolean:
548 case irms_int_number:
549 return new_tarval_from_long(0l, mode);
552 return new_tarval_from_long(_null_value, mode);
557 tarval *get_tarval_one(ir_mode *mode) {
560 if (get_mode_n_vector_elems(mode) > 1) {
561 /* vector arithmetic not implemented yet */
566 switch(get_mode_sort(mode)) {
567 case irms_control_flow:
573 case irms_internal_boolean:
574 return tarval_b_true;
576 case irms_float_number:
577 return new_tarval_from_double(1.0, mode);
580 case irms_int_number:
581 return new_tarval_from_long(1l, mode);
586 tarval *get_tarval_all_one(ir_mode *mode) {
589 if (get_mode_n_vector_elems(mode) > 1) {
590 /* vector arithmetic not implemented yet */
595 switch(get_mode_sort(mode)) {
596 case irms_control_flow:
602 case irms_int_number:
603 case irms_internal_boolean:
605 return tarval_not(get_mode_null(mode));
608 case irms_float_number:
609 return new_tarval_from_double(1.0, mode);
614 tarval *get_tarval_minus_one(ir_mode *mode) {
617 if (get_mode_n_vector_elems(mode) > 1) {
618 /* vector arithmetic not implemented yet */
622 switch(get_mode_sort(mode)) {
623 case irms_control_flow:
626 case irms_internal_boolean:
633 case irms_float_number:
634 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
636 case irms_int_number:
637 return new_tarval_from_long(-1l, mode);
642 tarval *get_tarval_nan(ir_mode *mode) {
645 if (get_mode_n_vector_elems(mode) > 1) {
646 /* vector arithmetic not implemented yet */
650 if (get_mode_sort(mode) == irms_float_number) {
651 switch(get_mode_size_bits(mode)) {
653 fc_get_qnan(8, 23, NULL);
656 fc_get_qnan(11, 52, NULL);
660 fc_get_qnan(15, 64, NULL);
663 panic("Unsupported mode in get_tarval_nan()");
665 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
667 assert(0 && "tarval is not floating point");
672 tarval *get_tarval_plus_inf(ir_mode *mode) {
675 if (get_mode_n_vector_elems(mode) > 1) {
676 /* vector arithmetic not implemented yet */
680 if (get_mode_sort(mode) == irms_float_number) {
681 switch(get_mode_size_bits(mode)) {
683 fc_get_plusinf(8, 23, NULL);
686 fc_get_plusinf(11, 52, NULL);
690 fc_get_plusinf(15, 64, NULL);
693 panic("Unsupported mode in get_tarval_plus_inf()");
695 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
697 assert(0 && "tarval is not floating point");
702 tarval *get_tarval_minus_inf(ir_mode *mode) {
705 if (get_mode_n_vector_elems(mode) > 1) {
706 /* vector arithmetic not implemented yet */
710 if (get_mode_sort(mode) == irms_float_number) {
711 switch(get_mode_size_bits(mode)) {
713 fc_get_minusinf(8, 23, NULL);
716 fc_get_minusinf(11, 52, NULL);
720 fc_get_minusinf(15, 64, NULL);
723 panic("Unsupported mode in get_tarval_minus_inf()");
725 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
727 assert(0 && "tarval is not floating point");
733 * Arithmethic operations on tarvals ========================================
737 * test if negative number, 1 means 'yes'
739 int tarval_is_negative(tarval *a) {
742 if (get_mode_n_vector_elems(a->mode) > 1) {
743 /* vector arithmetic not implemented yet */
744 assert(0 && "tarval_is_negative is not allowed for vector modes");
748 switch (get_mode_sort(a->mode)) {
749 case irms_int_number:
750 if (!mode_is_signed(a->mode)) return 0;
752 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
754 case irms_float_number:
755 return fc_is_negative(a->value);
758 assert(0 && "not implemented");
764 * test if null, 1 means 'yes'
766 int tarval_is_null(tarval *a) {
769 a == get_mode_null(get_tarval_mode(a));
773 * test if one, 1 means 'yes'
775 int tarval_is_one(tarval *a) {
778 a == get_mode_one(get_tarval_mode(a));
781 int tarval_is_all_one(tarval *tv) {
784 tv == get_mode_all_one(get_tarval_mode(tv));
788 * test if one, 1 means 'yes'
790 int tarval_is_minus_one(tarval *a) {
793 a == get_mode_minus_one(get_tarval_mode(a));
799 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
803 if (a == tarval_bad || b == tarval_bad) {
804 assert(0 && "Comparison with tarval_bad");
808 if (a == tarval_undefined || b == tarval_undefined)
811 if (a->mode != b->mode)
814 if (get_mode_n_vector_elems(a->mode) > 1) {
815 /* vector arithmetic not implemented yet */
816 assert(0 && "cmp not implemented for vector modes");
819 /* Here the two tarvals are unequal and of the same mode */
820 switch (get_mode_sort(a->mode)) {
821 case irms_control_flow:
828 case irms_float_number:
832 * BEWARE: we cannot compare a == b here, because
833 * a NaN is always Unordered to any other value, even to itself!
835 switch (fc_comp(a->value, b->value)) {
836 case -1: return pn_Cmp_Lt;
837 case 0: return pn_Cmp_Eq;
838 case 1: return pn_Cmp_Gt;
839 case 2: return pn_Cmp_Uo;
840 default: return pn_Cmp_False;
843 case irms_int_number:
846 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
848 case irms_internal_boolean:
851 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
857 * convert to other mode
859 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
866 if (src->mode == dst_mode) return src;
868 if (get_mode_n_vector_elems(src->mode) > 1) {
869 /* vector arithmetic not implemented yet */
873 switch (get_mode_sort(src->mode)) {
874 case irms_control_flow:
879 /* cast float to something */
880 case irms_float_number:
881 switch (get_mode_sort(dst_mode)) {
882 case irms_float_number:
883 switch (get_mode_size_bits(dst_mode)) {
885 fc_cast(src->value, 8, 23, NULL);
888 fc_cast(src->value, 11, 52, NULL);
892 fc_cast(src->value, 15, 64, NULL);
895 panic("Unsupported mode in tarval_convert_to()");
897 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
899 case irms_int_number:
900 switch (GET_FLOAT_TO_INT_MODE()) {
902 res = fc_int(src->value, NULL);
905 res = fc_rnd(src->value, NULL);
911 buffer = alloca(sc_get_buffer_length());
912 if (! fc_flt2int(res, buffer, dst_mode))
914 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
917 /* the rest can't be converted */
922 /* cast int/characters to something */
923 case irms_int_number:
924 switch (get_mode_sort(dst_mode)) {
927 case irms_int_number:
928 buffer = alloca(sc_get_buffer_length());
929 memcpy(buffer, src->value, sc_get_buffer_length());
930 sign_extend(buffer, dst_mode);
931 return get_tarval_overflow(buffer, src->length, dst_mode);
933 case irms_internal_boolean:
934 /* XXX C semantics */
935 if (src == get_mode_null(src->mode)) return tarval_b_false;
936 else return tarval_b_true;
938 case irms_float_number:
939 /* XXX floating point unit does not understand internal integer
940 * representation, convert to string first, then create float from
942 buffer = alloca(100);
943 /* decimal string representation because hexadecimal output is
944 * interpreted unsigned by fc_val_from_str, so this is a HACK */
945 snprintf(buffer, 100, "%s",
946 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
947 buffer[100 - 1] = '\0';
948 switch (get_mode_size_bits(dst_mode)) {
950 fc_val_from_str(buffer, 0, 8, 23, NULL);
953 fc_val_from_str(buffer, 0, 11, 52, NULL);
957 fc_val_from_str(buffer, 0, 15, 64, NULL);
960 panic("Unsupported mode in tarval_convert_to()");
962 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
969 case irms_internal_boolean:
970 /* beware: this is C semantic for the INTERNAL boolean mode */
971 if (get_mode_sort(dst_mode) == irms_int_number)
972 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
976 if (get_mode_sort(dst_mode) == irms_int_number) {
977 buffer = alloca(sc_get_buffer_length());
978 memcpy(buffer, src->value, sc_get_buffer_length());
979 sign_extend(buffer, src->mode);
980 return get_tarval_overflow(buffer, src->length, dst_mode);
991 tarval *tarval_not(tarval *a) {
996 /* works for vector mode without changes */
998 switch (get_mode_sort(a->mode)) {
1000 case irms_int_number:
1001 buffer = alloca(sc_get_buffer_length());
1002 sc_not(a->value, buffer);
1003 return get_tarval(buffer, a->length, a->mode);
1005 case irms_internal_boolean:
1006 if (a == tarval_b_true)
1007 return tarval_b_false;
1008 if (a == tarval_b_false)
1009 return tarval_b_true;
1013 assert(0 && "bitwise negation is only allowed for integer and boolean");
1019 * arithmetic negation
1021 tarval *tarval_neg(tarval *a) {
1025 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1027 /* note: negation is allowed even for unsigned modes. */
1029 if (get_mode_n_vector_elems(a->mode) > 1) {
1030 /* vector arithmetic not implemented yet */
1034 switch (get_mode_sort(a->mode)) {
1035 case irms_int_number:
1036 buffer = alloca(sc_get_buffer_length());
1037 sc_neg(a->value, buffer);
1038 return get_tarval_overflow(buffer, a->length, a->mode);
1040 case irms_float_number:
1044 fc_neg(a->value, NULL);
1045 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1055 tarval *tarval_add(tarval *a, tarval *b) {
1060 assert(a->mode == b->mode);
1062 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1063 /* vector arithmetic not implemented yet */
1067 switch (get_mode_sort(a->mode)) {
1068 case irms_int_number:
1069 /* modes of a,b are equal, so result has mode of a as this might be the character */
1070 buffer = alloca(sc_get_buffer_length());
1071 sc_add(a->value, b->value, buffer);
1072 return get_tarval_overflow(buffer, a->length, a->mode);
1074 case irms_float_number:
1078 fc_add(a->value, b->value, NULL);
1079 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1089 tarval *tarval_sub(tarval *a, tarval *b) {
1094 assert(a->mode == b->mode);
1096 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1097 /* vector arithmetic not implemented yet */
1100 switch (get_mode_sort(a->mode)) {
1101 case irms_int_number:
1102 /* modes of a,b are equal, so result has mode of a as this might be the character */
1103 buffer = alloca(sc_get_buffer_length());
1104 sc_sub(a->value, b->value, buffer);
1105 return get_tarval_overflow(buffer, a->length, a->mode);
1107 case irms_float_number:
1111 fc_sub(a->value, b->value, NULL);
1112 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1122 tarval *tarval_mul(tarval *a, tarval *b) {
1127 assert(a->mode == b->mode);
1129 if (get_mode_n_vector_elems(a->mode) > 1) {
1130 /* vector arithmetic not implemented yet */
1134 switch (get_mode_sort(a->mode)) {
1135 case irms_int_number:
1136 /* modes of a,b are equal */
1137 buffer = alloca(sc_get_buffer_length());
1138 sc_mul(a->value, b->value, buffer);
1139 return get_tarval_overflow(buffer, a->length, a->mode);
1141 case irms_float_number:
1145 fc_mul(a->value, b->value, NULL);
1146 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1154 * floating point division
1156 tarval *tarval_quo(tarval *a, tarval *b) {
1159 assert((a->mode == b->mode) && mode_is_float(a->mode));
1164 if (get_mode_n_vector_elems(a->mode) > 1) {
1165 /* vector arithmetic not implemented yet */
1169 fc_div(a->value, b->value, NULL);
1170 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1175 * overflow is impossible, but look out for division by zero
1177 tarval *tarval_div(tarval *a, tarval *b) {
1180 assert((a->mode == b->mode) && mode_is_int(a->mode));
1182 if (get_mode_n_vector_elems(a->mode) > 1) {
1183 /* vector arithmetic not implemented yet */
1188 if (b == get_mode_null(b->mode)) return tarval_bad;
1189 /* modes of a,b are equal */
1190 sc_div(a->value, b->value, NULL);
1191 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1196 * overflow is impossible, but look out for division by zero
1198 tarval *tarval_mod(tarval *a, tarval *b) {
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 */
1209 if (b == get_mode_null(b->mode)) return tarval_bad;
1210 /* modes of a,b are equal */
1211 sc_mod(a->value, b->value, NULL);
1212 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1216 * integer division AND remainder
1217 * overflow is impossible, but look out for division by zero
1219 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1220 int len = sc_get_buffer_length();
1221 char *div_res = alloca(len);
1222 char *mod_res = alloca(len);
1226 assert((a->mode == b->mode) && mode_is_int(a->mode));
1228 if (get_mode_n_vector_elems(a->mode) > 1) {
1229 /* vector arithmetic not implemented yet */
1235 if (b == get_mode_null(b->mode)) return tarval_bad;
1236 /* modes of a,b are equal */
1237 sc_divmod(a->value, b->value, div_res, mod_res);
1238 *mod = get_tarval(mod_res, len, a->mode);
1239 return get_tarval(div_res, len, a->mode);
1245 tarval *tarval_abs(tarval *a) {
1249 assert(mode_is_num(a->mode));
1251 if (get_mode_n_vector_elems(a->mode) > 1) {
1252 /* vector arithmetic not implemented yet */
1256 switch (get_mode_sort(a->mode)) {
1257 case irms_int_number:
1258 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1259 buffer = alloca(sc_get_buffer_length());
1260 sc_neg(a->value, buffer);
1261 return get_tarval_overflow(buffer, a->length, a->mode);
1265 case irms_float_number:
1269 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1270 fc_neg(a->value, NULL);
1271 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1284 tarval *tarval_and(tarval *a, tarval *b) {
1287 assert(a->mode == b->mode);
1289 /* works even for vector modes */
1291 switch(get_mode_sort(a->mode)) {
1292 case irms_internal_boolean:
1293 return (a == tarval_b_false) ? a : b;
1295 case irms_int_number:
1296 sc_and(a->value, b->value, NULL);
1297 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1300 assert(0 && "operation not defined on mode");
1308 tarval *tarval_or (tarval *a, tarval *b) {
1311 assert(a->mode == b->mode);
1313 /* works even for vector modes */
1315 switch (get_mode_sort(a->mode)) {
1316 case irms_internal_boolean:
1317 return (a == tarval_b_true) ? a : b;
1319 case irms_int_number:
1320 sc_or(a->value, b->value, NULL);
1321 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1324 assert(0 && "operation not defined on mode");
1330 * bitwise exclusive or (xor)
1332 tarval *tarval_eor(tarval *a, tarval *b) {
1335 assert((a->mode == b->mode));
1337 /* works even for vector modes */
1339 switch (get_mode_sort(a->mode)) {
1340 case irms_internal_boolean:
1341 return (a == b)? tarval_b_false : tarval_b_true;
1343 case irms_int_number:
1344 sc_xor(a->value, b->value, NULL);
1345 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1348 assert(0 && "operation not defined on mode");
1354 * bitwise left shift
1356 tarval *tarval_shl(tarval *a, tarval *b) {
1357 char *temp_val = NULL;
1361 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1363 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1364 /* vector arithmetic not implemented yet */
1368 if (get_mode_modulo_shift(a->mode) != 0) {
1369 temp_val = alloca(sc_get_buffer_length());
1371 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1372 sc_mod(b->value, temp_val, temp_val);
1374 temp_val = (char*)b->value;
1376 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1377 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1381 * bitwise unsigned right shift
1383 tarval *tarval_shr(tarval *a, tarval *b) {
1384 char *temp_val = NULL;
1388 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1390 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1391 /* vector arithmetic not implemented yet */
1395 if (get_mode_modulo_shift(a->mode) != 0) {
1396 temp_val = alloca(sc_get_buffer_length());
1398 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1399 sc_mod(b->value, temp_val, temp_val);
1401 temp_val = (char*)b->value;
1403 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1404 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1408 * bitwise signed right shift
1410 tarval *tarval_shrs(tarval *a, tarval *b) {
1411 char *temp_val = NULL;
1415 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1417 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1418 /* vector arithmetic not implemented yet */
1422 if (get_mode_modulo_shift(a->mode) != 0) {
1423 temp_val = alloca(sc_get_buffer_length());
1425 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1426 sc_mod(b->value, temp_val, temp_val);
1428 temp_val = (char*)b->value;
1430 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1431 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1437 tarval *tarval_rot(tarval *a, tarval *b) {
1438 char *temp_val = NULL;
1442 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1444 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1445 /* vector arithmetic not implemented yet */
1449 if (get_mode_modulo_shift(a->mode) != 0) {
1450 temp_val = alloca(sc_get_buffer_length());
1452 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1453 sc_mod(b->value, temp_val, temp_val);
1455 temp_val = (char*)b->value;
1457 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1458 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1462 * carry flag of the last operation
1464 int tarval_carry(void) {
1465 return sc_had_carry();
1471 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1472 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1476 const tarval_mode_info *mode_info;
1477 const char *prefix, *suffix;
1479 mode_info = tv->mode->tv_priv;
1481 mode_info = &default_info;
1482 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1483 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1485 switch (get_mode_sort(tv->mode)) {
1486 case irms_reference:
1487 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1489 case irms_int_number:
1490 switch (mode_info->mode_output) {
1493 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1497 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1503 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1506 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1508 case irms_float_number:
1509 switch (mode_info->mode_output) {
1511 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1514 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1519 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1523 case irms_internal_boolean:
1524 switch (mode_info->mode_output) {
1530 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1534 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1537 case irms_control_flow:
1539 case irms_auxiliary:
1540 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1547 * Output of tarvals to stdio.
1549 int tarval_printf(tarval *tv) {
1553 res = tarval_snprintf(buf, sizeof(buf), tv);
1554 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1559 char *get_tarval_bitpattern(tarval *tv) {
1561 int n = get_mode_size_bits(tv->mode);
1562 int bytes = (n + 7) / 8;
1563 char *res = xmalloc((n + 1) * sizeof(char));
1566 for(i = 0; i < bytes; i++) {
1567 byte = get_tarval_sub_bits(tv, i);
1568 for(j = 1; j < 256; j <<= 1)
1570 res[pos++] = j & byte ? '1' : '0';
1579 * access to the bitpattern
1581 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1582 switch (get_mode_arithmetic(tv->mode)) {
1583 case irma_twos_complement:
1584 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1586 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1588 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1593 * Specify the output options of one mode.
1595 * This functions stores the modinfo, so DO NOT DESTROY it.
1597 * Returns zero on success.
1599 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1602 mode->tv_priv = modeinfo;
1607 * Returns the output options of one mode.
1609 * This functions returns the mode info of a given mode.
1611 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1614 return mode->tv_priv;
1618 * Returns non-zero if a given (integer) tarval has only one single bit
1621 int tarval_is_single_bit(tarval *tv) {
1625 if (!tv || tv == tarval_bad) return 0;
1626 if (! mode_is_int(tv->mode)) return 0;
1628 l = get_mode_size_bytes(tv->mode);
1629 for (bits = 0, i = l - 1; i >= 0; --i) {
1630 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1632 /* check for more than one bit in these */
1644 * Returns non-zero if the mantissa of a floating point IEEE-754
1645 * tarval is zero (i.e. 1.0Exxx)
1647 int tarval_ieee754_zero_mantissa(tarval *tv) {
1648 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1649 return fc_zero_mantissa(tv->value);
1652 /* Returns the exponent of a floating point IEEE-754 tarval. */
1653 int tarval_ieee754_get_exponent(tarval *tv) {
1654 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1655 return fc_get_exponent(tv->value);
1658 /* Set the immediate precision for IEEE-754 results. */
1659 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1660 return fc_set_immediate_precision(bits);
1663 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1664 unsigned tarval_ieee754_get_exact(void) {
1665 return fc_is_exact();
1668 /* check if its the a floating point NaN */
1669 int tarval_is_NaN(tarval *tv) {
1670 if (! mode_is_float(tv->mode))
1672 return fc_is_nan(tv->value);
1675 /* check if its the a floating point +inf */
1676 int tarval_is_plus_inf(tarval *tv) {
1677 if (! mode_is_float(tv->mode))
1679 return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1682 /* check if its the a floating point -inf */
1683 int tarval_is_minus_inf(tarval *tv) {
1684 if (! mode_is_float(tv->mode))
1686 return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1689 /* check if the tarval represents a finite value */
1690 int tarval_is_finite(tarval *tv) {
1691 if (mode_is_float(tv->mode))
1692 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1697 * Sets the overflow mode for integer operations.
1699 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1700 int_overflow_mode = ov_mode;
1703 /* Get the overflow mode for integer operations. */
1704 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1705 return int_overflow_mode;
1708 /* Enable/Disable floating point constant folding. */
1709 int tarval_enable_fp_ops(int enable) {
1710 int old = !no_float;
1717 * default mode_info for output as HEX
1719 static const tarval_mode_info hex_output = {
1726 * Initialization of the tarval module: called before init_mode()
1728 void init_tarval_1(long null_value) {
1729 _null_value = null_value;
1731 /* initialize the sets holding the tarvals with a comparison function and
1732 * an initial size, which is the expected number of constants */
1733 tarvals = new_set(memcmp, N_CONSTANTS);
1734 values = new_set(memcmp, N_CONSTANTS);
1735 /* init strcalc with precision of 68 to support floating point values with 64
1736 * bit mantissa (needs extra bits for rounding and overflow) */
1742 * Initialization of the tarval module: called after init_mode()
1744 void init_tarval_2(void) {
1745 tarval_bad->kind = k_tarval;
1746 tarval_bad->mode = mode_BAD;
1747 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1749 tarval_undefined->kind = k_tarval;
1750 tarval_undefined->mode = mode_ANY;
1751 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1753 tarval_b_true->kind = k_tarval;
1754 tarval_b_true->mode = mode_b;
1755 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1757 tarval_b_false->kind = k_tarval;
1758 tarval_b_false->mode = mode_b;
1759 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1762 * assign output modes that are compatible with the
1763 * old implementation: Hex output
1765 set_tarval_mode_output_option(mode_Bs, &hex_output);
1766 set_tarval_mode_output_option(mode_Bu, &hex_output);
1767 set_tarval_mode_output_option(mode_Hs, &hex_output);
1768 set_tarval_mode_output_option(mode_Hu, &hex_output);
1769 set_tarval_mode_output_option(mode_Is, &hex_output);
1770 set_tarval_mode_output_option(mode_Iu, &hex_output);
1771 set_tarval_mode_output_option(mode_Ls, &hex_output);
1772 set_tarval_mode_output_option(mode_Lu, &hex_output);
1773 set_tarval_mode_output_option(mode_P, &hex_output);
1776 /* free all memory occupied by tarval. */
1777 void finish_tarval(void) {
1780 del_set(tarvals); tarvals = NULL;
1781 del_set(values); values = NULL;
1784 /****************************************************************************
1786 ****************************************************************************/