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 * File name: ir/tv/tv.c
23 * Purpose: Representation of and static computations on target machine
25 * Author: Mathias Heil
29 * Copyright: (c) 2003-2007 Universität Karlsruhe
33 * Values are stored in a format depending upon chosen arithmetic
34 * module. Default uses strcalc and fltcalc.
38 /* This implementation assumes:
39 * - target has IEEE-754 floating-point arithmetic. */
47 #include <assert.h> /* assertions */
48 #include <stdlib.h> /* atoi() */
50 # include <string.h> /* nice things for strings */
53 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
60 #include "set.h" /* to store tarvals in */
61 #include "entity_t.h" /* needed to store pointers to entities */
63 #include "irnode.h" /* defines boolean return values (pnc_number)*/
68 #include "firm_common.h"
70 /** Size of hash tables. Should correspond to average number of distinct constant
72 #define N_CONSTANTS 2048
74 /* get the integer overflow mode */
75 #define GET_OVERFLOW_MODE() int_overflow_mode
77 /* unused, float to int doesn't work yet */
78 enum float_to_int_mode {
83 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
85 #define SWITCH_NOINFINITY 0
86 #define SWITCH_NODENORMALS 0
88 /****************************************************************************
89 * local definitions and macros
90 ****************************************************************************/
92 # define TARVAL_VERIFY(a) tarval_verify((a))
94 # define TARVAL_VERIFY(a) ((void)0)
97 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
98 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
100 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
101 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
103 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
105 /****************************************************************************
107 ****************************************************************************/
108 static struct set *tarvals = NULL; /* container for tarval structs */
109 static struct set *values = NULL; /* container for values */
110 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
112 /****************************************************************************
114 ****************************************************************************/
116 static int hash_val(const void *value, unsigned int length);
117 static int hash_tv(tarval *tv);
118 static void _fail_verify(tarval *tv, const char* file, int line)
120 /* print a memory image of the tarval and throw an assertion */
122 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
124 printf("%s:%d: Invalid tarval (null)", file, line);
128 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
131 INLINE static void tarval_verify(tarval *tv)
137 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
138 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
140 if (!FIND_TARVAL(tv)) fail_verify(tv);
141 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
147 /** Hash a tarval. */
148 static int hash_tv(tarval *tv) {
149 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
152 /** Hash a value. Treat it as a byte array. */
153 static int hash_val(const void *value, unsigned int length) {
155 unsigned int hash = 0;
157 /* scramble the byte - array */
158 for (i = 0; i < length; ++i) {
159 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
160 hash += (hash << 11) ^ (hash >> 17);
166 /** finds tarval with value/mode or creates new tarval */
167 static tarval *get_tarval(const void *value, int length, ir_mode *mode) {
174 /* if there already is such a value, it is returned, else value
175 * is copied into the set */
176 tv.value = INSERT_VALUE(value, 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)
190 switch (get_mode_sort(mode)) {
191 case irms_int_number:
192 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
193 switch (GET_OVERFLOW_MODE()) {
194 case TV_OVERFLOW_SATURATE:
195 return get_mode_max(mode);
196 case TV_OVERFLOW_WRAP:
198 char *temp = alloca(sc_get_buffer_length());
199 sc_val_from_ulong(-1, temp);
200 sc_and(temp, value, temp);
201 /* the sc_ module expects that all bits are set ... */
202 sign_extend(temp, mode);
203 return get_tarval(temp, length, mode);
205 case TV_OVERFLOW_BAD:
208 return get_tarval(value, length, mode);
211 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
212 switch (GET_OVERFLOW_MODE()) {
213 case TV_OVERFLOW_SATURATE:
214 return get_mode_min(mode);
215 case TV_OVERFLOW_WRAP: {
216 char *temp = alloca(sc_get_buffer_length());
217 sc_val_from_ulong(-1, temp);
218 sc_and(temp, value, temp);
219 return get_tarval(temp, length, mode);
221 case TV_OVERFLOW_BAD:
224 return get_tarval(value, length, mode);
229 case irms_float_number:
230 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
231 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
234 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
235 return get_mode_null(mode);
242 return get_tarval(value, length, mode);
246 * public variables declared in tv.h
248 static tarval reserved_tv[4];
250 tarval *tarval_bad = &reserved_tv[0];
251 tarval *tarval_undefined = &reserved_tv[1];
252 tarval *tarval_b_false = &reserved_tv[2];
253 tarval *tarval_b_true = &reserved_tv[3];
256 * public functions declared in tv.h
260 * Constructors =============================================================
262 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
268 switch (get_mode_sort(mode)) {
269 case irms_control_flow:
275 case irms_internal_boolean:
276 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
277 if (strcasecmp(str, "true")) return tarval_b_true;
278 else if (strcasecmp(str, "false")) return tarval_b_true;
280 /* XXX This is C semantics */
281 return atoi(str) ? tarval_b_true : tarval_b_false;
283 case irms_float_number:
284 switch(get_mode_size_bits(mode)) {
286 fc_val_from_str(str, len, 8, 23, NULL);
289 fc_val_from_str(str, len, 11, 52, NULL);
292 fc_val_from_str(str, len, 15, 64, NULL);
295 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
298 /* same as integer modes */
299 case irms_int_number:
301 sc_val_from_str(str, len, NULL, mode);
302 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
305 assert(0); /* can't be reached, can it? */
310 * helper function, create a tarval from long
312 tarval *new_tarval_from_long(long l, ir_mode *mode) {
315 switch(get_mode_sort(mode)) {
316 case irms_internal_boolean:
317 /* XXX C semantics ! */
318 return l ? tarval_b_true : tarval_b_false ;
321 /* same as integer modes */
322 case irms_int_number:
324 sc_val_from_long(l, NULL);
325 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
327 case irms_float_number:
328 return new_tarval_from_double((long double)l, mode);
331 assert(0 && "unsupported mode sort");
336 /* returns non-zero if can be converted to long */
337 int tarval_is_long(tarval *tv) {
338 mode_sort sort = get_mode_sort(tv->mode);
340 if (sort != irms_int_number && sort != irms_character) return 0;
342 if (get_mode_size_bits(tv->mode) > (sizeof(long) << 3)) {
343 /* the value might be too big to fit in a long */
344 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
345 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
346 /* really doesn't fit */
353 /* this might overflow the machine's long, so use only with small values */
354 long get_tarval_long(tarval* tv) {
355 assert(tarval_is_long(tv) && "tarval too big to fit in long");
357 return sc_val_to_long(tv->value);
360 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
361 assert(mode && (get_mode_sort(mode) == irms_float_number));
363 switch (get_mode_size_bits(mode)) {
365 fc_val_from_float(d, 8, 23, NULL);
368 fc_val_from_float(d, 11, 52, NULL);
371 fc_val_from_float(d, 15, 64, NULL);
374 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
377 /* returns non-zero if can be converted to double */
378 int tarval_is_double(tarval *tv) {
381 return (get_mode_sort(tv->mode) == irms_float_number);
384 long double get_tarval_double(tarval *tv) {
385 assert(tarval_is_double(tv));
387 return fc_val_to_float(tv->value);
392 * Access routines for tarval fields ========================================
395 /* get the mode of the tarval */
396 ir_mode *(get_tarval_mode)(const tarval *tv) {
397 return _get_tarval_mode(tv);
401 * Special value query functions ============================================
403 * These functions calculate and return a tarval representing the requested
405 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
406 * functions, but these are stored on initialization of the irmode module and
407 * therefore the irmode functions should be prefered to the functions below.
410 tarval *(get_tarval_bad)(void) {
411 return _get_tarval_bad();
414 tarval *(get_tarval_undefined)(void) {
415 return _get_tarval_undefined();
418 tarval *(get_tarval_b_false)(void) {
419 return _get_tarval_b_false();
422 tarval *(get_tarval_b_true)(void) {
423 return _get_tarval_b_true();
426 tarval *get_tarval_max(ir_mode *mode) {
429 if (get_mode_n_vector_elems(mode) > 1) {
430 /* vector arithmetic not implemented yet */
434 switch(get_mode_sort(mode)) {
436 case irms_control_flow:
442 case irms_internal_boolean:
443 return tarval_b_true;
445 case irms_float_number:
446 switch(get_mode_size_bits(mode)) {
448 fc_get_max(8, 23, NULL);
451 fc_get_max(11, 52, NULL);
454 fc_get_max(15, 64, NULL);
457 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
459 case irms_int_number:
461 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
462 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
467 tarval *get_tarval_min(ir_mode *mode) {
470 if (get_mode_n_vector_elems(mode) > 1) {
471 /* vector arithmetic not implemented yet */
475 switch(get_mode_sort(mode)) {
477 case irms_control_flow:
483 case irms_internal_boolean:
484 return tarval_b_false;
486 case irms_float_number:
487 switch(get_mode_size_bits(mode)) {
489 fc_get_min(8, 23, NULL);
492 fc_get_min(11, 52, NULL);
495 fc_get_min(15, 64, NULL);
498 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
500 case irms_int_number:
502 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
503 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
508 /** The bit pattern for the pointer NULL */
509 static long _null_value;
511 tarval *get_tarval_null(ir_mode *mode) {
514 if (get_mode_n_vector_elems(mode) > 1) {
515 /* vector arithmetic not implemented yet */
519 switch(get_mode_sort(mode)) {
520 case irms_control_flow:
523 case irms_internal_boolean:
527 case irms_float_number:
528 return new_tarval_from_double(0.0, mode);
530 case irms_int_number:
532 return new_tarval_from_long(0l, mode);
535 return new_tarval_from_long(_null_value, mode);
540 tarval *get_tarval_one(ir_mode *mode) {
543 if (get_mode_n_vector_elems(mode) > 1) {
544 /* vector arithmetic not implemented yet */
548 switch(get_mode_sort(mode)) {
549 case irms_control_flow:
552 case irms_internal_boolean:
557 case irms_float_number:
558 return new_tarval_from_double(1.0, mode);
560 case irms_int_number:
562 return new_tarval_from_long(1l, mode);
568 tarval *get_tarval_minus_one(ir_mode *mode) {
571 if (get_mode_n_vector_elems(mode) > 1) {
572 /* vector arithmetic not implemented yet */
576 switch(get_mode_sort(mode)) {
577 case irms_control_flow:
580 case irms_internal_boolean:
585 case irms_float_number:
586 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
588 case irms_int_number:
590 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
595 tarval *get_tarval_nan(ir_mode *mode) {
598 if (get_mode_n_vector_elems(mode) > 1) {
599 /* vector arithmetic not implemented yet */
603 if (get_mode_sort(mode) == irms_float_number) {
604 switch(get_mode_size_bits(mode)) {
606 fc_get_qnan(8, 23, NULL);
609 fc_get_qnan(11, 52, NULL);
612 fc_get_qnan(15, 64, NULL);
615 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
617 assert(0 && "tarval is not floating point");
622 tarval *get_tarval_plus_inf(ir_mode *mode) {
625 if (get_mode_n_vector_elems(mode) > 1) {
626 /* vector arithmetic not implemented yet */
630 if (get_mode_sort(mode) == irms_float_number) {
631 switch(get_mode_size_bits(mode)) {
633 fc_get_plusinf(8, 23, NULL);
636 fc_get_plusinf(11, 52, NULL);
639 fc_get_plusinf(15, 64, NULL);
642 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
644 assert(0 && "tarval is not floating point");
649 tarval *get_tarval_minus_inf(ir_mode *mode) {
652 if (get_mode_n_vector_elems(mode) > 1) {
653 /* vector arithmetic not implemented yet */
657 if (get_mode_sort(mode) == irms_float_number) {
658 switch(get_mode_size_bits(mode)) {
660 fc_get_minusinf(8, 23, NULL);
663 fc_get_minusinf(11, 52, NULL);
666 fc_get_minusinf(15, 64, NULL);
669 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
671 assert(0 && "tarval is not floating point");
677 * Arithmethic operations on tarvals ========================================
681 * test if negative number, 1 means 'yes'
683 int tarval_is_negative(tarval *a) {
686 if (get_mode_n_vector_elems(a->mode) > 1) {
687 /* vector arithmetic not implemented yet */
688 assert(0 && "tarval_is_negative is not allowed for vector modes");
692 switch (get_mode_sort(a->mode)) {
693 case irms_int_number:
694 if (!mode_is_signed(a->mode)) return 0;
696 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
698 case irms_float_number:
699 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
702 assert(0 && "not implemented");
708 * test if null, 1 means 'yes'
710 int tarval_is_null(tarval *a) {
711 ir_mode *m = get_tarval_mode(a);
713 return a == get_tarval_null(m);
717 * test if one, 1 means 'yes'
719 int tarval_is_one(tarval *a) {
720 ir_mode *m = get_tarval_mode(a);
722 return a == get_tarval_one(m);
728 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
732 if (a == tarval_bad || b == tarval_bad) {
733 assert(0 && "Comparison with tarval_bad");
737 if (a == tarval_undefined || b == tarval_undefined)
740 if (a->mode != b->mode)
743 if (get_mode_n_vector_elems(a->mode) > 1) {
744 /* vector arithmetic not implemented yet */
745 assert(0 && "cmp not implemented for vector modes");
748 /* Here the two tarvals are unequal and of the same mode */
749 switch (get_mode_sort(a->mode)) {
750 case irms_control_flow:
758 case irms_float_number:
760 * BEWARE: we cannot compare a == b here, because
761 * a NaN is always Unordered to any other value, even to itself!
763 switch (fc_comp(a->value, b->value)) {
764 case -1: return pn_Cmp_Lt;
765 case 0: return pn_Cmp_Eq;
766 case 1: return pn_Cmp_Gt;
767 case 2: return pn_Cmp_Uo;
768 default: return pn_Cmp_False;
770 case irms_int_number:
774 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
776 case irms_internal_boolean:
779 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
785 * convert to other mode
787 tarval *tarval_convert_to(tarval *src, ir_mode *m) {
793 if (src->mode == m) return src;
795 if (get_mode_n_vector_elems(src->mode) > 1) {
796 /* vector arithmetic not implemented yet */
800 switch (get_mode_sort(src->mode)) {
801 case irms_control_flow:
806 /* cast float to something */
807 case irms_float_number:
808 switch (get_mode_sort(m)) {
809 case irms_float_number:
810 switch (get_mode_size_bits(m)) {
812 fc_cast(src->value, 8, 23, NULL);
815 fc_cast(src->value, 11, 52, NULL);
818 fc_cast(src->value, 15, 64, NULL);
823 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
825 case irms_int_number:
826 switch (GET_FLOAT_TO_INT_MODE()) {
828 fc_int(src->value, NULL);
831 fc_rnd(src->value, NULL);
837 /* FIXME: floating point unit can't produce a value in integer
839 * an intermediate representation is needed here first. */
840 /* return get_tarval(); */
844 /* the rest can't be converted */
849 /* cast int/characters to something */
850 case irms_int_number:
852 switch (get_mode_sort(m)) {
853 case irms_int_number:
855 buffer = alloca(sc_get_buffer_length());
856 memcpy(buffer, src->value, sc_get_buffer_length());
857 sign_extend(buffer, src->mode);
858 return get_tarval_overflow(buffer, src->length, m);
860 case irms_internal_boolean:
861 /* XXX C semantics */
862 if (src == get_mode_null(src->mode)) return tarval_b_false;
863 else return tarval_b_true;
865 case irms_float_number:
866 /* XXX floating point unit does not understand internal integer
867 * representation, convert to string first, then create float from
869 buffer = alloca(100);
870 /* decimal string representation because hexadecimal output is
871 * interpreted unsigned by fc_val_from_str, so this is a HACK */
872 snprintf(buffer, 100, "%s",
873 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
874 buffer[100 - 1] = '\0';
875 switch (get_mode_size_bits(m)) {
877 fc_val_from_str(buffer, 0, 8, 23, NULL);
880 fc_val_from_str(buffer, 0, 11, 52, NULL);
883 fc_val_from_str(buffer, 0, 15, 64, NULL);
886 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
889 /* allow 0 to be casted */
890 if (src == get_mode_null(src->mode))
891 return get_mode_null(m);
899 case irms_internal_boolean:
900 switch (get_mode_sort(m)) {
901 case irms_int_number:
902 if (src == tarval_b_true) return get_mode_one(m);
903 else return get_mode_null(m);
920 tarval *tarval_not(tarval *a) {
925 /* works for vector mode without changes */
927 switch (get_mode_sort(a->mode)) {
928 case irms_int_number:
929 buffer = alloca(sc_get_buffer_length());
930 sc_not(a->value, buffer);
931 return get_tarval(buffer, a->length, a->mode);
933 case irms_internal_boolean:
934 if (a == tarval_b_true)
935 return tarval_b_false;
936 if (a == tarval_b_false)
937 return tarval_b_true;
941 assert(0 && "bitwise negation is only allowed for integer and boolean");
947 * arithmetic negation
949 tarval *tarval_neg(tarval *a) {
953 assert(mode_is_num(a->mode)); /* negation only for numerical values */
955 /* note: negation is allowed even for unsigned modes. */
957 if (get_mode_n_vector_elems(a->mode) > 1) {
958 /* vector arithmetic not implemented yet */
962 switch (get_mode_sort(a->mode)) {
963 case irms_int_number:
964 buffer = alloca(sc_get_buffer_length());
965 sc_neg(a->value, buffer);
966 return get_tarval_overflow(buffer, a->length, a->mode);
968 case irms_float_number:
969 fc_neg(a->value, NULL);
970 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
980 tarval *tarval_add(tarval *a, tarval *b) {
985 assert(a->mode == b->mode);
987 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
988 /* vector arithmetic not implemented yet */
992 switch (get_mode_sort(a->mode)) {
994 case irms_int_number:
995 /* modes of a,b are equal, so result has mode of a as this might be the character */
996 buffer = alloca(sc_get_buffer_length());
997 sc_add(a->value, b->value, buffer);
998 return get_tarval_overflow(buffer, a->length, a->mode);
1000 case irms_float_number:
1001 fc_add(a->value, b->value, NULL);
1002 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1012 tarval *tarval_sub(tarval *a, tarval *b) {
1017 assert(a->mode == b->mode);
1019 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1020 /* vector arithmetic not implemented yet */
1023 switch (get_mode_sort(a->mode)) {
1024 case irms_character:
1025 case irms_int_number:
1026 /* modes of a,b are equal, so result has mode of a as this might be the character */
1027 buffer = alloca(sc_get_buffer_length());
1028 sc_sub(a->value, b->value, buffer);
1029 return get_tarval_overflow(buffer, a->length, a->mode);
1031 case irms_float_number:
1032 fc_sub(a->value, b->value, NULL);
1033 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1043 tarval *tarval_mul(tarval *a, tarval *b) {
1048 assert(a->mode == b->mode);
1050 if (get_mode_n_vector_elems(a->mode) > 1) {
1051 /* vector arithmetic not implemented yet */
1055 switch (get_mode_sort(a->mode)) {
1056 case irms_int_number:
1057 /* modes of a,b are equal */
1058 buffer = alloca(sc_get_buffer_length());
1059 sc_mul(a->value, b->value, buffer);
1060 return get_tarval_overflow(buffer, a->length, a->mode);
1062 case irms_float_number:
1063 fc_mul(a->value, b->value, NULL);
1064 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1072 * floating point division
1074 tarval *tarval_quo(tarval *a, tarval *b) {
1077 assert((a->mode == b->mode) && mode_is_float(a->mode));
1079 if (get_mode_n_vector_elems(a->mode) > 1) {
1080 /* vector arithmetic not implemented yet */
1084 fc_div(a->value, b->value, NULL);
1085 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1090 * overflow is impossible, but look out for division by zero
1092 tarval *tarval_div(tarval *a, tarval *b) {
1095 assert((a->mode == b->mode) && mode_is_int(a->mode));
1097 if (get_mode_n_vector_elems(a->mode) > 1) {
1098 /* vector arithmetic not implemented yet */
1103 if (b == get_mode_null(b->mode)) return tarval_bad;
1104 /* modes of a,b are equal */
1105 sc_div(a->value, b->value, NULL);
1106 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1111 * overflow is impossible, but look out for division by zero
1113 tarval *tarval_mod(tarval *a, tarval *b) {
1116 assert((a->mode == b->mode) && mode_is_int(a->mode));
1118 if (get_mode_n_vector_elems(a->mode) > 1) {
1119 /* vector arithmetic not implemented yet */
1124 if (b == get_mode_null(b->mode)) return tarval_bad;
1125 /* modes of a,b are equal */
1126 sc_mod(a->value, b->value, NULL);
1127 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1133 tarval *tarval_abs(tarval *a) {
1137 assert(mode_is_num(a->mode));
1139 if (get_mode_n_vector_elems(a->mode) > 1) {
1140 /* vector arithmetic not implemented yet */
1144 switch (get_mode_sort(a->mode)) {
1145 case irms_int_number:
1146 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1147 buffer = alloca(sc_get_buffer_length());
1148 sc_neg(a->value, buffer);
1149 return get_tarval_overflow(buffer, a->length, a->mode);
1153 case irms_float_number:
1154 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1155 fc_neg(a->value, NULL);
1156 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1169 tarval *tarval_and(tarval *a, tarval *b) {
1172 assert(a->mode == b->mode);
1174 /* works even for vector modes */
1176 switch(get_mode_sort(a->mode)) {
1177 case irms_internal_boolean:
1178 return (a == tarval_b_false) ? a : b;
1180 case irms_int_number:
1181 sc_and(a->value, b->value, NULL);
1182 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1185 assert(0 && "operation not defined on mode");
1193 tarval *tarval_or (tarval *a, tarval *b) {
1196 assert(a->mode == b->mode);
1198 /* works even for vector modes */
1200 switch (get_mode_sort(a->mode)) {
1201 case irms_internal_boolean:
1202 return (a == tarval_b_true) ? a : b;
1204 case irms_int_number:
1205 sc_or(a->value, b->value, NULL);
1206 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1209 assert(0 && "operation not defined on mode");
1215 * bitwise exclusive or (xor)
1217 tarval *tarval_eor(tarval *a, tarval *b) {
1220 assert((a->mode == b->mode));
1222 /* works even for vector modes */
1224 switch (get_mode_sort(a->mode)) {
1225 case irms_internal_boolean:
1226 return (a == b)? tarval_b_false : tarval_b_true;
1228 case irms_int_number:
1229 sc_xor(a->value, b->value, NULL);
1230 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1233 assert(0 && "operation not defined on mode");
1239 * bitwise left shift
1241 tarval *tarval_shl(tarval *a, tarval *b) {
1242 char *temp_val = NULL;
1246 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1248 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1249 /* vector arithmetic not implemented yet */
1253 if (get_mode_modulo_shift(a->mode) != 0) {
1254 temp_val = alloca(sc_get_buffer_length());
1256 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1257 sc_mod(b->value, temp_val, temp_val);
1259 temp_val = (char*)b->value;
1261 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1262 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1266 * bitwise unsigned right shift
1268 tarval *tarval_shr(tarval *a, tarval *b) {
1269 char *temp_val = NULL;
1273 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1275 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1276 /* vector arithmetic not implemented yet */
1280 if (get_mode_modulo_shift(a->mode) != 0) {
1281 temp_val = alloca(sc_get_buffer_length());
1283 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1284 sc_mod(b->value, temp_val, temp_val);
1286 temp_val = (char*)b->value;
1288 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1289 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1293 * bitwise signed right shift
1295 tarval *tarval_shrs(tarval *a, tarval *b) {
1296 char *temp_val = NULL;
1300 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1302 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1303 /* vector arithmetic not implemented yet */
1307 if (get_mode_modulo_shift(a->mode) != 0) {
1308 temp_val = alloca(sc_get_buffer_length());
1310 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1311 sc_mod(b->value, temp_val, temp_val);
1313 temp_val = (char*)b->value;
1315 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1316 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1322 tarval *tarval_rot(tarval *a, tarval *b) {
1323 char *temp_val = NULL;
1327 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1329 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1330 /* vector arithmetic not implemented yet */
1334 if (get_mode_modulo_shift(a->mode) != 0) {
1335 temp_val = alloca(sc_get_buffer_length());
1337 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1338 sc_mod(b->value, temp_val, temp_val);
1340 temp_val = (char*)b->value;
1342 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1343 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1347 * carry flag of the last operation
1349 int tarval_carry(void) {
1350 return sc_had_carry();
1356 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1357 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1361 const tarval_mode_info *mode_info;
1362 const char *prefix, *suffix;
1364 mode_info = tv->mode->tv_priv;
1366 mode_info = &default_info;
1367 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1368 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1370 switch (get_mode_sort(tv->mode)) {
1371 case irms_reference:
1372 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1374 case irms_int_number:
1375 case irms_character:
1376 switch (mode_info->mode_output) {
1379 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1383 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1389 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1392 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1394 case irms_float_number:
1395 switch (mode_info->mode_output) {
1397 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1400 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1405 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1409 case irms_internal_boolean:
1410 switch (mode_info->mode_output) {
1416 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1420 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1423 case irms_control_flow:
1425 case irms_auxiliary:
1426 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1433 * Output of tarvals to stdio.
1435 int tarval_printf(tarval *tv) {
1439 res = tarval_snprintf(buf, sizeof(buf), tv);
1440 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1445 char *get_tarval_bitpattern(tarval *tv) {
1447 int n = get_mode_size_bits(tv->mode);
1448 int bytes = (n + 7) / 8;
1449 char *res = xmalloc((n + 1) * sizeof(char));
1452 for(i = 0; i < bytes; i++) {
1453 byte = get_tarval_sub_bits(tv, i);
1454 for(j = 1; j < 256; j <<= 1)
1456 res[pos++] = j & byte ? '1' : '0';
1465 * access to the bitpattern
1467 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1468 switch (get_mode_sort(tv->mode)) {
1469 case irms_int_number:
1470 case irms_character:
1471 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1473 case irms_float_number:
1474 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1482 * Specify the output options of one mode.
1484 * This functions stores the modinfo, so DO NOT DESTROY it.
1486 * Returns zero on success.
1488 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1491 mode->tv_priv = modeinfo;
1496 * Returns the output options of one mode.
1498 * This functions returns the mode info of a given mode.
1500 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1503 return mode->tv_priv;
1507 * Identifying tarvals values for algebraic simplifications.
1510 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1511 * - TV_CLASSIFY_ONE for multiplicative neutral,
1512 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1513 * - TV_CLASSIFY_OTHER else
1515 tarval_classification_t classify_tarval(tarval *tv) {
1516 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1518 if (tv == get_mode_null(tv->mode))
1519 return TV_CLASSIFY_NULL;
1520 else if (tv == get_mode_one(tv->mode))
1521 return TV_CLASSIFY_ONE;
1522 else if ((get_mode_sort(tv->mode) == irms_int_number)
1523 && (tv == new_tarval_from_long(-1, tv->mode)))
1524 return TV_CLASSIFY_ALL_ONE;
1526 return TV_CLASSIFY_OTHER;
1530 * Returns non-zero if a given (integer) tarval has only one single bit
1533 int is_single_bit_tarval(tarval *tv) {
1537 if (!tv || tv == tarval_bad) return 0;
1538 if (! mode_is_int(tv->mode)) return 0;
1540 l = get_mode_size_bytes(tv->mode);
1541 for (bits = 0, i = l - 1; i >= 0; --i) {
1542 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1544 /* check for more than one bit in these */
1556 * Sets the overflow mode for integer operations.
1558 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1559 int_overflow_mode = ov_mode;
1563 * Get the overflow mode for integer operations.
1565 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1566 return int_overflow_mode;
1570 * default mode_info for output as HEX
1572 static const tarval_mode_info hex_output = {
1579 * Initialization of the tarval module: called before init_mode()
1581 void init_tarval_1(long null_value) {
1582 _null_value = null_value;
1584 /* initialize the sets holding the tarvals with a comparison function and
1585 * an initial size, which is the expected number of constants */
1586 tarvals = new_set(memcmp, N_CONSTANTS);
1587 values = new_set(memcmp, N_CONSTANTS);
1588 /* init strcalc with precision of 68 to support floating point values with 64
1589 * bit mantissa (needs extra bits for rounding and overflow) */
1595 * Initialization of the tarval module: called after init_mode()
1597 void init_tarval_2(void) {
1598 tarval_bad->kind = k_tarval;
1599 tarval_bad->mode = mode_BAD;
1600 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1602 tarval_undefined->kind = k_tarval;
1603 tarval_undefined->mode = mode_ANY;
1604 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1606 tarval_b_true->kind = k_tarval;
1607 tarval_b_true->mode = mode_b;
1608 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1610 tarval_b_false->kind = k_tarval;
1611 tarval_b_false->mode = mode_b;
1612 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1615 * assign output modes that are compatible with the
1616 * old implementation: Hex output
1618 set_tarval_mode_output_option(mode_U, &hex_output);
1619 set_tarval_mode_output_option(mode_C, &hex_output);
1620 set_tarval_mode_output_option(mode_Bs, &hex_output);
1621 set_tarval_mode_output_option(mode_Bu, &hex_output);
1622 set_tarval_mode_output_option(mode_Hs, &hex_output);
1623 set_tarval_mode_output_option(mode_Hu, &hex_output);
1624 set_tarval_mode_output_option(mode_Is, &hex_output);
1625 set_tarval_mode_output_option(mode_Iu, &hex_output);
1626 set_tarval_mode_output_option(mode_Ls, &hex_output);
1627 set_tarval_mode_output_option(mode_Lu, &hex_output);
1628 set_tarval_mode_output_option(mode_P, &hex_output);
1631 /* free all memory occupied by tarval. */
1632 void finish_tarval(void) {
1635 del_set(tarvals); tarvals = NULL;
1636 del_set(values); values = NULL;
1639 /****************************************************************************
1641 ****************************************************************************/