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.
36 #include <assert.h> /* assertions */
37 #include <stdlib.h> /* atoi() */
39 # include <string.h> /* nice things for strings */
42 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
49 #include "set.h" /* to store tarvals in */
50 #include "entity_t.h" /* needed to store pointers to entities */
52 #include "irnode.h" /* defines boolean return values (pnc_number)*/
57 #include "firm_common.h"
60 /** Size of hash tables. Should correspond to average number of distinct constant
62 #define N_CONSTANTS 2048
64 /* get the integer overflow mode */
65 #define GET_OVERFLOW_MODE() int_overflow_mode
67 /* unused, float to int doesn't work yet */
68 enum float_to_int_mode {
73 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
75 #define SWITCH_NOINFINITY 0
76 #define SWITCH_NODENORMALS 0
78 /****************************************************************************
79 * local definitions and macros
80 ****************************************************************************/
82 # define TARVAL_VERIFY(a) tarval_verify((a))
84 # define TARVAL_VERIFY(a) ((void)0)
87 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
88 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
90 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
91 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
93 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
95 /** A set containing all existing tarvals. */
96 static struct set *tarvals = NULL;
97 /** A set containing all existing values. */
98 static struct set *values = NULL;
100 /** The carry flag for SOME operations. -1 means UNDEFINED here */
101 static int carry_flag = -1;
103 /** The integer overflow mode. */
104 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
106 /** if this is set non-zero, the constant folding for floating point is OFF */
107 static int no_float = 0;
109 /** IEEE-754r half precision */
110 static const ieee_descriptor_t half_desc = { 5, 10, 0, NORMAL };
111 /** IEEE-754 single precision */
112 static const ieee_descriptor_t single_desc = { 8, 23, 0, NORMAL };
113 /** IEEE-754 double precision */
114 static const ieee_descriptor_t double_desc = { 11, 52, 0, NORMAL };
115 /** Intel x87 extended precision */
116 static const ieee_descriptor_t extended_desc = { 15, 63, 1, NORMAL };
118 /** IEEE-754r quad precision */
119 static const ieee_descriptor_t quad_desc = { 15, 112, 0, NORMAL };
121 /****************************************************************************
123 ****************************************************************************/
125 static int hash_val(const void *value, unsigned int length);
126 static int hash_tv(tarval *tv);
127 static void _fail_verify(tarval *tv, const char* file, int line)
129 /* print a memory image of the tarval and throw an assertion */
131 panic("%s:%d: Invalid tarval: mode: %F\n value: [%p]", file, line, tv->mode, tv->value);
133 panic("%s:%d: Invalid tarval (null)", file, line);
136 inline static void tarval_verify(tarval *tv) __attribute__ ((unused));
139 inline static void tarval_verify(tarval *tv)
145 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
146 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
148 if (!FIND_TARVAL(tv)) fail_verify(tv);
149 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
153 /** Hash a tarval. */
154 static int hash_tv(tarval *tv) {
155 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
158 /** Hash a value. Treat it as a byte array. */
159 static int hash_val(const void *value, unsigned int length) {
161 unsigned int hash = 0;
163 /* scramble the byte - array */
164 for (i = 0; i < length; ++i) {
165 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
166 hash += (hash << 11) ^ (hash >> 17);
172 static int cmp_tv(const void *p1, const void *p2, size_t n) {
173 const tarval *tv1 = p1;
174 const tarval *tv2 = p2;
177 assert(tv1->kind == k_tarval);
178 assert(tv2->kind == k_tarval);
179 if (tv1->mode < tv2->mode)
181 if (tv1->mode > tv2->mode)
183 if (tv1->length < tv2->length)
185 if (tv1->length > tv2->length)
187 if (tv1->value < tv2->value)
189 if (tv1->value > tv2->value)
195 /** finds tarval with value/mode or creates new tarval */
196 static tarval *get_tarval(const void *value, int length, ir_mode *mode) {
203 /* if there already is such a value, it is returned, else value
204 * is copied into the set */
205 char *temp = alloca(length);
206 memcpy(temp, value, length);
207 if (get_mode_arithmetic(mode) == irma_twos_complement) {
208 sign_extend(temp, mode);
210 tv.value = INSERT_VALUE(temp, length);
214 /* if there is such a tarval, it is returned, else tv is copied
216 return (tarval *)INSERT_TARVAL(&tv);
222 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
226 switch (get_mode_sort(mode)) {
228 /* addresses always wrap around */
229 temp = alloca(sc_get_buffer_length());
230 memcpy(temp, value, sc_get_buffer_length());
231 sc_truncate(get_mode_size_bits(mode), temp);
232 /* the sc_ module expects that all bits are set ... */
233 sign_extend(temp, mode);
234 return get_tarval(temp, length, mode);
236 case irms_int_number:
237 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
238 switch (GET_OVERFLOW_MODE()) {
239 case TV_OVERFLOW_SATURATE:
240 return get_mode_max(mode);
241 case TV_OVERFLOW_WRAP:
242 temp = alloca(sc_get_buffer_length());
243 memcpy(temp, value, sc_get_buffer_length());
244 sc_truncate(get_mode_size_bits(mode), temp);
245 /* the sc_ module expects that all bits are set ... */
246 sign_extend(temp, mode);
247 return get_tarval(temp, length, mode);
248 case TV_OVERFLOW_BAD:
251 return get_tarval(value, length, mode);
254 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
255 switch (GET_OVERFLOW_MODE()) {
256 case TV_OVERFLOW_SATURATE:
257 return get_mode_min(mode);
258 case TV_OVERFLOW_WRAP: {
259 char *temp = alloca(sc_get_buffer_length());
260 memcpy(temp, value, sc_get_buffer_length());
261 sc_truncate(get_mode_size_bits(mode), temp);
262 return get_tarval(temp, length, mode);
264 case TV_OVERFLOW_BAD:
267 return get_tarval(value, length, mode);
272 case irms_float_number:
273 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
274 /* clip infinity to maximum value */
275 return fc_is_negative(value) ? get_mode_min(mode) : get_mode_max(mode);
278 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
279 /* clip denormals to zero */
280 return get_mode_null(mode);
287 return get_tarval(value, length, mode);
291 * public variables declared in tv.h
293 static tarval reserved_tv[6];
295 tarval *tarval_b_false = &reserved_tv[0];
296 tarval *tarval_b_true = &reserved_tv[1];
297 tarval *tarval_bad = &reserved_tv[2];
298 tarval *tarval_undefined = &reserved_tv[3];
299 tarval *tarval_reachable = &reserved_tv[4];
300 tarval *tarval_unreachable = &reserved_tv[5];
303 * get the float descriptor for given mode.
305 static const ieee_descriptor_t *get_descriptor(const ir_mode *mode) {
306 switch (get_mode_size_bits(mode)) {
307 case 16: return &half_desc;
308 case 32: return &single_desc;
309 case 64: return &double_desc;
311 case 96: return &extended_desc;
312 case 128: return &quad_desc;
314 panic("Unsupported mode in get_descriptor()");
320 * public functions declared in tv.h
324 * Constructors =============================================================
326 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
328 const ieee_descriptor_t *desc;
334 switch (get_mode_sort(mode)) {
335 case irms_control_flow:
338 panic("Unsupported tarval creation with mode %F", mode);
340 case irms_internal_boolean:
341 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
342 if (strcasecmp(str, "true"))
343 return tarval_b_true;
344 else if (strcasecmp(str, "false"))
345 return tarval_b_true;
347 /* XXX This is C semantics */
348 return atoi(str) ? tarval_b_true : tarval_b_false;
350 case irms_float_number:
351 desc = get_descriptor(mode);
352 fc_val_from_str(str, len, desc, NULL);
353 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
356 /* same as integer modes */
357 case irms_int_number:
358 sc_val_from_str(str, len, NULL, mode);
359 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
361 panic("Unsupported tarval creation with mode %F", mode);
365 * helper function, create a tarval from long
367 tarval *new_tarval_from_long(long l, ir_mode *mode) {
370 switch (get_mode_sort(mode)) {
371 case irms_internal_boolean:
372 /* XXX C semantics ! */
373 return l ? tarval_b_true : tarval_b_false ;
376 /* same as integer modes */
377 case irms_int_number:
378 sc_val_from_long(l, NULL);
379 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
381 case irms_float_number:
382 return new_tarval_from_double((long double)l, mode);
385 assert(0 && "unsupported mode sort");
390 /* returns non-zero if can be converted to long */
391 int tarval_is_long(tarval *tv) {
392 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
395 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
396 /* the value might be too big to fit in a long */
397 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
398 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
399 /* really doesn't fit */
406 /* this might overflow the machine's long, so use only with small values */
407 long get_tarval_long(tarval* tv) {
408 assert(tarval_is_long(tv) && "tarval too big to fit in long");
410 return sc_val_to_long(tv->value);
413 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
414 const ieee_descriptor_t *desc;
416 assert(mode && (get_mode_sort(mode) == irms_float_number));
417 desc = get_descriptor(mode);
418 fc_val_from_ieee754(d, desc, NULL);
419 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
422 /* returns non-zero if can be converted to double */
423 int tarval_is_double(tarval *tv) {
426 return (get_mode_sort(tv->mode) == irms_float_number);
429 long double get_tarval_double(tarval *tv) {
430 assert(tarval_is_double(tv));
432 return fc_val_to_ieee754(tv->value);
437 * Access routines for tarval fields ========================================
440 /* get the mode of the tarval */
441 ir_mode *(get_tarval_mode)(const tarval *tv) {
442 return _get_tarval_mode(tv);
446 * Special value query functions ============================================
448 * These functions calculate and return a tarval representing the requested
450 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
451 * functions, but these are stored on initialization of the irmode module and
452 * therefore the irmode functions should be preferred to the functions below.
455 tarval *(get_tarval_bad)(void) {
456 return _get_tarval_bad();
459 tarval *(get_tarval_undefined)(void) {
460 return _get_tarval_undefined();
463 tarval *(get_tarval_b_false)(void) {
464 return _get_tarval_b_false();
467 tarval *(get_tarval_b_true)(void) {
468 return _get_tarval_b_true();
471 tarval *(get_tarval_reachable)(void) {
472 return _get_tarval_reachable();
475 tarval *(get_tarval_unreachable)(void) {
476 return _get_tarval_unreachable();
479 tarval *get_tarval_max(ir_mode *mode) {
480 const ieee_descriptor_t *desc;
483 if (get_mode_n_vector_elems(mode) > 1) {
484 /* vector arithmetic not implemented yet */
488 switch (get_mode_sort(mode)) {
489 case irms_control_flow:
492 panic("mode %F does not support maximum value", mode);
494 case irms_internal_boolean:
495 return tarval_b_true;
497 case irms_float_number:
498 desc = get_descriptor(mode);
499 fc_get_max(desc, NULL);
500 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
503 case irms_int_number:
504 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
505 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
510 tarval *get_tarval_min(ir_mode *mode) {
511 const ieee_descriptor_t *desc;
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 panic("mode %F does not support minimum value", mode);
525 case irms_internal_boolean:
526 return tarval_b_false;
528 case irms_float_number:
529 desc = get_descriptor(mode);
530 fc_get_min(desc, NULL);
531 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
534 case irms_int_number:
535 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
536 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
541 /** The bit pattern for the pointer NULL */
542 static long _null_value = 0;
544 tarval *get_tarval_null(ir_mode *mode) {
547 if (get_mode_n_vector_elems(mode) > 1) {
548 /* vector arithmetic not implemented yet */
552 switch (get_mode_sort(mode)) {
553 case irms_control_flow:
556 panic("mode %F does not support null value", mode);
558 case irms_float_number:
559 return new_tarval_from_double(0.0, mode);
561 case irms_internal_boolean:
562 case irms_int_number:
563 return new_tarval_from_long(0l, mode);
566 return new_tarval_from_long(_null_value, mode);
571 tarval *get_tarval_one(ir_mode *mode) {
574 if (get_mode_n_vector_elems(mode) > 1)
575 panic("vector arithmetic not implemented yet");
577 switch (get_mode_sort(mode)) {
578 case irms_control_flow:
581 panic("mode %F does not support one value", mode);
583 case irms_internal_boolean:
584 return tarval_b_true;
586 case irms_float_number:
587 return new_tarval_from_double(1.0, mode);
590 case irms_int_number:
591 return new_tarval_from_long(1l, mode);
596 tarval *get_tarval_all_one(ir_mode *mode) {
599 if (get_mode_n_vector_elems(mode) > 1)
600 panic("vector arithmetic not implemented yet");
602 switch (get_mode_sort(mode)) {
603 case irms_control_flow:
606 panic("mode %F does not support all-one value", mode);
608 case irms_int_number:
609 case irms_internal_boolean:
611 return tarval_not(get_mode_null(mode));
614 case irms_float_number:
615 return new_tarval_from_double(1.0, mode);
620 int tarval_is_constant(tarval *tv) {
621 int num_res = sizeof(reserved_tv) / sizeof(reserved_tv[0]);
623 /* reserved tarvals are NOT constants. Note that although
624 tarval_b_true and tarval_b_false are reserved, they are constants of course. */
625 return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
628 tarval *get_tarval_minus_one(ir_mode *mode) {
631 if (get_mode_n_vector_elems(mode) > 1)
632 panic("vector arithmetic not implemented yet");
634 switch (get_mode_sort(mode)) {
635 case irms_control_flow:
638 case irms_internal_boolean:
639 panic("mode %F does not support minus one value", mode);
644 case irms_float_number:
645 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
647 case irms_int_number:
648 return new_tarval_from_long(-1l, mode);
653 tarval *get_tarval_nan(ir_mode *mode) {
654 const ieee_descriptor_t *desc;
657 if (get_mode_n_vector_elems(mode) > 1)
658 panic("vector arithmetic not implemented yet");
660 if (get_mode_sort(mode) == irms_float_number) {
661 desc = get_descriptor(mode);
662 fc_get_qnan(desc, NULL);
663 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
665 panic("mode %F does not support NaN value", mode);
668 tarval *get_tarval_plus_inf(ir_mode *mode) {
670 if (get_mode_n_vector_elems(mode) > 1)
671 panic("vector arithmetic not implemented yet");
673 if (get_mode_sort(mode) == irms_float_number) {
674 const ieee_descriptor_t *desc = get_descriptor(mode);
675 fc_get_plusinf(desc, NULL);
676 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
678 panic("mode %F does not support +inf value", mode);
681 tarval *get_tarval_minus_inf(ir_mode *mode) {
684 if (get_mode_n_vector_elems(mode) > 1)
685 panic("vector arithmetic not implemented yet");
687 if (get_mode_sort(mode) == irms_float_number) {
688 const ieee_descriptor_t *desc = get_descriptor(mode);
689 fc_get_minusinf(desc, NULL);
690 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
692 panic("mode %F does not support -inf value", mode);
696 * Arithmetic operations on tarvals ========================================
700 * test if negative number, 1 means 'yes'
702 int tarval_is_negative(tarval *a) {
703 if (get_mode_n_vector_elems(a->mode) > 1)
704 panic("vector arithmetic not implemented yet");
706 switch (get_mode_sort(a->mode)) {
707 case irms_int_number:
708 if (!mode_is_signed(a->mode)) return 0;
710 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
712 case irms_float_number:
713 return fc_is_negative(a->value);
716 panic("mode %F does not support negation value", a->mode);
721 * test if null, 1 means 'yes'
723 int tarval_is_null(tarval *a) {
726 a == get_mode_null(get_tarval_mode(a));
730 * test if one, 1 means 'yes'
732 int tarval_is_one(tarval *a) {
735 a == get_mode_one(get_tarval_mode(a));
738 int tarval_is_all_one(tarval *tv) {
741 tv == get_mode_all_one(get_tarval_mode(tv));
745 * test if one, 1 means 'yes'
747 int tarval_is_minus_one(tarval *a) {
750 a == get_mode_minus_one(get_tarval_mode(a));
756 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
759 if (a == tarval_bad || b == tarval_bad) {
760 panic("Comparison with tarval_bad");
764 if (a == tarval_undefined || b == tarval_undefined)
767 if (a->mode != b->mode)
770 if (get_mode_n_vector_elems(a->mode) > 1) {
771 /* vector arithmetic not implemented yet */
772 assert(0 && "cmp not implemented for vector modes");
775 /* Here the two tarvals are unequal and of the same mode */
776 switch (get_mode_sort(a->mode)) {
777 case irms_control_flow:
784 case irms_float_number:
785 /* it should be safe to enable this even if other arithmetic is disabled */
787 return pn_Cmp_False;*/
789 * BEWARE: we cannot compare a == b here, because
790 * a NaN is always Unordered to any other value, even to itself!
792 switch (fc_comp(a->value, b->value)) {
793 case -1: return pn_Cmp_Lt;
794 case 0: return pn_Cmp_Eq;
795 case 1: return pn_Cmp_Gt;
796 case 2: return pn_Cmp_Uo;
797 default: return pn_Cmp_False;
800 case irms_int_number:
803 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
805 case irms_internal_boolean:
808 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
814 * convert to other mode
816 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
819 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) {
961 assert(mode_is_num(a->mode)); /* negation only for numerical values */
965 /* note: negation is allowed even for unsigned modes. */
967 if (get_mode_n_vector_elems(a->mode) > 1) {
968 /* vector arithmetic not implemented yet */
972 switch (get_mode_sort(a->mode)) {
973 case irms_int_number:
974 buffer = alloca(sc_get_buffer_length());
975 sc_neg(a->value, buffer);
976 return get_tarval_overflow(buffer, a->length, a->mode);
978 case irms_float_number:
979 /* it should be safe to enable this even if other arithmetic is disabled */
983 fc_neg(a->value, NULL);
984 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
994 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 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1019 return get_tarval_overflow(buffer, a->length, a->mode);
1021 case irms_float_number:
1025 fc_add(a->value, b->value, NULL);
1026 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1036 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 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1061 return get_tarval_overflow(buffer, a->length, a->mode);
1063 case irms_float_number:
1067 fc_sub(a->value, b->value, NULL);
1068 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1078 tarval *tarval_mul(tarval *a, tarval *b) {
1081 assert(a->mode == b->mode);
1085 if (get_mode_n_vector_elems(a->mode) > 1) {
1086 /* vector arithmetic not implemented yet */
1090 switch (get_mode_sort(a->mode)) {
1091 case irms_int_number:
1092 /* modes of a,b are equal */
1093 buffer = alloca(sc_get_buffer_length());
1094 sc_mul(a->value, b->value, buffer);
1095 return get_tarval_overflow(buffer, a->length, a->mode);
1097 case irms_float_number:
1101 fc_mul(a->value, b->value, NULL);
1102 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1110 * floating point division
1112 tarval *tarval_quo(tarval *a, tarval *b) {
1113 assert((a->mode == b->mode) && mode_is_float(a->mode));
1120 if (get_mode_n_vector_elems(a->mode) > 1) {
1121 /* vector arithmetic not implemented yet */
1125 fc_div(a->value, b->value, NULL);
1126 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1131 * overflow is impossible, but look out for division by zero
1133 tarval *tarval_div(tarval *a, tarval *b) {
1134 assert((a->mode == b->mode) && mode_is_int(a->mode));
1138 if (get_mode_n_vector_elems(a->mode) > 1) {
1139 /* vector arithmetic not implemented yet */
1144 if (b == get_mode_null(b->mode)) return tarval_bad;
1145 /* modes of a,b are equal */
1146 sc_div(a->value, b->value, NULL);
1147 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1152 * overflow is impossible, but look out for division by zero
1154 tarval *tarval_mod(tarval *a, tarval *b) {
1155 assert((a->mode == b->mode) && mode_is_int(a->mode));
1159 if (get_mode_n_vector_elems(a->mode) > 1) {
1160 /* vector arithmetic not implemented yet */
1165 if (b == get_mode_null(b->mode)) return tarval_bad;
1166 /* modes of a,b are equal */
1167 sc_mod(a->value, b->value, NULL);
1168 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1172 * integer division AND remainder
1173 * overflow is impossible, but look out for division by zero
1175 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1176 int len = sc_get_buffer_length();
1177 char *div_res = alloca(len);
1178 char *mod_res = alloca(len);
1180 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 */
1191 if (b == get_mode_null(b->mode)) return tarval_bad;
1192 /* modes of a,b are equal */
1193 sc_divmod(a->value, b->value, div_res, mod_res);
1194 *mod = get_tarval(mod_res, len, a->mode);
1195 return get_tarval(div_res, len, a->mode);
1201 tarval *tarval_abs(tarval *a) {
1205 assert(mode_is_num(a->mode));
1207 if (get_mode_n_vector_elems(a->mode) > 1) {
1208 /* vector arithmetic not implemented yet */
1212 switch (get_mode_sort(a->mode)) {
1213 case irms_int_number:
1214 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1215 buffer = alloca(sc_get_buffer_length());
1216 sc_neg(a->value, buffer);
1217 return get_tarval_overflow(buffer, a->length, a->mode);
1221 case irms_float_number:
1222 /* it should be safe to enable this even if other arithmetic is disabled */
1224 return tarval_bad;*/
1226 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1227 fc_neg(a->value, NULL);
1228 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1241 tarval *tarval_and(tarval *a, tarval *b) {
1242 assert(a->mode == b->mode);
1244 /* 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) {
1265 assert(a->mode == b->mode);
1267 /* works even for vector modes */
1270 switch (get_mode_sort(a->mode)) {
1271 case irms_internal_boolean:
1272 return (a == tarval_b_true) ? a : b;
1274 case irms_int_number:
1275 sc_or(a->value, b->value, NULL);
1276 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1279 assert(0 && "operation not defined on mode");
1285 * bitwise exclusive or (xor)
1287 tarval *tarval_eor(tarval *a, tarval *b) {
1288 assert((a->mode == b->mode));
1290 /* works even for vector modes */
1293 switch (get_mode_sort(a->mode)) {
1294 case irms_internal_boolean:
1295 return (a == b)? tarval_b_false : tarval_b_true;
1297 case irms_int_number:
1298 sc_xor(a->value, b->value, NULL);
1299 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1302 assert(0 && "operation not defined on mode");
1308 * bitwise left shift
1310 tarval *tarval_shl(tarval *a, tarval *b) {
1311 char *temp_val = NULL;
1313 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1317 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1318 /* vector arithmetic not implemented yet */
1322 if (get_mode_modulo_shift(a->mode) != 0) {
1323 temp_val = alloca(sc_get_buffer_length());
1325 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1326 sc_mod(b->value, temp_val, temp_val);
1328 temp_val = (char*)b->value;
1330 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1331 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1335 * bitwise unsigned right shift
1337 tarval *tarval_shr(tarval *a, tarval *b) {
1338 char *temp_val = NULL;
1340 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1344 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1345 /* vector arithmetic not implemented yet */
1349 if (get_mode_modulo_shift(a->mode) != 0) {
1350 temp_val = alloca(sc_get_buffer_length());
1352 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1353 sc_mod(b->value, temp_val, temp_val);
1355 temp_val = (char*)b->value;
1357 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1358 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1362 * bitwise signed right shift
1364 tarval *tarval_shrs(tarval *a, tarval *b) {
1365 char *temp_val = NULL;
1367 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1371 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1372 /* vector arithmetic not implemented yet */
1376 if (get_mode_modulo_shift(a->mode) != 0) {
1377 temp_val = alloca(sc_get_buffer_length());
1379 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1380 sc_mod(b->value, temp_val, temp_val);
1382 temp_val = (char*)b->value;
1384 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1385 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1389 * bitwise rotation to left
1391 tarval *tarval_rotl(tarval *a, tarval *b) {
1392 char *temp_val = NULL;
1394 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1398 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1399 /* vector arithmetic not implemented yet */
1403 if (get_mode_modulo_shift(a->mode) != 0) {
1404 temp_val = alloca(sc_get_buffer_length());
1406 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1407 sc_mod(b->value, temp_val, temp_val);
1409 temp_val = (char*)b->value;
1411 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1412 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1416 * carry flag of the last operation
1418 int tarval_carry(void) {
1419 if (carry_flag == -1)
1420 panic("Carry undefined for the last operation");
1427 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1428 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1432 const tarval_mode_info *mode_info;
1433 const char *prefix, *suffix;
1435 mode_info = tv->mode->tv_priv;
1437 mode_info = &default_info;
1438 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1439 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1441 switch (get_mode_sort(tv->mode)) {
1442 case irms_reference:
1443 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1445 case irms_int_number:
1446 switch (mode_info->mode_output) {
1449 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1453 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1459 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1462 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1464 case irms_float_number:
1465 switch (mode_info->mode_output) {
1467 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1470 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1475 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1479 case irms_internal_boolean:
1480 switch (mode_info->mode_output) {
1486 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1490 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1493 case irms_control_flow:
1495 case irms_auxiliary:
1496 if (tv == tarval_bad)
1497 return snprintf(buf, len, "<TV_BAD>");
1498 if (tv == tarval_undefined)
1499 return snprintf(buf, len, "<TV_UNDEF>");
1500 if (tv == tarval_unreachable)
1501 return snprintf(buf, len, "<TV_UNREACHABLE>");
1502 if (tv == tarval_reachable)
1503 return snprintf(buf, len, "<TV_REACHABLE>");
1504 return snprintf(buf, len, "<TV_??""?>");
1511 * Output of tarvals to stdio.
1513 int tarval_printf(tarval *tv) {
1517 res = tarval_snprintf(buf, sizeof(buf), tv);
1518 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1523 char *get_tarval_bitpattern(tarval *tv) {
1525 int n = get_mode_size_bits(tv->mode);
1526 int bytes = (n + 7) / 8;
1527 char *res = XMALLOCN(char, n + 1);
1530 for(i = 0; i < bytes; i++) {
1531 byte = get_tarval_sub_bits(tv, i);
1532 for(j = 1; j < 256; j <<= 1)
1534 res[pos++] = j & byte ? '1' : '0';
1543 * access to the bitpattern
1545 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1546 switch (get_mode_arithmetic(tv->mode)) {
1547 case irma_twos_complement:
1548 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1550 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1552 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1557 * Specify the output options of one mode.
1559 * This functions stores the modinfo, so DO NOT DESTROY it.
1561 * Returns zero on success.
1563 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1566 mode->tv_priv = modeinfo;
1571 * Returns the output options of one mode.
1573 * This functions returns the mode info of a given mode.
1575 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1578 return mode->tv_priv;
1582 * Returns non-zero if a given (integer) tarval has only one single bit
1585 int tarval_is_single_bit(tarval *tv) {
1589 if (!tv || tv == tarval_bad) return 0;
1590 if (! mode_is_int(tv->mode)) return 0;
1592 l = get_mode_size_bytes(tv->mode);
1593 for (bits = 0, i = l - 1; i >= 0; --i) {
1594 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1596 /* check for more than one bit in these */
1608 * Returns non-zero if the mantissa of a floating point IEEE-754
1609 * tarval is zero (i.e. 1.0Exxx)
1611 int tarval_ieee754_zero_mantissa(tarval *tv) {
1612 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1613 return fc_zero_mantissa(tv->value);
1616 /* Returns the exponent of a floating point IEEE-754 tarval. */
1617 int tarval_ieee754_get_exponent(tarval *tv) {
1618 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1619 return fc_get_exponent(tv->value);
1623 * Check if the tarval can be converted to the given mode without
1626 int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode) {
1627 const ieee_descriptor_t *desc = get_descriptor(mode);
1628 return fc_can_lossless_conv_to(tv->value, desc);
1631 /* Set the immediate precision for IEEE-754 results. */
1632 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1633 return fc_set_immediate_precision(bits);
1636 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1637 unsigned tarval_ieee754_get_exact(void) {
1638 return fc_is_exact();
1641 /* Return the size of the mantissa in bits (including possible
1642 implicit bits) for the given mode. */
1643 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode) {
1644 const ieee_descriptor_t *desc;
1646 assert(get_mode_arithmetic(mode) == irma_ieee754);
1647 desc = get_descriptor(mode);
1649 return desc->mantissa_size + desc->explicit_one;
1652 /* check if its the a floating point NaN */
1653 int tarval_is_NaN(tarval *tv) {
1654 if (! mode_is_float(tv->mode))
1656 return fc_is_nan(tv->value);
1659 /* check if its the a floating point +inf */
1660 int tarval_is_plus_inf(tarval *tv) {
1661 if (! mode_is_float(tv->mode))
1663 return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1666 /* check if its the a floating point -inf */
1667 int tarval_is_minus_inf(tarval *tv) {
1668 if (! mode_is_float(tv->mode))
1670 return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1673 /* check if the tarval represents a finite value */
1674 int tarval_is_finite(tarval *tv) {
1675 if (mode_is_float(tv->mode))
1676 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1681 * Sets the overflow mode for integer operations.
1683 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1684 int_overflow_mode = ov_mode;
1687 /* Get the overflow mode for integer operations. */
1688 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1689 return int_overflow_mode;
1692 /* Enable/Disable floating point constant folding. */
1693 int tarval_enable_fp_ops(int enable) {
1694 int old = !no_float;
1701 * default mode_info for output as HEX
1703 static const tarval_mode_info hex_output = {
1710 * Initialization of the tarval module: called before init_mode()
1712 void init_tarval_1(long null_value, int support_quad_precision) {
1713 /* if these assertion fail, tarval_is_constant() will follow ... */
1714 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1715 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1717 _null_value = null_value;
1719 /* initialize the sets holding the tarvals with a comparison function and
1720 * an initial size, which is the expected number of constants */
1721 tarvals = new_set(cmp_tv, N_CONSTANTS);
1722 values = new_set(memcmp, N_CONSTANTS);
1723 /* calls init_strcalc() with needed size */
1724 init_fltcalc(support_quad_precision ? 112 : 64);
1728 * Initialization of the tarval module: called after init_mode()
1730 void init_tarval_2(void) {
1731 tarval_bad->kind = k_tarval;
1732 tarval_bad->mode = mode_BAD;
1733 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1735 tarval_undefined->kind = k_tarval;
1736 tarval_undefined->mode = mode_ANY;
1737 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1739 tarval_b_true->kind = k_tarval;
1740 tarval_b_true->mode = mode_b;
1741 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1743 tarval_b_false->kind = k_tarval;
1744 tarval_b_false->mode = mode_b;
1745 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1747 tarval_unreachable->kind = k_tarval;
1748 tarval_unreachable->mode = mode_X;
1749 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1751 tarval_reachable->kind = k_tarval;
1752 tarval_reachable->mode = mode_X;
1753 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1756 * assign output modes that are compatible with the
1757 * old implementation: Hex output
1759 set_tarval_mode_output_option(mode_Bs, &hex_output);
1760 set_tarval_mode_output_option(mode_Bu, &hex_output);
1761 set_tarval_mode_output_option(mode_Hs, &hex_output);
1762 set_tarval_mode_output_option(mode_Hu, &hex_output);
1763 set_tarval_mode_output_option(mode_Is, &hex_output);
1764 set_tarval_mode_output_option(mode_Iu, &hex_output);
1765 set_tarval_mode_output_option(mode_Ls, &hex_output);
1766 set_tarval_mode_output_option(mode_Lu, &hex_output);
1767 set_tarval_mode_output_option(mode_P, &hex_output);
1770 /* free all memory occupied by tarval. */
1771 void finish_tarval(void) {
1774 del_set(tarvals); tarvals = NULL;
1775 del_set(values); values = NULL;
1778 int (is_tarval)(const void *thing) {
1779 return _is_tarval(thing);
1782 /****************************************************************************
1784 ****************************************************************************/