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 /** A set containing all existing tarvals. */
98 static struct set *tarvals = NULL;
99 /** A set containing all existing values. */
100 static struct set *values = NULL;
102 /** The integer overflow mode. */
103 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
105 /** if this is set non-zero, the constant folding for floating point is OFF */
106 static int no_float = 0;
108 /** IEEE-754r half precision */
109 static const ieee_descriptor_t half_desc = { 5, 10, 0, NORMAL };
110 /** IEEE-754 single precision */
111 static const ieee_descriptor_t single_desc = { 8, 23, 0, NORMAL };
112 /** IEEE-754 double precision */
113 static const ieee_descriptor_t double_desc = { 11, 52, 0, NORMAL };
114 /** Intel x87 extended precision */
115 static const ieee_descriptor_t extended_desc = { 15, 63, 1, NORMAL };
117 /** IEEE-754r quad precision */
118 static const ieee_descriptor_t quad_desc = { 15, 112, 0, NORMAL };
120 /****************************************************************************
122 ****************************************************************************/
124 static int hash_val(const void *value, unsigned int length);
125 static int hash_tv(tarval *tv);
126 static void _fail_verify(tarval *tv, const char* file, int line)
128 /* print a memory image of the tarval and throw an assertion */
130 panic("%s:%d: Invalid tarval: mode: %F\n value: [%p]", file, line, tv->mode, tv->value);
132 panic("%s:%d: Invalid tarval (null)", file, line);
135 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
138 INLINE static void tarval_verify(tarval *tv)
144 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
145 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
147 if (!FIND_TARVAL(tv)) fail_verify(tv);
148 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
152 /** Hash a tarval. */
153 static int hash_tv(tarval *tv) {
154 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
157 /** Hash a value. Treat it as a byte array. */
158 static int hash_val(const void *value, unsigned int length) {
160 unsigned int hash = 0;
162 /* scramble the byte - array */
163 for (i = 0; i < length; ++i) {
164 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
165 hash += (hash << 11) ^ (hash >> 17);
171 static int cmp_tv(const void *p1, const void *p2, size_t n) {
172 const tarval *tv1 = p1;
173 const tarval *tv2 = p2;
176 assert(tv1->kind == k_tarval);
177 assert(tv2->kind == k_tarval);
178 if (tv1->mode < tv2->mode)
180 if (tv1->mode > tv2->mode)
182 if (tv1->length < tv2->length)
184 if (tv1->length > tv2->length)
186 if (tv1->value < tv2->value)
188 if (tv1->value > tv2->value)
194 /** finds tarval with value/mode or creates new tarval */
195 static tarval *get_tarval(const void *value, int length, ir_mode *mode) {
202 /* if there already is such a value, it is returned, else value
203 * is copied into the set */
204 char *temp = alloca(length);
205 memcpy(temp, value, length);
206 if (get_mode_arithmetic(mode) == irma_twos_complement) {
207 sign_extend(temp, mode);
209 tv.value = INSERT_VALUE(temp, length);
213 /* if there is such a tarval, it is returned, else tv is copied
215 return (tarval *)INSERT_TARVAL(&tv);
221 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
225 switch (get_mode_sort(mode)) {
227 /* addresses always wrap around */
228 temp = alloca(sc_get_buffer_length());
229 memcpy(temp, value, sc_get_buffer_length());
230 sc_truncate(get_mode_size_bits(mode), temp);
231 /* the sc_ module expects that all bits are set ... */
232 sign_extend(temp, mode);
233 return get_tarval(temp, length, mode);
235 case irms_int_number:
236 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
237 switch (GET_OVERFLOW_MODE()) {
238 case TV_OVERFLOW_SATURATE:
239 return get_mode_max(mode);
240 case TV_OVERFLOW_WRAP:
241 temp = alloca(sc_get_buffer_length());
242 memcpy(temp, value, sc_get_buffer_length());
243 sc_truncate(get_mode_size_bits(mode), temp);
244 /* the sc_ module expects that all bits are set ... */
245 sign_extend(temp, mode);
246 return get_tarval(temp, length, mode);
247 case TV_OVERFLOW_BAD:
250 return get_tarval(value, length, mode);
253 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
254 switch (GET_OVERFLOW_MODE()) {
255 case TV_OVERFLOW_SATURATE:
256 return get_mode_min(mode);
257 case TV_OVERFLOW_WRAP: {
258 char *temp = alloca(sc_get_buffer_length());
259 memcpy(temp, value, sc_get_buffer_length());
260 sc_truncate(get_mode_size_bits(mode), temp);
261 return get_tarval(temp, length, mode);
263 case TV_OVERFLOW_BAD:
266 return get_tarval(value, length, mode);
271 case irms_float_number:
272 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
273 /* clip infinity to maximum value */
274 return fc_is_negative(value) ? get_mode_min(mode) : get_mode_max(mode);
277 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
278 /* clip denormals to zero */
279 return get_mode_null(mode);
286 return get_tarval(value, length, mode);
290 * public variables declared in tv.h
292 static tarval reserved_tv[6];
294 tarval *tarval_b_false = &reserved_tv[0];
295 tarval *tarval_b_true = &reserved_tv[1];
296 tarval *tarval_bad = &reserved_tv[2];
297 tarval *tarval_undefined = &reserved_tv[3];
298 tarval *tarval_reachable = &reserved_tv[4];
299 tarval *tarval_unreachable = &reserved_tv[5];
302 * get the float descriptor for given mode.
304 static const ieee_descriptor_t *get_descriptor(const ir_mode *mode) {
305 switch (get_mode_size_bits(mode)) {
306 case 16: return &half_desc;
307 case 32: return &single_desc;
308 case 64: return &double_desc;
310 case 96: return &extended_desc;
311 case 128: return &quad_desc;
313 panic("Unsupported mode in get_descriptor()");
319 * public functions declared in tv.h
323 * Constructors =============================================================
325 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
327 const ieee_descriptor_t *desc;
333 switch (get_mode_sort(mode)) {
334 case irms_control_flow:
337 panic("Unsupported tarval creation with mode %F", mode);
339 case irms_internal_boolean:
340 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
341 if (strcasecmp(str, "true"))
342 return tarval_b_true;
343 else if (strcasecmp(str, "false"))
344 return tarval_b_true;
346 /* XXX This is C semantics */
347 return atoi(str) ? tarval_b_true : tarval_b_false;
349 case irms_float_number:
350 desc = get_descriptor(mode);
351 fc_val_from_str(str, len, desc, NULL);
352 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
355 /* same as integer modes */
356 case irms_int_number:
357 sc_val_from_str(str, len, NULL, mode);
358 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
360 panic("Unsupported tarval creation with mode %F", mode);
364 * helper function, create a tarval from long
366 tarval *new_tarval_from_long(long l, ir_mode *mode) {
369 switch (get_mode_sort(mode)) {
370 case irms_internal_boolean:
371 /* XXX C semantics ! */
372 return l ? tarval_b_true : tarval_b_false ;
375 /* same as integer modes */
376 case irms_int_number:
377 sc_val_from_long(l, NULL);
378 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
380 case irms_float_number:
381 return new_tarval_from_double((long double)l, mode);
384 assert(0 && "unsupported mode sort");
389 /* returns non-zero if can be converted to long */
390 int tarval_is_long(tarval *tv) {
391 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
394 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
395 /* the value might be too big to fit in a long */
396 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
397 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
398 /* really doesn't fit */
405 /* this might overflow the machine's long, so use only with small values */
406 long get_tarval_long(tarval* tv) {
407 assert(tarval_is_long(tv) && "tarval too big to fit in long");
409 return sc_val_to_long(tv->value);
412 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
413 const ieee_descriptor_t *desc;
415 assert(mode && (get_mode_sort(mode) == irms_float_number));
416 desc = get_descriptor(mode);
417 fc_val_from_ieee754(d, desc, NULL);
418 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
421 /* returns non-zero if can be converted to double */
422 int tarval_is_double(tarval *tv) {
425 return (get_mode_sort(tv->mode) == irms_float_number);
428 long double get_tarval_double(tarval *tv) {
429 assert(tarval_is_double(tv));
431 return fc_val_to_ieee754(tv->value);
436 * Access routines for tarval fields ========================================
439 /* get the mode of the tarval */
440 ir_mode *(get_tarval_mode)(const tarval *tv) {
441 return _get_tarval_mode(tv);
445 * Special value query functions ============================================
447 * These functions calculate and return a tarval representing the requested
449 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
450 * functions, but these are stored on initialization of the irmode module and
451 * therefore the irmode functions should be preferred to the functions below.
454 tarval *(get_tarval_bad)(void) {
455 return _get_tarval_bad();
458 tarval *(get_tarval_undefined)(void) {
459 return _get_tarval_undefined();
462 tarval *(get_tarval_b_false)(void) {
463 return _get_tarval_b_false();
466 tarval *(get_tarval_b_true)(void) {
467 return _get_tarval_b_true();
470 tarval *(get_tarval_reachable)(void) {
471 return _get_tarval_reachable();
474 tarval *(get_tarval_unreachable)(void) {
475 return _get_tarval_unreachable();
478 tarval *get_tarval_max(ir_mode *mode) {
479 const ieee_descriptor_t *desc;
482 if (get_mode_n_vector_elems(mode) > 1) {
483 /* vector arithmetic not implemented yet */
487 switch (get_mode_sort(mode)) {
488 case irms_control_flow:
491 panic("mode %F does not support maximum value", mode);
493 case irms_internal_boolean:
494 return tarval_b_true;
496 case irms_float_number:
497 desc = get_descriptor(mode);
498 fc_get_max(desc, NULL);
499 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
502 case irms_int_number:
503 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
504 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
509 tarval *get_tarval_min(ir_mode *mode) {
510 const ieee_descriptor_t *desc;
513 if (get_mode_n_vector_elems(mode) > 1) {
514 /* vector arithmetic not implemented yet */
518 switch (get_mode_sort(mode)) {
519 case irms_control_flow:
522 panic("mode %F does not support minimum value", mode);
524 case irms_internal_boolean:
525 return tarval_b_false;
527 case irms_float_number:
528 desc = get_descriptor(mode);
529 fc_get_min(desc, NULL);
530 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
533 case irms_int_number:
534 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
535 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
540 /** The bit pattern for the pointer NULL */
541 static long _null_value = 0;
543 tarval *get_tarval_null(ir_mode *mode) {
546 if (get_mode_n_vector_elems(mode) > 1) {
547 /* vector arithmetic not implemented yet */
551 switch (get_mode_sort(mode)) {
552 case irms_control_flow:
555 panic("mode %F does not support null value", mode);
557 case irms_float_number:
558 return new_tarval_from_double(0.0, mode);
560 case irms_internal_boolean:
561 case irms_int_number:
562 return new_tarval_from_long(0l, mode);
565 return new_tarval_from_long(_null_value, mode);
570 tarval *get_tarval_one(ir_mode *mode) {
573 if (get_mode_n_vector_elems(mode) > 1)
574 panic("vector arithmetic not implemented yet");
576 switch (get_mode_sort(mode)) {
577 case irms_control_flow:
580 panic("mode %F does not support one value", mode);
582 case irms_internal_boolean:
583 return tarval_b_true;
585 case irms_float_number:
586 return new_tarval_from_double(1.0, mode);
589 case irms_int_number:
590 return new_tarval_from_long(1l, mode);
595 tarval *get_tarval_all_one(ir_mode *mode) {
598 if (get_mode_n_vector_elems(mode) > 1)
599 panic("vector arithmetic not implemented yet");
601 switch (get_mode_sort(mode)) {
602 case irms_control_flow:
605 panic("mode %F does not support all-one value", mode);
607 case irms_int_number:
608 case irms_internal_boolean:
610 return tarval_not(get_mode_null(mode));
613 case irms_float_number:
614 return new_tarval_from_double(1.0, mode);
619 int tarval_is_constant(tarval *tv) {
620 int num_res = sizeof(reserved_tv) / sizeof(reserved_tv[0]);
622 /* reserved tarvals are NOT constants. Note that although
623 tarval_b_true and tarval_b_false are reserved, they are constants of course. */
624 return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
627 tarval *get_tarval_minus_one(ir_mode *mode) {
630 if (get_mode_n_vector_elems(mode) > 1)
631 panic("vector arithmetic not implemented yet");
633 switch (get_mode_sort(mode)) {
634 case irms_control_flow:
637 case irms_internal_boolean:
638 panic("mode %F does not support minus one value", mode);
643 case irms_float_number:
644 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
646 case irms_int_number:
647 return new_tarval_from_long(-1l, mode);
652 tarval *get_tarval_nan(ir_mode *mode) {
653 const ieee_descriptor_t *desc;
656 if (get_mode_n_vector_elems(mode) > 1)
657 panic("vector arithmetic not implemented yet");
659 if (get_mode_sort(mode) == irms_float_number) {
660 desc = get_descriptor(mode);
661 fc_get_qnan(desc, NULL);
662 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
664 panic("mode %F does not support NaN value", mode);
667 tarval *get_tarval_plus_inf(ir_mode *mode) {
669 if (get_mode_n_vector_elems(mode) > 1)
670 panic("vector arithmetic not implemented yet");
672 if (get_mode_sort(mode) == irms_float_number) {
673 const ieee_descriptor_t *desc = get_descriptor(mode);
674 fc_get_plusinf(desc, NULL);
675 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
677 panic("mode %F does not support +inf value", mode);
680 tarval *get_tarval_minus_inf(ir_mode *mode) {
683 if (get_mode_n_vector_elems(mode) > 1)
684 panic("vector arithmetic not implemented yet");
686 if (get_mode_sort(mode) == irms_float_number) {
687 const ieee_descriptor_t *desc = get_descriptor(mode);
688 fc_get_minusinf(desc, NULL);
689 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
691 panic("mode %F does not support -inf value", mode);
695 * Arithmetic operations on tarvals ========================================
699 * test if negative number, 1 means 'yes'
701 int tarval_is_negative(tarval *a) {
704 if (get_mode_n_vector_elems(a->mode) > 1)
705 panic("vector arithmetic not implemented yet");
707 switch (get_mode_sort(a->mode)) {
708 case irms_int_number:
709 if (!mode_is_signed(a->mode)) return 0;
711 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
713 case irms_float_number:
714 return fc_is_negative(a->value);
717 panic("mode %F does not support negation value", a->mode);
722 * test if null, 1 means 'yes'
724 int tarval_is_null(tarval *a) {
727 a == get_mode_null(get_tarval_mode(a));
731 * test if one, 1 means 'yes'
733 int tarval_is_one(tarval *a) {
736 a == get_mode_one(get_tarval_mode(a));
739 int tarval_is_all_one(tarval *tv) {
742 tv == get_mode_all_one(get_tarval_mode(tv));
746 * test if one, 1 means 'yes'
748 int tarval_is_minus_one(tarval *a) {
751 a == get_mode_minus_one(get_tarval_mode(a));
757 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
761 if (a == tarval_bad || b == tarval_bad) {
762 panic("Comparison with tarval_bad");
766 if (a == tarval_undefined || b == tarval_undefined)
769 if (a->mode != b->mode)
772 if (get_mode_n_vector_elems(a->mode) > 1) {
773 /* vector arithmetic not implemented yet */
774 assert(0 && "cmp not implemented for vector modes");
777 /* Here the two tarvals are unequal and of the same mode */
778 switch (get_mode_sort(a->mode)) {
779 case irms_control_flow:
786 case irms_float_number:
787 /* it should be safe to enable this even if other arithmetic is disabled */
789 return pn_Cmp_False;*/
791 * BEWARE: we cannot compare a == b here, because
792 * a NaN is always Unordered to any other value, even to itself!
794 switch (fc_comp(a->value, b->value)) {
795 case -1: return pn_Cmp_Lt;
796 case 0: return pn_Cmp_Eq;
797 case 1: return pn_Cmp_Gt;
798 case 2: return pn_Cmp_Uo;
799 default: return pn_Cmp_False;
802 case irms_int_number:
805 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
807 case irms_internal_boolean:
810 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
816 * convert to other mode
818 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
821 const ieee_descriptor_t *desc;
826 if (src->mode == dst_mode)
829 if (get_mode_n_vector_elems(src->mode) > 1) {
830 /* vector arithmetic not implemented yet */
834 switch (get_mode_sort(src->mode)) {
835 case irms_control_flow:
840 /* cast float to something */
841 case irms_float_number:
842 switch (get_mode_sort(dst_mode)) {
843 case irms_float_number:
844 desc = get_descriptor(dst_mode);
845 fc_cast(src->value, desc, NULL);
846 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
848 case irms_int_number:
849 switch (GET_FLOAT_TO_INT_MODE()) {
851 res = fc_int(src->value, NULL);
854 res = fc_rnd(src->value, NULL);
857 panic("Unsupported float to int conversion mode in tarval_convert_to()");
860 buffer = alloca(sc_get_buffer_length());
861 if (! fc_flt2int(res, buffer, dst_mode))
863 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
866 /* the rest can't be converted */
871 /* cast int/characters to something */
872 case irms_int_number:
873 switch (get_mode_sort(dst_mode)) {
876 case irms_int_number:
877 buffer = alloca(sc_get_buffer_length());
878 memcpy(buffer, src->value, sc_get_buffer_length());
879 sign_extend(buffer, dst_mode);
880 return get_tarval_overflow(buffer, src->length, dst_mode);
882 case irms_internal_boolean:
883 /* XXX C semantics */
884 if (src == get_mode_null(src->mode)) return tarval_b_false;
885 else return tarval_b_true;
887 case irms_float_number:
888 /* XXX floating point unit does not understand internal integer
889 * representation, convert to string first, then create float from
891 buffer = alloca(100);
892 /* decimal string representation because hexadecimal output is
893 * interpreted unsigned by fc_val_from_str, so this is a HACK */
894 snprintf(buffer, 100, "%s",
895 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
896 buffer[100 - 1] = '\0';
897 desc = get_descriptor(dst_mode);
898 fc_val_from_str(buffer, 0, desc, NULL);
899 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
906 case irms_internal_boolean:
907 /* beware: this is C semantic for the INTERNAL boolean mode */
908 if (get_mode_sort(dst_mode) == irms_int_number)
909 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
913 if (get_mode_sort(dst_mode) == irms_int_number) {
914 buffer = alloca(sc_get_buffer_length());
915 memcpy(buffer, src->value, sc_get_buffer_length());
916 sign_extend(buffer, src->mode);
917 return get_tarval_overflow(buffer, src->length, dst_mode);
928 tarval *tarval_not(tarval *a) {
933 /* works for vector mode without changes */
935 switch (get_mode_sort(a->mode)) {
937 case irms_int_number:
938 buffer = alloca(sc_get_buffer_length());
939 sc_not(a->value, buffer);
940 return get_tarval(buffer, a->length, a->mode);
942 case irms_internal_boolean:
943 if (a == tarval_b_true)
944 return tarval_b_false;
945 if (a == tarval_b_false)
946 return tarval_b_true;
950 assert(0 && "bitwise negation is only allowed for integer and boolean");
956 * arithmetic negation
958 tarval *tarval_neg(tarval *a) {
962 assert(mode_is_num(a->mode)); /* negation only for numerical values */
964 /* note: negation is allowed even for unsigned modes. */
966 if (get_mode_n_vector_elems(a->mode) > 1) {
967 /* vector arithmetic not implemented yet */
971 switch (get_mode_sort(a->mode)) {
972 case irms_int_number:
973 buffer = alloca(sc_get_buffer_length());
974 sc_neg(a->value, buffer);
975 return get_tarval_overflow(buffer, a->length, a->mode);
977 case irms_float_number:
978 /* it should be safe to enable this even if other arithmetic is disabled */
982 fc_neg(a->value, NULL);
983 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
993 tarval *tarval_add(tarval *a, tarval *b) {
999 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1000 /* vector arithmetic not implemented yet */
1004 if (mode_is_reference(a->mode) && a->mode != b->mode) {
1005 b = tarval_convert_to(b, a->mode);
1006 } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
1007 a = tarval_convert_to(a, b->mode);
1010 assert(a->mode == b->mode);
1012 switch (get_mode_sort(a->mode)) {
1013 case irms_reference:
1014 case irms_int_number:
1015 /* modes of a,b are equal, so result has mode of a as this might be the character */
1016 buffer = alloca(sc_get_buffer_length());
1017 sc_add(a->value, b->value, buffer);
1018 return get_tarval_overflow(buffer, a->length, a->mode);
1020 case irms_float_number:
1024 fc_add(a->value, b->value, NULL);
1025 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1035 tarval *tarval_sub(tarval *a, tarval *b, ir_mode *dst_mode) {
1041 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1042 /* vector arithmetic not implemented yet */
1046 if (dst_mode != NULL) {
1047 if (a->mode != dst_mode)
1048 a = tarval_convert_to(a, dst_mode);
1049 if (b->mode != dst_mode)
1050 b = tarval_convert_to(b, dst_mode);
1052 assert(a->mode == b->mode);
1054 switch (get_mode_sort(a->mode)) {
1055 case irms_reference:
1056 case irms_int_number:
1057 /* modes of a,b are equal, so result has mode of a as this might be the character */
1058 buffer = alloca(sc_get_buffer_length());
1059 sc_sub(a->value, b->value, buffer);
1060 return get_tarval_overflow(buffer, a->length, a->mode);
1062 case irms_float_number:
1066 fc_sub(a->value, b->value, NULL);
1067 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1077 tarval *tarval_mul(tarval *a, tarval *b) {
1082 assert(a->mode == b->mode);
1084 if (get_mode_n_vector_elems(a->mode) > 1) {
1085 /* vector arithmetic not implemented yet */
1089 switch (get_mode_sort(a->mode)) {
1090 case irms_int_number:
1091 /* modes of a,b are equal */
1092 buffer = alloca(sc_get_buffer_length());
1093 sc_mul(a->value, b->value, buffer);
1094 return get_tarval_overflow(buffer, a->length, a->mode);
1096 case irms_float_number:
1100 fc_mul(a->value, b->value, NULL);
1101 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1109 * floating point division
1111 tarval *tarval_quo(tarval *a, tarval *b) {
1114 assert((a->mode == b->mode) && mode_is_float(a->mode));
1119 if (get_mode_n_vector_elems(a->mode) > 1) {
1120 /* vector arithmetic not implemented yet */
1124 fc_div(a->value, b->value, NULL);
1125 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1130 * overflow is impossible, but look out for division by zero
1132 tarval *tarval_div(tarval *a, tarval *b) {
1135 assert((a->mode == b->mode) && mode_is_int(a->mode));
1137 if (get_mode_n_vector_elems(a->mode) > 1) {
1138 /* vector arithmetic not implemented yet */
1143 if (b == get_mode_null(b->mode)) return tarval_bad;
1144 /* modes of a,b are equal */
1145 sc_div(a->value, b->value, NULL);
1146 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1151 * overflow is impossible, but look out for division by zero
1153 tarval *tarval_mod(tarval *a, tarval *b) {
1156 assert((a->mode == b->mode) && mode_is_int(a->mode));
1158 if (get_mode_n_vector_elems(a->mode) > 1) {
1159 /* vector arithmetic not implemented yet */
1164 if (b == get_mode_null(b->mode)) return tarval_bad;
1165 /* modes of a,b are equal */
1166 sc_mod(a->value, b->value, NULL);
1167 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1171 * integer division AND remainder
1172 * overflow is impossible, but look out for division by zero
1174 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1175 int len = sc_get_buffer_length();
1176 char *div_res = alloca(len);
1177 char *mod_res = alloca(len);
1181 assert((a->mode == b->mode) && mode_is_int(a->mode));
1183 if (get_mode_n_vector_elems(a->mode) > 1) {
1184 /* 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_divmod(a->value, b->value, div_res, mod_res);
1193 *mod = get_tarval(mod_res, len, a->mode);
1194 return get_tarval(div_res, len, a->mode);
1200 tarval *tarval_abs(tarval *a) {
1204 assert(mode_is_num(a->mode));
1206 if (get_mode_n_vector_elems(a->mode) > 1) {
1207 /* vector arithmetic not implemented yet */
1211 switch (get_mode_sort(a->mode)) {
1212 case irms_int_number:
1213 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1214 buffer = alloca(sc_get_buffer_length());
1215 sc_neg(a->value, buffer);
1216 return get_tarval_overflow(buffer, a->length, a->mode);
1220 case irms_float_number:
1221 /* it should be safe to enable this even if other arithmetic is disabled */
1223 return tarval_bad;*/
1225 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1226 fc_neg(a->value, NULL);
1227 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1240 tarval *tarval_and(tarval *a, tarval *b) {
1243 assert(a->mode == b->mode);
1245 /* works even for vector modes */
1247 switch (get_mode_sort(a->mode)) {
1248 case irms_internal_boolean:
1249 return (a == tarval_b_false) ? a : b;
1251 case irms_int_number:
1252 sc_and(a->value, b->value, NULL);
1253 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1256 assert(0 && "operation not defined on mode");
1264 tarval *tarval_or(tarval *a, tarval *b) {
1267 assert(a->mode == b->mode);
1269 /* works even for vector modes */
1271 switch (get_mode_sort(a->mode)) {
1272 case irms_internal_boolean:
1273 return (a == tarval_b_true) ? a : b;
1275 case irms_int_number:
1276 sc_or(a->value, b->value, NULL);
1277 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1280 assert(0 && "operation not defined on mode");
1286 * bitwise exclusive or (xor)
1288 tarval *tarval_eor(tarval *a, tarval *b) {
1291 assert((a->mode == b->mode));
1293 /* works even for vector modes */
1295 switch (get_mode_sort(a->mode)) {
1296 case irms_internal_boolean:
1297 return (a == b)? tarval_b_false : tarval_b_true;
1299 case irms_int_number:
1300 sc_xor(a->value, b->value, NULL);
1301 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1304 assert(0 && "operation not defined on mode");
1310 * bitwise left shift
1312 tarval *tarval_shl(tarval *a, tarval *b) {
1313 char *temp_val = NULL;
1317 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1319 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1320 /* vector arithmetic not implemented yet */
1324 if (get_mode_modulo_shift(a->mode) != 0) {
1325 temp_val = alloca(sc_get_buffer_length());
1327 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1328 sc_mod(b->value, temp_val, temp_val);
1330 temp_val = (char*)b->value;
1332 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1333 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1337 * bitwise unsigned right shift
1339 tarval *tarval_shr(tarval *a, tarval *b) {
1340 char *temp_val = NULL;
1344 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1346 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1347 /* vector arithmetic not implemented yet */
1351 if (get_mode_modulo_shift(a->mode) != 0) {
1352 temp_val = alloca(sc_get_buffer_length());
1354 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1355 sc_mod(b->value, temp_val, temp_val);
1357 temp_val = (char*)b->value;
1359 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1360 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1364 * bitwise signed right shift
1366 tarval *tarval_shrs(tarval *a, tarval *b) {
1367 char *temp_val = NULL;
1371 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1373 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1374 /* vector arithmetic not implemented yet */
1378 if (get_mode_modulo_shift(a->mode) != 0) {
1379 temp_val = alloca(sc_get_buffer_length());
1381 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1382 sc_mod(b->value, temp_val, temp_val);
1384 temp_val = (char*)b->value;
1386 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1387 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1391 * bitwise rotation to left
1393 tarval *tarval_rotl(tarval *a, tarval *b) {
1394 char *temp_val = NULL;
1398 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1400 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1401 /* vector arithmetic not implemented yet */
1405 if (get_mode_modulo_shift(a->mode) != 0) {
1406 temp_val = alloca(sc_get_buffer_length());
1408 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1409 sc_mod(b->value, temp_val, temp_val);
1411 temp_val = (char*)b->value;
1413 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1414 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1418 * carry flag of the last operation
1420 int tarval_carry(void) {
1421 panic("tarval_carry() requetsed: not implemented on all operations");
1422 return sc_had_carry();
1428 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1429 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1433 const tarval_mode_info *mode_info;
1434 const char *prefix, *suffix;
1436 mode_info = tv->mode->tv_priv;
1438 mode_info = &default_info;
1439 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1440 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1442 switch (get_mode_sort(tv->mode)) {
1443 case irms_reference:
1444 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1446 case irms_int_number:
1447 switch (mode_info->mode_output) {
1450 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1454 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1460 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1463 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1465 case irms_float_number:
1466 switch (mode_info->mode_output) {
1468 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1471 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1476 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1480 case irms_internal_boolean:
1481 switch (mode_info->mode_output) {
1487 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1491 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1494 case irms_control_flow:
1496 case irms_auxiliary:
1497 if (tv == tarval_bad)
1498 return snprintf(buf, len, "<TV_BAD>");
1499 if (tv == tarval_undefined)
1500 return snprintf(buf, len, "<TV_UNDEF>");
1501 if (tv == tarval_unreachable)
1502 return snprintf(buf, len, "<TV_UNREACHABLE>");
1503 if (tv == tarval_reachable)
1504 return snprintf(buf, len, "<TV_REACHABLE>");
1505 return snprintf(buf, len, "<TV_??""?>");
1512 * Output of tarvals to stdio.
1514 int tarval_printf(tarval *tv) {
1518 res = tarval_snprintf(buf, sizeof(buf), tv);
1519 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1524 char *get_tarval_bitpattern(tarval *tv) {
1526 int n = get_mode_size_bits(tv->mode);
1527 int bytes = (n + 7) / 8;
1528 char *res = XMALLOCN(char, n + 1);
1531 for(i = 0; i < bytes; i++) {
1532 byte = get_tarval_sub_bits(tv, i);
1533 for(j = 1; j < 256; j <<= 1)
1535 res[pos++] = j & byte ? '1' : '0';
1544 * access to the bitpattern
1546 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1547 switch (get_mode_arithmetic(tv->mode)) {
1548 case irma_twos_complement:
1549 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1551 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1553 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1558 * Specify the output options of one mode.
1560 * This functions stores the modinfo, so DO NOT DESTROY it.
1562 * Returns zero on success.
1564 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1567 mode->tv_priv = modeinfo;
1572 * Returns the output options of one mode.
1574 * This functions returns the mode info of a given mode.
1576 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1579 return mode->tv_priv;
1583 * Returns non-zero if a given (integer) tarval has only one single bit
1586 int tarval_is_single_bit(tarval *tv) {
1590 if (!tv || tv == tarval_bad) return 0;
1591 if (! mode_is_int(tv->mode)) return 0;
1593 l = get_mode_size_bytes(tv->mode);
1594 for (bits = 0, i = l - 1; i >= 0; --i) {
1595 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1597 /* check for more than one bit in these */
1609 * Returns non-zero if the mantissa of a floating point IEEE-754
1610 * tarval is zero (i.e. 1.0Exxx)
1612 int tarval_ieee754_zero_mantissa(tarval *tv) {
1613 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1614 return fc_zero_mantissa(tv->value);
1617 /* Returns the exponent of a floating point IEEE-754 tarval. */
1618 int tarval_ieee754_get_exponent(tarval *tv) {
1619 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1620 return fc_get_exponent(tv->value);
1624 * Check if the tarval can be converted to the given mode without
1627 int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode) {
1628 const ieee_descriptor_t *desc = get_descriptor(mode);
1629 return fc_can_lossless_conv_to(tv->value, desc);
1632 /* Set the immediate precision for IEEE-754 results. */
1633 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1634 return fc_set_immediate_precision(bits);
1637 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1638 unsigned tarval_ieee754_get_exact(void) {
1639 return fc_is_exact();
1642 /* Return the size of the mantissa in bits (including possible
1643 implicit bits) for the given mode. */
1644 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode) {
1645 const ieee_descriptor_t *desc;
1647 assert(get_mode_arithmetic(mode) == irma_ieee754);
1648 desc = get_descriptor(mode);
1650 return desc->mantissa_size + desc->explicit_one;
1653 /* check if its the a floating point NaN */
1654 int tarval_is_NaN(tarval *tv) {
1655 if (! mode_is_float(tv->mode))
1657 return fc_is_nan(tv->value);
1660 /* check if its the a floating point +inf */
1661 int tarval_is_plus_inf(tarval *tv) {
1662 if (! mode_is_float(tv->mode))
1664 return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1667 /* check if its the a floating point -inf */
1668 int tarval_is_minus_inf(tarval *tv) {
1669 if (! mode_is_float(tv->mode))
1671 return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1674 /* check if the tarval represents a finite value */
1675 int tarval_is_finite(tarval *tv) {
1676 if (mode_is_float(tv->mode))
1677 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1682 * Sets the overflow mode for integer operations.
1684 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1685 int_overflow_mode = ov_mode;
1688 /* Get the overflow mode for integer operations. */
1689 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1690 return int_overflow_mode;
1693 /* Enable/Disable floating point constant folding. */
1694 int tarval_enable_fp_ops(int enable) {
1695 int old = !no_float;
1702 * default mode_info for output as HEX
1704 static const tarval_mode_info hex_output = {
1711 * Initialization of the tarval module: called before init_mode()
1713 void init_tarval_1(long null_value, int support_quad_precision) {
1714 /* if these assertion fail, tarval_is_constant() will follow ... */
1715 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1716 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1718 _null_value = null_value;
1720 /* initialize the sets holding the tarvals with a comparison function and
1721 * an initial size, which is the expected number of constants */
1722 tarvals = new_set(cmp_tv, N_CONSTANTS);
1723 values = new_set(memcmp, N_CONSTANTS);
1724 /* calls init_strcalc() with needed size */
1725 init_fltcalc(support_quad_precision ? 112 : 64);
1729 * Initialization of the tarval module: called after init_mode()
1731 void init_tarval_2(void) {
1732 tarval_bad->kind = k_tarval;
1733 tarval_bad->mode = mode_BAD;
1734 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1736 tarval_undefined->kind = k_tarval;
1737 tarval_undefined->mode = mode_ANY;
1738 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1740 tarval_b_true->kind = k_tarval;
1741 tarval_b_true->mode = mode_b;
1742 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1744 tarval_b_false->kind = k_tarval;
1745 tarval_b_false->mode = mode_b;
1746 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1748 tarval_unreachable->kind = k_tarval;
1749 tarval_unreachable->mode = mode_X;
1750 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1752 tarval_reachable->kind = k_tarval;
1753 tarval_reachable->mode = mode_X;
1754 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1757 * assign output modes that are compatible with the
1758 * old implementation: Hex output
1760 set_tarval_mode_output_option(mode_Bs, &hex_output);
1761 set_tarval_mode_output_option(mode_Bu, &hex_output);
1762 set_tarval_mode_output_option(mode_Hs, &hex_output);
1763 set_tarval_mode_output_option(mode_Hu, &hex_output);
1764 set_tarval_mode_output_option(mode_Is, &hex_output);
1765 set_tarval_mode_output_option(mode_Iu, &hex_output);
1766 set_tarval_mode_output_option(mode_Ls, &hex_output);
1767 set_tarval_mode_output_option(mode_Lu, &hex_output);
1768 set_tarval_mode_output_option(mode_P, &hex_output);
1771 /* free all memory occupied by tarval. */
1772 void finish_tarval(void) {
1775 del_set(tarvals); tarvals = NULL;
1776 del_set(values); values = NULL;
1779 int (is_tarval)(const void *thing) {
1780 return _is_tarval(thing);
1783 /****************************************************************************
1785 ****************************************************************************/