2 * Copyright (C) 1995-2008 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:
829 /* it should be safe to enable this even if other arithmetic is disabled */
831 return pn_Cmp_False;*/
833 * BEWARE: we cannot compare a == b here, because
834 * a NaN is always Unordered to any other value, even to itself!
836 switch (fc_comp(a->value, b->value)) {
837 case -1: return pn_Cmp_Lt;
838 case 0: return pn_Cmp_Eq;
839 case 1: return pn_Cmp_Gt;
840 case 2: return pn_Cmp_Uo;
841 default: return pn_Cmp_False;
844 case irms_int_number:
847 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
849 case irms_internal_boolean:
852 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
858 * convert to other mode
860 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
867 if (src->mode == dst_mode) return src;
869 if (get_mode_n_vector_elems(src->mode) > 1) {
870 /* vector arithmetic not implemented yet */
874 switch (get_mode_sort(src->mode)) {
875 case irms_control_flow:
880 /* cast float to something */
881 case irms_float_number:
882 switch (get_mode_sort(dst_mode)) {
883 case irms_float_number:
884 switch (get_mode_size_bits(dst_mode)) {
886 fc_cast(src->value, 8, 23, NULL);
889 fc_cast(src->value, 11, 52, NULL);
893 fc_cast(src->value, 15, 64, NULL);
896 panic("Unsupported mode in tarval_convert_to()");
898 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
900 case irms_int_number:
901 switch (GET_FLOAT_TO_INT_MODE()) {
903 res = fc_int(src->value, NULL);
906 res = fc_rnd(src->value, NULL);
912 buffer = alloca(sc_get_buffer_length());
913 if (! fc_flt2int(res, buffer, dst_mode))
915 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
918 /* the rest can't be converted */
923 /* cast int/characters to something */
924 case irms_int_number:
925 switch (get_mode_sort(dst_mode)) {
928 case irms_int_number:
929 buffer = alloca(sc_get_buffer_length());
930 memcpy(buffer, src->value, sc_get_buffer_length());
931 sign_extend(buffer, dst_mode);
932 return get_tarval_overflow(buffer, src->length, dst_mode);
934 case irms_internal_boolean:
935 /* XXX C semantics */
936 if (src == get_mode_null(src->mode)) return tarval_b_false;
937 else return tarval_b_true;
939 case irms_float_number:
940 /* XXX floating point unit does not understand internal integer
941 * representation, convert to string first, then create float from
943 buffer = alloca(100);
944 /* decimal string representation because hexadecimal output is
945 * interpreted unsigned by fc_val_from_str, so this is a HACK */
946 snprintf(buffer, 100, "%s",
947 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
948 buffer[100 - 1] = '\0';
949 switch (get_mode_size_bits(dst_mode)) {
951 fc_val_from_str(buffer, 0, 8, 23, NULL);
954 fc_val_from_str(buffer, 0, 11, 52, NULL);
958 fc_val_from_str(buffer, 0, 15, 64, NULL);
961 panic("Unsupported mode in tarval_convert_to()");
963 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
970 case irms_internal_boolean:
971 /* beware: this is C semantic for the INTERNAL boolean mode */
972 if (get_mode_sort(dst_mode) == irms_int_number)
973 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
977 if (get_mode_sort(dst_mode) == irms_int_number) {
978 buffer = alloca(sc_get_buffer_length());
979 memcpy(buffer, src->value, sc_get_buffer_length());
980 sign_extend(buffer, src->mode);
981 return get_tarval_overflow(buffer, src->length, dst_mode);
992 tarval *tarval_not(tarval *a) {
997 /* works for vector mode without changes */
999 switch (get_mode_sort(a->mode)) {
1000 case irms_reference:
1001 case irms_int_number:
1002 buffer = alloca(sc_get_buffer_length());
1003 sc_not(a->value, buffer);
1004 return get_tarval(buffer, a->length, a->mode);
1006 case irms_internal_boolean:
1007 if (a == tarval_b_true)
1008 return tarval_b_false;
1009 if (a == tarval_b_false)
1010 return tarval_b_true;
1014 assert(0 && "bitwise negation is only allowed for integer and boolean");
1020 * arithmetic negation
1022 tarval *tarval_neg(tarval *a) {
1026 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1028 /* note: negation is allowed even for unsigned modes. */
1030 if (get_mode_n_vector_elems(a->mode) > 1) {
1031 /* vector arithmetic not implemented yet */
1035 switch (get_mode_sort(a->mode)) {
1036 case irms_int_number:
1037 buffer = alloca(sc_get_buffer_length());
1038 sc_neg(a->value, buffer);
1039 return get_tarval_overflow(buffer, a->length, a->mode);
1041 case irms_float_number:
1042 /* it should be safe to enable this even if other arithmetic is disabled */
1044 return tarval_bad;*/
1046 fc_neg(a->value, NULL);
1047 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1057 tarval *tarval_add(tarval *a, tarval *b) {
1062 assert(a->mode == b->mode);
1064 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1065 /* vector arithmetic not implemented yet */
1069 switch (get_mode_sort(a->mode)) {
1070 case irms_int_number:
1071 /* modes of a,b are equal, so result has mode of a as this might be the character */
1072 buffer = alloca(sc_get_buffer_length());
1073 sc_add(a->value, b->value, buffer);
1074 return get_tarval_overflow(buffer, a->length, a->mode);
1076 case irms_float_number:
1080 fc_add(a->value, b->value, NULL);
1081 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1091 tarval *tarval_sub(tarval *a, tarval *b) {
1096 assert(a->mode == b->mode);
1098 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1099 /* vector arithmetic not implemented yet */
1102 switch (get_mode_sort(a->mode)) {
1103 case irms_int_number:
1104 /* modes of a,b are equal, so result has mode of a as this might be the character */
1105 buffer = alloca(sc_get_buffer_length());
1106 sc_sub(a->value, b->value, buffer);
1107 return get_tarval_overflow(buffer, a->length, a->mode);
1109 case irms_float_number:
1113 fc_sub(a->value, b->value, NULL);
1114 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1124 tarval *tarval_mul(tarval *a, tarval *b) {
1129 assert(a->mode == b->mode);
1131 if (get_mode_n_vector_elems(a->mode) > 1) {
1132 /* vector arithmetic not implemented yet */
1136 switch (get_mode_sort(a->mode)) {
1137 case irms_int_number:
1138 /* modes of a,b are equal */
1139 buffer = alloca(sc_get_buffer_length());
1140 sc_mul(a->value, b->value, buffer);
1141 return get_tarval_overflow(buffer, a->length, a->mode);
1143 case irms_float_number:
1147 fc_mul(a->value, b->value, NULL);
1148 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1156 * floating point division
1158 tarval *tarval_quo(tarval *a, tarval *b) {
1161 assert((a->mode == b->mode) && mode_is_float(a->mode));
1166 if (get_mode_n_vector_elems(a->mode) > 1) {
1167 /* vector arithmetic not implemented yet */
1171 fc_div(a->value, b->value, NULL);
1172 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1177 * overflow is impossible, but look out for division by zero
1179 tarval *tarval_div(tarval *a, tarval *b) {
1182 assert((a->mode == b->mode) && mode_is_int(a->mode));
1184 if (get_mode_n_vector_elems(a->mode) > 1) {
1185 /* vector arithmetic not implemented yet */
1190 if (b == get_mode_null(b->mode)) return tarval_bad;
1191 /* modes of a,b are equal */
1192 sc_div(a->value, b->value, NULL);
1193 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1198 * overflow is impossible, but look out for division by zero
1200 tarval *tarval_mod(tarval *a, tarval *b) {
1203 assert((a->mode == b->mode) && mode_is_int(a->mode));
1205 if (get_mode_n_vector_elems(a->mode) > 1) {
1206 /* vector arithmetic not implemented yet */
1211 if (b == get_mode_null(b->mode)) return tarval_bad;
1212 /* modes of a,b are equal */
1213 sc_mod(a->value, b->value, NULL);
1214 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1218 * integer division AND remainder
1219 * overflow is impossible, but look out for division by zero
1221 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1222 int len = sc_get_buffer_length();
1223 char *div_res = alloca(len);
1224 char *mod_res = alloca(len);
1228 assert((a->mode == b->mode) && mode_is_int(a->mode));
1230 if (get_mode_n_vector_elems(a->mode) > 1) {
1231 /* vector arithmetic not implemented yet */
1237 if (b == get_mode_null(b->mode)) return tarval_bad;
1238 /* modes of a,b are equal */
1239 sc_divmod(a->value, b->value, div_res, mod_res);
1240 *mod = get_tarval(mod_res, len, a->mode);
1241 return get_tarval(div_res, len, a->mode);
1247 tarval *tarval_abs(tarval *a) {
1251 assert(mode_is_num(a->mode));
1253 if (get_mode_n_vector_elems(a->mode) > 1) {
1254 /* vector arithmetic not implemented yet */
1258 switch (get_mode_sort(a->mode)) {
1259 case irms_int_number:
1260 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1261 buffer = alloca(sc_get_buffer_length());
1262 sc_neg(a->value, buffer);
1263 return get_tarval_overflow(buffer, a->length, a->mode);
1267 case irms_float_number:
1268 /* it should be safe to enable this even if other arithmetic is disabled */
1270 return tarval_bad;*/
1272 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1273 fc_neg(a->value, NULL);
1274 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1287 tarval *tarval_and(tarval *a, tarval *b) {
1290 assert(a->mode == b->mode);
1292 /* works even for vector modes */
1294 switch(get_mode_sort(a->mode)) {
1295 case irms_internal_boolean:
1296 return (a == tarval_b_false) ? a : b;
1298 case irms_int_number:
1299 sc_and(a->value, b->value, NULL);
1300 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1303 assert(0 && "operation not defined on mode");
1311 tarval *tarval_or (tarval *a, tarval *b) {
1314 assert(a->mode == b->mode);
1316 /* works even for vector modes */
1318 switch (get_mode_sort(a->mode)) {
1319 case irms_internal_boolean:
1320 return (a == tarval_b_true) ? a : b;
1322 case irms_int_number:
1323 sc_or(a->value, b->value, NULL);
1324 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1327 assert(0 && "operation not defined on mode");
1333 * bitwise exclusive or (xor)
1335 tarval *tarval_eor(tarval *a, tarval *b) {
1338 assert((a->mode == b->mode));
1340 /* works even for vector modes */
1342 switch (get_mode_sort(a->mode)) {
1343 case irms_internal_boolean:
1344 return (a == b)? tarval_b_false : tarval_b_true;
1346 case irms_int_number:
1347 sc_xor(a->value, b->value, NULL);
1348 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1351 assert(0 && "operation not defined on mode");
1357 * bitwise left shift
1359 tarval *tarval_shl(tarval *a, tarval *b) {
1360 char *temp_val = NULL;
1364 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1366 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1367 /* vector arithmetic not implemented yet */
1371 if (get_mode_modulo_shift(a->mode) != 0) {
1372 temp_val = alloca(sc_get_buffer_length());
1374 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1375 sc_mod(b->value, temp_val, temp_val);
1377 temp_val = (char*)b->value;
1379 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1380 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1384 * bitwise unsigned right shift
1386 tarval *tarval_shr(tarval *a, tarval *b) {
1387 char *temp_val = NULL;
1391 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1393 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1394 /* vector arithmetic not implemented yet */
1398 if (get_mode_modulo_shift(a->mode) != 0) {
1399 temp_val = alloca(sc_get_buffer_length());
1401 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1402 sc_mod(b->value, temp_val, temp_val);
1404 temp_val = (char*)b->value;
1406 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1407 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1411 * bitwise signed right shift
1413 tarval *tarval_shrs(tarval *a, tarval *b) {
1414 char *temp_val = NULL;
1418 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1420 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1421 /* vector arithmetic not implemented yet */
1425 if (get_mode_modulo_shift(a->mode) != 0) {
1426 temp_val = alloca(sc_get_buffer_length());
1428 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1429 sc_mod(b->value, temp_val, temp_val);
1431 temp_val = (char*)b->value;
1433 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1434 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1440 tarval *tarval_rot(tarval *a, tarval *b) {
1441 char *temp_val = NULL;
1445 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1447 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1448 /* vector arithmetic not implemented yet */
1452 if (get_mode_modulo_shift(a->mode) != 0) {
1453 temp_val = alloca(sc_get_buffer_length());
1455 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1456 sc_mod(b->value, temp_val, temp_val);
1458 temp_val = (char*)b->value;
1460 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1461 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1465 * carry flag of the last operation
1467 int tarval_carry(void) {
1468 return sc_had_carry();
1474 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1475 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1479 const tarval_mode_info *mode_info;
1480 const char *prefix, *suffix;
1482 mode_info = tv->mode->tv_priv;
1484 mode_info = &default_info;
1485 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1486 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1488 switch (get_mode_sort(tv->mode)) {
1489 case irms_reference:
1490 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1492 case irms_int_number:
1493 switch (mode_info->mode_output) {
1496 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1500 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1506 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1509 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1511 case irms_float_number:
1512 switch (mode_info->mode_output) {
1514 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1517 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1522 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1526 case irms_internal_boolean:
1527 switch (mode_info->mode_output) {
1533 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1537 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1540 case irms_control_flow:
1542 case irms_auxiliary:
1543 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1550 * Output of tarvals to stdio.
1552 int tarval_printf(tarval *tv) {
1556 res = tarval_snprintf(buf, sizeof(buf), tv);
1557 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1562 char *get_tarval_bitpattern(tarval *tv) {
1564 int n = get_mode_size_bits(tv->mode);
1565 int bytes = (n + 7) / 8;
1566 char *res = xmalloc((n + 1) * sizeof(char));
1569 for(i = 0; i < bytes; i++) {
1570 byte = get_tarval_sub_bits(tv, i);
1571 for(j = 1; j < 256; j <<= 1)
1573 res[pos++] = j & byte ? '1' : '0';
1582 * access to the bitpattern
1584 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1585 switch (get_mode_arithmetic(tv->mode)) {
1586 case irma_twos_complement:
1587 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1589 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1591 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1596 * Specify the output options of one mode.
1598 * This functions stores the modinfo, so DO NOT DESTROY it.
1600 * Returns zero on success.
1602 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1605 mode->tv_priv = modeinfo;
1610 * Returns the output options of one mode.
1612 * This functions returns the mode info of a given mode.
1614 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1617 return mode->tv_priv;
1621 * Returns non-zero if a given (integer) tarval has only one single bit
1624 int tarval_is_single_bit(tarval *tv) {
1628 if (!tv || tv == tarval_bad) return 0;
1629 if (! mode_is_int(tv->mode)) return 0;
1631 l = get_mode_size_bytes(tv->mode);
1632 for (bits = 0, i = l - 1; i >= 0; --i) {
1633 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1635 /* check for more than one bit in these */
1647 * Returns non-zero if the mantissa of a floating point IEEE-754
1648 * tarval is zero (i.e. 1.0Exxx)
1650 int tarval_ieee754_zero_mantissa(tarval *tv) {
1651 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1652 return fc_zero_mantissa(tv->value);
1655 /* Returns the exponent of a floating point IEEE-754 tarval. */
1656 int tarval_ieee754_get_exponent(tarval *tv) {
1657 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1658 return fc_get_exponent(tv->value);
1661 /* Set the immediate precision for IEEE-754 results. */
1662 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1663 return fc_set_immediate_precision(bits);
1666 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1667 unsigned tarval_ieee754_get_exact(void) {
1668 return fc_is_exact();
1671 /* check if its the a floating point NaN */
1672 int tarval_is_NaN(tarval *tv) {
1673 if (! mode_is_float(tv->mode))
1675 return fc_is_nan(tv->value);
1678 /* check if its the a floating point +inf */
1679 int tarval_is_plus_inf(tarval *tv) {
1680 if (! mode_is_float(tv->mode))
1682 return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1685 /* check if its the a floating point -inf */
1686 int tarval_is_minus_inf(tarval *tv) {
1687 if (! mode_is_float(tv->mode))
1689 return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1692 /* check if the tarval represents a finite value */
1693 int tarval_is_finite(tarval *tv) {
1694 if (mode_is_float(tv->mode))
1695 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1700 * Sets the overflow mode for integer operations.
1702 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1703 int_overflow_mode = ov_mode;
1706 /* Get the overflow mode for integer operations. */
1707 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1708 return int_overflow_mode;
1711 /* Enable/Disable floating point constant folding. */
1712 int tarval_enable_fp_ops(int enable) {
1713 int old = !no_float;
1720 * default mode_info for output as HEX
1722 static const tarval_mode_info hex_output = {
1729 * Initialization of the tarval module: called before init_mode()
1731 void init_tarval_1(long null_value) {
1732 _null_value = null_value;
1734 /* initialize the sets holding the tarvals with a comparison function and
1735 * an initial size, which is the expected number of constants */
1736 tarvals = new_set(memcmp, N_CONSTANTS);
1737 values = new_set(memcmp, N_CONSTANTS);
1738 /* init strcalc with precision of 68 to support floating point values with 64
1739 * bit mantissa (needs extra bits for rounding and overflow) */
1745 * Initialization of the tarval module: called after init_mode()
1747 void init_tarval_2(void) {
1748 tarval_bad->kind = k_tarval;
1749 tarval_bad->mode = mode_BAD;
1750 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1752 tarval_undefined->kind = k_tarval;
1753 tarval_undefined->mode = mode_ANY;
1754 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1756 tarval_b_true->kind = k_tarval;
1757 tarval_b_true->mode = mode_b;
1758 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1760 tarval_b_false->kind = k_tarval;
1761 tarval_b_false->mode = mode_b;
1762 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1765 * assign output modes that are compatible with the
1766 * old implementation: Hex output
1768 set_tarval_mode_output_option(mode_Bs, &hex_output);
1769 set_tarval_mode_output_option(mode_Bu, &hex_output);
1770 set_tarval_mode_output_option(mode_Hs, &hex_output);
1771 set_tarval_mode_output_option(mode_Hu, &hex_output);
1772 set_tarval_mode_output_option(mode_Is, &hex_output);
1773 set_tarval_mode_output_option(mode_Iu, &hex_output);
1774 set_tarval_mode_output_option(mode_Ls, &hex_output);
1775 set_tarval_mode_output_option(mode_Lu, &hex_output);
1776 set_tarval_mode_output_option(mode_P, &hex_output);
1779 /* free all memory occupied by tarval. */
1780 void finish_tarval(void) {
1783 del_set(tarvals); tarvals = NULL;
1784 del_set(values); values = NULL;
1787 /****************************************************************************
1789 ****************************************************************************/