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;
106 /****************************************************************************
108 ****************************************************************************/
110 static int hash_val(const void *value, unsigned int length);
111 static int hash_tv(tarval *tv);
112 static void _fail_verify(tarval *tv, const char* file, int line)
114 /* print a memory image of the tarval and throw an assertion */
116 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
118 printf("%s:%d: Invalid tarval (null)", file, line);
122 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
125 INLINE static void tarval_verify(tarval *tv)
131 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
132 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
134 if (!FIND_TARVAL(tv)) fail_verify(tv);
135 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
141 /** Hash a tarval. */
142 static int hash_tv(tarval *tv) {
143 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
146 /** Hash a value. Treat it as a byte array. */
147 static int hash_val(const void *value, unsigned int length) {
149 unsigned int hash = 0;
151 /* scramble the byte - array */
152 for (i = 0; i < length; ++i) {
153 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
154 hash += (hash << 11) ^ (hash >> 17);
160 /** finds tarval with value/mode or creates new tarval */
161 static tarval *get_tarval(const void *value, int length, ir_mode *mode) {
168 /* if there already is such a value, it is returned, else value
169 * is copied into the set */
170 tv.value = INSERT_VALUE(value, length);
174 /* if there is such a tarval, it is returned, else tv is copied
176 return (tarval *)INSERT_TARVAL(&tv);
182 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
184 switch (get_mode_sort(mode)) {
185 case irms_int_number:
186 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
187 switch (GET_OVERFLOW_MODE()) {
188 case TV_OVERFLOW_SATURATE:
189 return get_mode_max(mode);
190 case TV_OVERFLOW_WRAP:
192 char *temp = alloca(sc_get_buffer_length());
193 sc_val_from_ulong(-1, temp);
194 sc_and(temp, value, temp);
195 /* the sc_ module expects that all bits are set ... */
196 sign_extend(temp, mode);
197 return get_tarval(temp, length, mode);
199 case TV_OVERFLOW_BAD:
202 return get_tarval(value, length, mode);
205 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
206 switch (GET_OVERFLOW_MODE()) {
207 case TV_OVERFLOW_SATURATE:
208 return get_mode_min(mode);
209 case TV_OVERFLOW_WRAP: {
210 char *temp = alloca(sc_get_buffer_length());
211 sc_val_from_ulong(-1, temp);
212 sc_and(temp, value, temp);
213 return get_tarval(temp, length, mode);
215 case TV_OVERFLOW_BAD:
218 return get_tarval(value, length, mode);
223 case irms_float_number:
224 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
225 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
228 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
229 return get_mode_null(mode);
236 return get_tarval(value, length, mode);
240 * public variables declared in tv.h
242 static tarval reserved_tv[4];
244 tarval *tarval_bad = &reserved_tv[0];
245 tarval *tarval_undefined = &reserved_tv[1];
246 tarval *tarval_b_false = &reserved_tv[2];
247 tarval *tarval_b_true = &reserved_tv[3];
250 * public functions declared in tv.h
254 * Constructors =============================================================
256 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
262 switch (get_mode_sort(mode)) {
263 case irms_control_flow:
269 case irms_internal_boolean:
270 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
271 if (strcasecmp(str, "true")) return tarval_b_true;
272 else if (strcasecmp(str, "false")) return tarval_b_true;
274 /* XXX This is C semantics */
275 return atoi(str) ? tarval_b_true : tarval_b_false;
277 case irms_float_number:
278 switch(get_mode_size_bits(mode)) {
280 fc_val_from_str(str, len, 8, 23, NULL);
283 fc_val_from_str(str, len, 11, 52, NULL);
286 fc_val_from_str(str, len, 15, 64, NULL);
289 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
292 /* same as integer modes */
293 case irms_int_number:
294 sc_val_from_str(str, len, NULL, mode);
295 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
298 assert(0); /* can't be reached, can it? */
303 * helper function, create a tarval from long
305 tarval *new_tarval_from_long(long l, ir_mode *mode) {
308 switch(get_mode_sort(mode)) {
309 case irms_internal_boolean:
310 /* XXX C semantics ! */
311 return l ? tarval_b_true : tarval_b_false ;
314 /* same as integer modes */
315 case irms_int_number:
316 sc_val_from_long(l, NULL);
317 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
319 case irms_float_number:
320 return new_tarval_from_double((long double)l, mode);
323 assert(0 && "unsupported mode sort");
328 /* returns non-zero if can be converted to long */
329 int tarval_is_long(tarval *tv) {
330 if (!mode_is_int(tv->mode)) return 0;
332 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
333 /* the value might be too big to fit in a long */
334 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
335 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
336 /* really doesn't fit */
343 /* this might overflow the machine's long, so use only with small values */
344 long get_tarval_long(tarval* tv) {
345 assert(tarval_is_long(tv) && "tarval too big to fit in long");
347 return sc_val_to_long(tv->value);
350 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
351 assert(mode && (get_mode_sort(mode) == irms_float_number));
353 switch (get_mode_size_bits(mode)) {
355 fc_val_from_float(d, 8, 23, NULL);
358 fc_val_from_float(d, 11, 52, NULL);
361 fc_val_from_float(d, 15, 64, NULL);
364 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
367 /* returns non-zero if can be converted to double */
368 int tarval_is_double(tarval *tv) {
371 return (get_mode_sort(tv->mode) == irms_float_number);
374 long double get_tarval_double(tarval *tv) {
375 assert(tarval_is_double(tv));
377 return fc_val_to_float(tv->value);
382 * Access routines for tarval fields ========================================
385 /* get the mode of the tarval */
386 ir_mode *(get_tarval_mode)(const tarval *tv) {
387 return _get_tarval_mode(tv);
391 * Special value query functions ============================================
393 * These functions calculate and return a tarval representing the requested
395 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
396 * functions, but these are stored on initialization of the irmode module and
397 * therefore the irmode functions should be prefered to the functions below.
400 tarval *(get_tarval_bad)(void) {
401 return _get_tarval_bad();
404 tarval *(get_tarval_undefined)(void) {
405 return _get_tarval_undefined();
408 tarval *(get_tarval_b_false)(void) {
409 return _get_tarval_b_false();
412 tarval *(get_tarval_b_true)(void) {
413 return _get_tarval_b_true();
416 tarval *get_tarval_max(ir_mode *mode) {
419 if (get_mode_n_vector_elems(mode) > 1) {
420 /* vector arithmetic not implemented yet */
424 switch(get_mode_sort(mode)) {
426 case irms_control_flow:
432 case irms_internal_boolean:
433 return tarval_b_true;
435 case irms_float_number:
436 switch(get_mode_size_bits(mode)) {
438 fc_get_max(8, 23, NULL);
441 fc_get_max(11, 52, NULL);
444 fc_get_max(15, 64, NULL);
447 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
449 case irms_int_number:
450 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
451 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
456 tarval *get_tarval_min(ir_mode *mode) {
459 if (get_mode_n_vector_elems(mode) > 1) {
460 /* vector arithmetic not implemented yet */
464 switch(get_mode_sort(mode)) {
466 case irms_control_flow:
472 case irms_internal_boolean:
473 return tarval_b_false;
475 case irms_float_number:
476 switch(get_mode_size_bits(mode)) {
478 fc_get_min(8, 23, NULL);
481 fc_get_min(11, 52, NULL);
484 fc_get_min(15, 64, NULL);
487 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
489 case irms_int_number:
490 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
491 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
496 /** The bit pattern for the pointer NULL */
497 static long _null_value;
499 tarval *get_tarval_null(ir_mode *mode) {
502 if (get_mode_n_vector_elems(mode) > 1) {
503 /* vector arithmetic not implemented yet */
507 switch(get_mode_sort(mode)) {
508 case irms_control_flow:
511 case irms_internal_boolean:
515 case irms_float_number:
516 return new_tarval_from_double(0.0, mode);
518 case irms_int_number:
519 return new_tarval_from_long(0l, mode);
522 return new_tarval_from_long(_null_value, mode);
527 tarval *get_tarval_one(ir_mode *mode) {
530 if (get_mode_n_vector_elems(mode) > 1) {
531 /* vector arithmetic not implemented yet */
535 switch(get_mode_sort(mode)) {
536 case irms_control_flow:
539 case irms_internal_boolean:
544 case irms_float_number:
545 return new_tarval_from_double(1.0, mode);
547 case irms_int_number:
548 return new_tarval_from_long(1l, mode);
554 tarval *get_tarval_minus_one(ir_mode *mode) {
557 if (get_mode_n_vector_elems(mode) > 1) {
558 /* vector arithmetic not implemented yet */
562 switch(get_mode_sort(mode)) {
563 case irms_control_flow:
566 case irms_internal_boolean:
571 case irms_float_number:
572 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
574 case irms_int_number:
575 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
580 tarval *get_tarval_nan(ir_mode *mode) {
583 if (get_mode_n_vector_elems(mode) > 1) {
584 /* vector arithmetic not implemented yet */
588 if (get_mode_sort(mode) == irms_float_number) {
589 switch(get_mode_size_bits(mode)) {
591 fc_get_qnan(8, 23, NULL);
594 fc_get_qnan(11, 52, NULL);
597 fc_get_qnan(15, 64, NULL);
600 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
602 assert(0 && "tarval is not floating point");
607 tarval *get_tarval_plus_inf(ir_mode *mode) {
610 if (get_mode_n_vector_elems(mode) > 1) {
611 /* vector arithmetic not implemented yet */
615 if (get_mode_sort(mode) == irms_float_number) {
616 switch(get_mode_size_bits(mode)) {
618 fc_get_plusinf(8, 23, NULL);
621 fc_get_plusinf(11, 52, NULL);
624 fc_get_plusinf(15, 64, NULL);
627 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
629 assert(0 && "tarval is not floating point");
634 tarval *get_tarval_minus_inf(ir_mode *mode) {
637 if (get_mode_n_vector_elems(mode) > 1) {
638 /* vector arithmetic not implemented yet */
642 if (get_mode_sort(mode) == irms_float_number) {
643 switch(get_mode_size_bits(mode)) {
645 fc_get_minusinf(8, 23, NULL);
648 fc_get_minusinf(11, 52, NULL);
651 fc_get_minusinf(15, 64, NULL);
654 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
656 assert(0 && "tarval is not floating point");
662 * Arithmethic operations on tarvals ========================================
666 * test if negative number, 1 means 'yes'
668 int tarval_is_negative(tarval *a) {
671 if (get_mode_n_vector_elems(a->mode) > 1) {
672 /* vector arithmetic not implemented yet */
673 assert(0 && "tarval_is_negative is not allowed for vector modes");
677 switch (get_mode_sort(a->mode)) {
678 case irms_int_number:
679 if (!mode_is_signed(a->mode)) return 0;
681 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
683 case irms_float_number:
684 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
687 assert(0 && "not implemented");
693 * test if null, 1 means 'yes'
695 int tarval_is_null(tarval *a) {
696 ir_mode *m = get_tarval_mode(a);
698 return a == get_tarval_null(m);
702 * test if one, 1 means 'yes'
704 int tarval_is_one(tarval *a) {
705 ir_mode *m = get_tarval_mode(a);
707 return a == get_tarval_one(m);
713 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
717 if (a == tarval_bad || b == tarval_bad) {
718 assert(0 && "Comparison with tarval_bad");
722 if (a == tarval_undefined || b == tarval_undefined)
725 if (a->mode != b->mode)
728 if (get_mode_n_vector_elems(a->mode) > 1) {
729 /* vector arithmetic not implemented yet */
730 assert(0 && "cmp not implemented for vector modes");
733 /* Here the two tarvals are unequal and of the same mode */
734 switch (get_mode_sort(a->mode)) {
735 case irms_control_flow:
743 case irms_float_number:
747 * BEWARE: we cannot compare a == b here, because
748 * a NaN is always Unordered to any other value, even to itself!
750 switch (fc_comp(a->value, b->value)) {
751 case -1: return pn_Cmp_Lt;
752 case 0: return pn_Cmp_Eq;
753 case 1: return pn_Cmp_Gt;
754 case 2: return pn_Cmp_Uo;
755 default: return pn_Cmp_False;
757 case irms_int_number:
760 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
762 case irms_internal_boolean:
765 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
771 * convert to other mode
773 tarval *tarval_convert_to(tarval *src, ir_mode *m) {
779 if (src->mode == m) return src;
781 if (get_mode_n_vector_elems(src->mode) > 1) {
782 /* vector arithmetic not implemented yet */
786 switch (get_mode_sort(src->mode)) {
787 case irms_control_flow:
792 /* cast float to something */
793 case irms_float_number:
794 switch (get_mode_sort(m)) {
795 case irms_float_number:
796 switch (get_mode_size_bits(m)) {
798 fc_cast(src->value, 8, 23, NULL);
801 fc_cast(src->value, 11, 52, NULL);
804 fc_cast(src->value, 15, 64, NULL);
809 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
811 case irms_int_number:
812 switch (GET_FLOAT_TO_INT_MODE()) {
814 fc_int(src->value, NULL);
817 fc_rnd(src->value, NULL);
823 /* FIXME: floating point unit can't produce a value in integer
825 * an intermediate representation is needed here first. */
826 /* return get_tarval(); */
830 /* the rest can't be converted */
835 /* cast int/characters to something */
836 case irms_int_number:
837 switch (get_mode_sort(m)) {
838 case irms_int_number:
839 buffer = alloca(sc_get_buffer_length());
840 memcpy(buffer, src->value, sc_get_buffer_length());
841 sign_extend(buffer, m);
842 return get_tarval_overflow(buffer, src->length, m);
844 case irms_internal_boolean:
845 /* XXX C semantics */
846 if (src == get_mode_null(src->mode)) return tarval_b_false;
847 else return tarval_b_true;
849 case irms_float_number:
850 /* XXX floating point unit does not understand internal integer
851 * representation, convert to string first, then create float from
853 buffer = alloca(100);
854 /* decimal string representation because hexadecimal output is
855 * interpreted unsigned by fc_val_from_str, so this is a HACK */
856 snprintf(buffer, 100, "%s",
857 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
858 buffer[100 - 1] = '\0';
859 switch (get_mode_size_bits(m)) {
861 fc_val_from_str(buffer, 0, 8, 23, NULL);
864 fc_val_from_str(buffer, 0, 11, 52, NULL);
867 fc_val_from_str(buffer, 0, 15, 64, NULL);
870 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
873 /* allow 0 to be casted */
874 if (src == get_mode_null(src->mode))
875 return get_mode_null(m);
883 case irms_internal_boolean:
884 switch (get_mode_sort(m)) {
885 case irms_int_number:
886 if (src == tarval_b_true) return get_mode_one(m);
887 else return get_mode_null(m);
895 switch(get_mode_sort(m)) {
896 case irms_int_number:
897 buffer = alloca(sc_get_buffer_length());
898 memcpy(buffer, src->value, sc_get_buffer_length());
899 sign_extend(buffer, src->mode);
900 return get_tarval_overflow(buffer, src->length, m);
914 tarval *tarval_not(tarval *a) {
919 /* works for vector mode without changes */
921 switch (get_mode_sort(a->mode)) {
922 case irms_int_number:
923 buffer = alloca(sc_get_buffer_length());
924 sc_not(a->value, buffer);
925 return get_tarval(buffer, a->length, a->mode);
927 case irms_internal_boolean:
928 if (a == tarval_b_true)
929 return tarval_b_false;
930 if (a == tarval_b_false)
931 return tarval_b_true;
935 assert(0 && "bitwise negation is only allowed for integer and boolean");
941 * arithmetic negation
943 tarval *tarval_neg(tarval *a) {
947 assert(mode_is_num(a->mode)); /* negation only for numerical values */
949 /* note: negation is allowed even for unsigned modes. */
951 if (get_mode_n_vector_elems(a->mode) > 1) {
952 /* vector arithmetic not implemented yet */
956 switch (get_mode_sort(a->mode)) {
957 case irms_int_number:
958 buffer = alloca(sc_get_buffer_length());
959 sc_neg(a->value, buffer);
960 return get_tarval_overflow(buffer, a->length, a->mode);
962 case irms_float_number:
966 fc_neg(a->value, NULL);
967 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
977 tarval *tarval_add(tarval *a, tarval *b) {
982 assert(a->mode == b->mode);
984 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
985 /* vector arithmetic not implemented yet */
989 switch (get_mode_sort(a->mode)) {
990 case irms_int_number:
991 /* modes of a,b are equal, so result has mode of a as this might be the character */
992 buffer = alloca(sc_get_buffer_length());
993 sc_add(a->value, b->value, buffer);
994 return get_tarval_overflow(buffer, a->length, a->mode);
996 case irms_float_number:
1000 fc_add(a->value, b->value, NULL);
1001 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1011 tarval *tarval_sub(tarval *a, tarval *b) {
1016 assert(a->mode == b->mode);
1018 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1019 /* vector arithmetic not implemented yet */
1022 switch (get_mode_sort(a->mode)) {
1023 case irms_int_number:
1024 /* modes of a,b are equal, so result has mode of a as this might be the character */
1025 buffer = alloca(sc_get_buffer_length());
1026 sc_sub(a->value, b->value, buffer);
1027 return get_tarval_overflow(buffer, a->length, a->mode);
1029 case irms_float_number:
1033 fc_sub(a->value, b->value, NULL);
1034 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1044 tarval *tarval_mul(tarval *a, tarval *b) {
1049 assert(a->mode == b->mode);
1051 if (get_mode_n_vector_elems(a->mode) > 1) {
1052 /* vector arithmetic not implemented yet */
1056 switch (get_mode_sort(a->mode)) {
1057 case irms_int_number:
1058 /* modes of a,b are equal */
1059 buffer = alloca(sc_get_buffer_length());
1060 sc_mul(a->value, b->value, buffer);
1061 return get_tarval_overflow(buffer, a->length, a->mode);
1063 case irms_float_number:
1067 fc_mul(a->value, b->value, NULL);
1068 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1076 * floating point division
1078 tarval *tarval_quo(tarval *a, tarval *b) {
1081 assert((a->mode == b->mode) && mode_is_float(a->mode));
1086 if (get_mode_n_vector_elems(a->mode) > 1) {
1087 /* vector arithmetic not implemented yet */
1091 fc_div(a->value, b->value, NULL);
1092 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1097 * overflow is impossible, but look out for division by zero
1099 tarval *tarval_div(tarval *a, tarval *b) {
1102 assert((a->mode == b->mode) && mode_is_int(a->mode));
1104 if (get_mode_n_vector_elems(a->mode) > 1) {
1105 /* vector arithmetic not implemented yet */
1110 if (b == get_mode_null(b->mode)) return tarval_bad;
1111 /* modes of a,b are equal */
1112 sc_div(a->value, b->value, NULL);
1113 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1118 * overflow is impossible, but look out for division by zero
1120 tarval *tarval_mod(tarval *a, tarval *b) {
1123 assert((a->mode == b->mode) && mode_is_int(a->mode));
1125 if (get_mode_n_vector_elems(a->mode) > 1) {
1126 /* vector arithmetic not implemented yet */
1131 if (b == get_mode_null(b->mode)) return tarval_bad;
1132 /* modes of a,b are equal */
1133 sc_mod(a->value, b->value, NULL);
1134 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1140 tarval *tarval_abs(tarval *a) {
1144 assert(mode_is_num(a->mode));
1146 if (get_mode_n_vector_elems(a->mode) > 1) {
1147 /* vector arithmetic not implemented yet */
1151 switch (get_mode_sort(a->mode)) {
1152 case irms_int_number:
1153 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1154 buffer = alloca(sc_get_buffer_length());
1155 sc_neg(a->value, buffer);
1156 return get_tarval_overflow(buffer, a->length, a->mode);
1160 case irms_float_number:
1164 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1165 fc_neg(a->value, NULL);
1166 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1179 tarval *tarval_and(tarval *a, tarval *b) {
1182 assert(a->mode == b->mode);
1184 /* works even for vector modes */
1186 switch(get_mode_sort(a->mode)) {
1187 case irms_internal_boolean:
1188 return (a == tarval_b_false) ? a : b;
1190 case irms_int_number:
1191 sc_and(a->value, b->value, NULL);
1192 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1195 assert(0 && "operation not defined on mode");
1203 tarval *tarval_or (tarval *a, tarval *b) {
1206 assert(a->mode == b->mode);
1208 /* works even for vector modes */
1210 switch (get_mode_sort(a->mode)) {
1211 case irms_internal_boolean:
1212 return (a == tarval_b_true) ? a : b;
1214 case irms_int_number:
1215 sc_or(a->value, b->value, NULL);
1216 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1219 assert(0 && "operation not defined on mode");
1225 * bitwise exclusive or (xor)
1227 tarval *tarval_eor(tarval *a, tarval *b) {
1230 assert((a->mode == b->mode));
1232 /* works even for vector modes */
1234 switch (get_mode_sort(a->mode)) {
1235 case irms_internal_boolean:
1236 return (a == b)? tarval_b_false : tarval_b_true;
1238 case irms_int_number:
1239 sc_xor(a->value, b->value, NULL);
1240 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1243 assert(0 && "operation not defined on mode");
1249 * bitwise left shift
1251 tarval *tarval_shl(tarval *a, tarval *b) {
1252 char *temp_val = NULL;
1256 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1258 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1259 /* vector arithmetic not implemented yet */
1263 if (get_mode_modulo_shift(a->mode) != 0) {
1264 temp_val = alloca(sc_get_buffer_length());
1266 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1267 sc_mod(b->value, temp_val, temp_val);
1269 temp_val = (char*)b->value;
1271 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1272 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1276 * bitwise unsigned right shift
1278 tarval *tarval_shr(tarval *a, tarval *b) {
1279 char *temp_val = NULL;
1283 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1285 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1286 /* vector arithmetic not implemented yet */
1290 if (get_mode_modulo_shift(a->mode) != 0) {
1291 temp_val = alloca(sc_get_buffer_length());
1293 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1294 sc_mod(b->value, temp_val, temp_val);
1296 temp_val = (char*)b->value;
1298 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1299 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1303 * bitwise signed right shift
1305 tarval *tarval_shrs(tarval *a, tarval *b) {
1306 char *temp_val = NULL;
1310 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1312 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1313 /* vector arithmetic not implemented yet */
1317 if (get_mode_modulo_shift(a->mode) != 0) {
1318 temp_val = alloca(sc_get_buffer_length());
1320 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1321 sc_mod(b->value, temp_val, temp_val);
1323 temp_val = (char*)b->value;
1325 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1326 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1332 tarval *tarval_rot(tarval *a, tarval *b) {
1333 char *temp_val = NULL;
1337 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1339 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1340 /* vector arithmetic not implemented yet */
1344 if (get_mode_modulo_shift(a->mode) != 0) {
1345 temp_val = alloca(sc_get_buffer_length());
1347 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1348 sc_mod(b->value, temp_val, temp_val);
1350 temp_val = (char*)b->value;
1352 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1353 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1357 * carry flag of the last operation
1359 int tarval_carry(void) {
1360 return sc_had_carry();
1366 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1367 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1371 const tarval_mode_info *mode_info;
1372 const char *prefix, *suffix;
1374 mode_info = tv->mode->tv_priv;
1376 mode_info = &default_info;
1377 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1378 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1380 switch (get_mode_sort(tv->mode)) {
1381 case irms_reference:
1382 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1384 case irms_int_number:
1385 switch (mode_info->mode_output) {
1388 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1392 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1398 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1401 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1403 case irms_float_number:
1404 switch (mode_info->mode_output) {
1406 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1409 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1414 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1418 case irms_internal_boolean:
1419 switch (mode_info->mode_output) {
1425 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1429 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1432 case irms_control_flow:
1434 case irms_auxiliary:
1435 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1442 * Output of tarvals to stdio.
1444 int tarval_printf(tarval *tv) {
1448 res = tarval_snprintf(buf, sizeof(buf), tv);
1449 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1454 char *get_tarval_bitpattern(tarval *tv) {
1456 int n = get_mode_size_bits(tv->mode);
1457 int bytes = (n + 7) / 8;
1458 char *res = xmalloc((n + 1) * sizeof(char));
1461 for(i = 0; i < bytes; i++) {
1462 byte = get_tarval_sub_bits(tv, i);
1463 for(j = 1; j < 256; j <<= 1)
1465 res[pos++] = j & byte ? '1' : '0';
1474 * access to the bitpattern
1476 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1477 switch (get_mode_arithmetic(tv->mode)) {
1478 case irma_twos_complement:
1479 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1481 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1483 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1488 * Specify the output options of one mode.
1490 * This functions stores the modinfo, so DO NOT DESTROY it.
1492 * Returns zero on success.
1494 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1497 mode->tv_priv = modeinfo;
1502 * Returns the output options of one mode.
1504 * This functions returns the mode info of a given mode.
1506 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1509 return mode->tv_priv;
1513 * Identifying tarvals values for algebraic simplifications.
1516 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1517 * - TV_CLASSIFY_ONE for multiplicative neutral,
1518 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1519 * - TV_CLASSIFY_OTHER else
1521 tarval_classification_t classify_tarval(tarval *tv) {
1522 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1524 if (tv == get_mode_null(tv->mode))
1525 return TV_CLASSIFY_NULL;
1526 else if (tv == get_mode_one(tv->mode))
1527 return TV_CLASSIFY_ONE;
1528 else if ((get_mode_sort(tv->mode) == irms_int_number)
1529 && (tv == new_tarval_from_long(-1, tv->mode)))
1530 return TV_CLASSIFY_ALL_ONE;
1532 return TV_CLASSIFY_OTHER;
1536 * Returns non-zero if a given (integer) tarval has only one single bit
1539 int is_single_bit_tarval(tarval *tv) {
1543 if (!tv || tv == tarval_bad) return 0;
1544 if (! mode_is_int(tv->mode)) return 0;
1546 l = get_mode_size_bytes(tv->mode);
1547 for (bits = 0, i = l - 1; i >= 0; --i) {
1548 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1550 /* check for more than one bit in these */
1562 * Sets the overflow mode for integer operations.
1564 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1565 int_overflow_mode = ov_mode;
1569 * Get the overflow mode for integer operations.
1571 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1572 return int_overflow_mode;
1576 * default mode_info for output as HEX
1578 static const tarval_mode_info hex_output = {
1585 * Initialization of the tarval module: called before init_mode()
1587 void init_tarval_1(long null_value) {
1588 _null_value = null_value;
1590 /* initialize the sets holding the tarvals with a comparison function and
1591 * an initial size, which is the expected number of constants */
1592 tarvals = new_set(memcmp, N_CONSTANTS);
1593 values = new_set(memcmp, N_CONSTANTS);
1594 /* init strcalc with precision of 68 to support floating point values with 64
1595 * bit mantissa (needs extra bits for rounding and overflow) */
1601 * Initialization of the tarval module: called after init_mode()
1603 void init_tarval_2(void) {
1604 tarval_bad->kind = k_tarval;
1605 tarval_bad->mode = mode_BAD;
1606 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1608 tarval_undefined->kind = k_tarval;
1609 tarval_undefined->mode = mode_ANY;
1610 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1612 tarval_b_true->kind = k_tarval;
1613 tarval_b_true->mode = mode_b;
1614 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1616 tarval_b_false->kind = k_tarval;
1617 tarval_b_false->mode = mode_b;
1618 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1621 * assign output modes that are compatible with the
1622 * old implementation: Hex output
1624 set_tarval_mode_output_option(mode_Bs, &hex_output);
1625 set_tarval_mode_output_option(mode_Bu, &hex_output);
1626 set_tarval_mode_output_option(mode_Hs, &hex_output);
1627 set_tarval_mode_output_option(mode_Hu, &hex_output);
1628 set_tarval_mode_output_option(mode_Is, &hex_output);
1629 set_tarval_mode_output_option(mode_Iu, &hex_output);
1630 set_tarval_mode_output_option(mode_Ls, &hex_output);
1631 set_tarval_mode_output_option(mode_Lu, &hex_output);
1632 set_tarval_mode_output_option(mode_P, &hex_output);
1635 /* free all memory occupied by tarval. */
1636 void finish_tarval(void) {
1639 del_set(tarvals); tarvals = NULL;
1640 del_set(values); values = NULL;
1643 /****************************************************************************
1645 ****************************************************************************/