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 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
132 printf("%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:
341 case irms_internal_boolean:
342 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
343 if (strcasecmp(str, "true"))
344 return tarval_b_true;
345 else if (strcasecmp(str, "false"))
346 return tarval_b_true;
348 /* XXX This is C semantics */
349 return atoi(str) ? tarval_b_true : tarval_b_false;
351 case irms_float_number:
352 desc = get_descriptor(mode);
353 fc_val_from_str(str, len, desc, NULL);
354 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
357 /* same as integer modes */
358 case irms_int_number:
359 sc_val_from_str(str, len, NULL, mode);
360 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
363 assert(0); /* can't be reached, can it? */
368 * helper function, create a tarval from long
370 tarval *new_tarval_from_long(long l, ir_mode *mode) {
373 switch (get_mode_sort(mode)) {
374 case irms_internal_boolean:
375 /* XXX C semantics ! */
376 return l ? tarval_b_true : tarval_b_false ;
379 /* same as integer modes */
380 case irms_int_number:
381 sc_val_from_long(l, NULL);
382 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
384 case irms_float_number:
385 return new_tarval_from_double((long double)l, mode);
388 assert(0 && "unsupported mode sort");
393 /* returns non-zero if can be converted to long */
394 int tarval_is_long(tarval *tv) {
395 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
398 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
399 /* the value might be too big to fit in a long */
400 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
401 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
402 /* really doesn't fit */
409 /* this might overflow the machine's long, so use only with small values */
410 long get_tarval_long(tarval* tv) {
411 assert(tarval_is_long(tv) && "tarval too big to fit in long");
413 return sc_val_to_long(tv->value);
416 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
417 const ieee_descriptor_t *desc;
419 assert(mode && (get_mode_sort(mode) == irms_float_number));
420 desc = get_descriptor(mode);
421 fc_val_from_ieee754(d, desc, NULL);
422 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
425 /* returns non-zero if can be converted to double */
426 int tarval_is_double(tarval *tv) {
429 return (get_mode_sort(tv->mode) == irms_float_number);
432 long double get_tarval_double(tarval *tv) {
433 assert(tarval_is_double(tv));
435 return fc_val_to_ieee754(tv->value);
440 * Access routines for tarval fields ========================================
443 /* get the mode of the tarval */
444 ir_mode *(get_tarval_mode)(const tarval *tv) {
445 return _get_tarval_mode(tv);
449 * Special value query functions ============================================
451 * These functions calculate and return a tarval representing the requested
453 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
454 * functions, but these are stored on initialization of the irmode module and
455 * therefore the irmode functions should be preferred to the functions below.
458 tarval *(get_tarval_bad)(void) {
459 return _get_tarval_bad();
462 tarval *(get_tarval_undefined)(void) {
463 return _get_tarval_undefined();
466 tarval *(get_tarval_b_false)(void) {
467 return _get_tarval_b_false();
470 tarval *(get_tarval_b_true)(void) {
471 return _get_tarval_b_true();
474 tarval *(get_tarval_reachable)(void) {
475 return _get_tarval_reachable();
478 tarval *(get_tarval_unreachable)(void) {
479 return _get_tarval_unreachable();
482 tarval *get_tarval_max(ir_mode *mode) {
483 const ieee_descriptor_t *desc;
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_true;
501 case irms_float_number:
502 desc = get_descriptor(mode);
503 fc_get_max(desc, NULL);
504 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
507 case irms_int_number:
508 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
509 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
514 tarval *get_tarval_min(ir_mode *mode) {
515 const ieee_descriptor_t *desc;
518 if (get_mode_n_vector_elems(mode) > 1) {
519 /* vector arithmetic not implemented yet */
523 switch (get_mode_sort(mode)) {
524 case irms_control_flow:
530 case irms_internal_boolean:
531 return tarval_b_false;
533 case irms_float_number:
534 desc = get_descriptor(mode);
535 fc_get_min(desc, NULL);
536 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
539 case irms_int_number:
540 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
541 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
546 /** The bit pattern for the pointer NULL */
547 static long _null_value = 0;
549 tarval *get_tarval_null(ir_mode *mode) {
552 if (get_mode_n_vector_elems(mode) > 1) {
553 /* vector arithmetic not implemented yet */
557 switch (get_mode_sort(mode)) {
558 case irms_control_flow:
564 case irms_float_number:
565 return new_tarval_from_double(0.0, mode);
567 case irms_internal_boolean:
568 case irms_int_number:
569 return new_tarval_from_long(0l, mode);
572 return new_tarval_from_long(_null_value, mode);
577 tarval *get_tarval_one(ir_mode *mode) {
580 if (get_mode_n_vector_elems(mode) > 1) {
581 /* vector arithmetic not implemented yet */
586 switch (get_mode_sort(mode)) {
587 case irms_control_flow:
593 case irms_internal_boolean:
594 return tarval_b_true;
596 case irms_float_number:
597 return new_tarval_from_double(1.0, mode);
600 case irms_int_number:
601 return new_tarval_from_long(1l, mode);
606 tarval *get_tarval_all_one(ir_mode *mode) {
609 if (get_mode_n_vector_elems(mode) > 1) {
610 /* vector arithmetic not implemented yet */
615 switch (get_mode_sort(mode)) {
616 case irms_control_flow:
622 case irms_int_number:
623 case irms_internal_boolean:
625 return tarval_not(get_mode_null(mode));
628 case irms_float_number:
629 return new_tarval_from_double(1.0, mode);
634 int tarval_is_constant(tarval *tv) {
635 int num_res = sizeof(reserved_tv) / sizeof(reserved_tv[0]);
637 /* reserved tarvals are NOT constants. Note that although
638 tarval_b_true and tarval_b_false are reserved, they are constants of course. */
639 return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
642 tarval *get_tarval_minus_one(ir_mode *mode) {
645 if (get_mode_n_vector_elems(mode) > 1) {
646 /* vector arithmetic not implemented yet */
650 switch (get_mode_sort(mode)) {
651 case irms_control_flow:
654 case irms_internal_boolean:
661 case irms_float_number:
662 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
664 case irms_int_number:
665 return new_tarval_from_long(-1l, mode);
670 tarval *get_tarval_nan(ir_mode *mode) {
671 const ieee_descriptor_t *desc;
674 if (get_mode_n_vector_elems(mode) > 1) {
675 /* vector arithmetic not implemented yet */
679 if (get_mode_sort(mode) == irms_float_number) {
680 desc = get_descriptor(mode);
681 fc_get_qnan(desc, NULL);
682 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
684 assert(0 && "tarval is not floating point");
689 tarval *get_tarval_plus_inf(ir_mode *mode) {
691 if (get_mode_n_vector_elems(mode) > 1) {
692 /* vector arithmetic not implemented yet */
696 if (get_mode_sort(mode) == irms_float_number) {
697 const ieee_descriptor_t *desc = get_descriptor(mode);
698 fc_get_plusinf(desc, NULL);
699 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
701 assert(0 && "tarval is not floating point");
706 tarval *get_tarval_minus_inf(ir_mode *mode) {
709 if (get_mode_n_vector_elems(mode) > 1) {
710 /* vector arithmetic not implemented yet */
714 if (get_mode_sort(mode) == irms_float_number) {
715 const ieee_descriptor_t *desc = get_descriptor(mode);
716 fc_get_minusinf(desc, NULL);
717 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
719 assert(0 && "tarval is not floating point");
725 * Arithmethic operations on tarvals ========================================
729 * test if negative number, 1 means 'yes'
731 int tarval_is_negative(tarval *a) {
734 if (get_mode_n_vector_elems(a->mode) > 1) {
735 /* vector arithmetic not implemented yet */
736 assert(0 && "tarval_is_negative is not allowed for vector modes");
740 switch (get_mode_sort(a->mode)) {
741 case irms_int_number:
742 if (!mode_is_signed(a->mode)) return 0;
744 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
746 case irms_float_number:
747 return fc_is_negative(a->value);
750 assert(0 && "not implemented");
756 * test if null, 1 means 'yes'
758 int tarval_is_null(tarval *a) {
761 a == get_mode_null(get_tarval_mode(a));
765 * test if one, 1 means 'yes'
767 int tarval_is_one(tarval *a) {
770 a == get_mode_one(get_tarval_mode(a));
773 int tarval_is_all_one(tarval *tv) {
776 tv == get_mode_all_one(get_tarval_mode(tv));
780 * test if one, 1 means 'yes'
782 int tarval_is_minus_one(tarval *a) {
785 a == get_mode_minus_one(get_tarval_mode(a));
791 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
795 if (a == tarval_bad || b == tarval_bad) {
796 assert(0 && "Comparison with tarval_bad");
800 if (a == tarval_undefined || b == tarval_undefined)
803 if (a->mode != b->mode)
806 if (get_mode_n_vector_elems(a->mode) > 1) {
807 /* vector arithmetic not implemented yet */
808 assert(0 && "cmp not implemented for vector modes");
811 /* Here the two tarvals are unequal and of the same mode */
812 switch (get_mode_sort(a->mode)) {
813 case irms_control_flow:
820 case irms_float_number:
821 /* it should be safe to enable this even if other arithmetic is disabled */
823 return pn_Cmp_False;*/
825 * BEWARE: we cannot compare a == b here, because
826 * a NaN is always Unordered to any other value, even to itself!
828 switch (fc_comp(a->value, b->value)) {
829 case -1: return pn_Cmp_Lt;
830 case 0: return pn_Cmp_Eq;
831 case 1: return pn_Cmp_Gt;
832 case 2: return pn_Cmp_Uo;
833 default: return pn_Cmp_False;
836 case irms_int_number:
839 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
841 case irms_internal_boolean:
844 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
850 * convert to other mode
852 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
855 const ieee_descriptor_t *desc;
860 if (src->mode == dst_mode)
863 if (get_mode_n_vector_elems(src->mode) > 1) {
864 /* vector arithmetic not implemented yet */
868 switch (get_mode_sort(src->mode)) {
869 case irms_control_flow:
874 /* cast float to something */
875 case irms_float_number:
876 switch (get_mode_sort(dst_mode)) {
877 case irms_float_number:
878 desc = get_descriptor(dst_mode);
879 fc_cast(src->value, desc, NULL);
880 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
882 case irms_int_number:
883 switch (GET_FLOAT_TO_INT_MODE()) {
885 res = fc_int(src->value, NULL);
888 res = fc_rnd(src->value, NULL);
891 panic("Unsupported float to int conversion mode in tarval_convert_to()");
894 buffer = alloca(sc_get_buffer_length());
895 if (! fc_flt2int(res, buffer, dst_mode))
897 return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
900 /* the rest can't be converted */
905 /* cast int/characters to something */
906 case irms_int_number:
907 switch (get_mode_sort(dst_mode)) {
910 case irms_int_number:
911 buffer = alloca(sc_get_buffer_length());
912 memcpy(buffer, src->value, sc_get_buffer_length());
913 sign_extend(buffer, dst_mode);
914 return get_tarval_overflow(buffer, src->length, dst_mode);
916 case irms_internal_boolean:
917 /* XXX C semantics */
918 if (src == get_mode_null(src->mode)) return tarval_b_false;
919 else return tarval_b_true;
921 case irms_float_number:
922 /* XXX floating point unit does not understand internal integer
923 * representation, convert to string first, then create float from
925 buffer = alloca(100);
926 /* decimal string representation because hexadecimal output is
927 * interpreted unsigned by fc_val_from_str, so this is a HACK */
928 snprintf(buffer, 100, "%s",
929 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
930 buffer[100 - 1] = '\0';
931 desc = get_descriptor(dst_mode);
932 fc_val_from_str(buffer, 0, desc, NULL);
933 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
940 case irms_internal_boolean:
941 /* beware: this is C semantic for the INTERNAL boolean mode */
942 if (get_mode_sort(dst_mode) == irms_int_number)
943 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
947 if (get_mode_sort(dst_mode) == irms_int_number) {
948 buffer = alloca(sc_get_buffer_length());
949 memcpy(buffer, src->value, sc_get_buffer_length());
950 sign_extend(buffer, src->mode);
951 return get_tarval_overflow(buffer, src->length, dst_mode);
962 tarval *tarval_not(tarval *a) {
967 /* works for vector mode without changes */
969 switch (get_mode_sort(a->mode)) {
971 case irms_int_number:
972 buffer = alloca(sc_get_buffer_length());
973 sc_not(a->value, buffer);
974 return get_tarval(buffer, a->length, a->mode);
976 case irms_internal_boolean:
977 if (a == tarval_b_true)
978 return tarval_b_false;
979 if (a == tarval_b_false)
980 return tarval_b_true;
984 assert(0 && "bitwise negation is only allowed for integer and boolean");
990 * arithmetic negation
992 tarval *tarval_neg(tarval *a) {
996 assert(mode_is_num(a->mode)); /* negation only for numerical values */
998 /* note: negation is allowed even for unsigned modes. */
1000 if (get_mode_n_vector_elems(a->mode) > 1) {
1001 /* vector arithmetic not implemented yet */
1005 switch (get_mode_sort(a->mode)) {
1006 case irms_int_number:
1007 buffer = alloca(sc_get_buffer_length());
1008 sc_neg(a->value, buffer);
1009 return get_tarval_overflow(buffer, a->length, a->mode);
1011 case irms_float_number:
1012 /* it should be safe to enable this even if other arithmetic is disabled */
1014 return tarval_bad;*/
1016 fc_neg(a->value, NULL);
1017 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1027 tarval *tarval_add(tarval *a, tarval *b) {
1033 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1034 /* vector arithmetic not implemented yet */
1038 if (mode_is_reference(a->mode) && a->mode != b->mode) {
1039 b = tarval_convert_to(b, a->mode);
1040 } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
1041 a = tarval_convert_to(a, b->mode);
1044 assert(a->mode == b->mode);
1046 switch (get_mode_sort(a->mode)) {
1047 case irms_reference:
1048 case irms_int_number:
1049 /* modes of a,b are equal, so result has mode of a as this might be the character */
1050 buffer = alloca(sc_get_buffer_length());
1051 sc_add(a->value, b->value, buffer);
1052 return get_tarval_overflow(buffer, a->length, a->mode);
1054 case irms_float_number:
1058 fc_add(a->value, b->value, NULL);
1059 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1069 tarval *tarval_sub(tarval *a, tarval *b, ir_mode *dst_mode) {
1075 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1076 /* vector arithmetic not implemented yet */
1080 if (dst_mode != NULL) {
1081 if (a->mode != dst_mode)
1082 a = tarval_convert_to(a, dst_mode);
1083 if (b->mode != dst_mode)
1084 b = tarval_convert_to(b, dst_mode);
1086 assert(a->mode == b->mode);
1088 switch (get_mode_sort(a->mode)) {
1089 case irms_reference:
1090 case irms_int_number:
1091 /* modes of a,b are equal, so result has mode of a as this might be the character */
1092 buffer = alloca(sc_get_buffer_length());
1093 sc_sub(a->value, b->value, buffer);
1094 return get_tarval_overflow(buffer, a->length, a->mode);
1096 case irms_float_number:
1100 fc_sub(a->value, b->value, NULL);
1101 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1111 tarval *tarval_mul(tarval *a, tarval *b) {
1116 assert(a->mode == b->mode);
1118 if (get_mode_n_vector_elems(a->mode) > 1) {
1119 /* vector arithmetic not implemented yet */
1123 switch (get_mode_sort(a->mode)) {
1124 case irms_int_number:
1125 /* modes of a,b are equal */
1126 buffer = alloca(sc_get_buffer_length());
1127 sc_mul(a->value, b->value, buffer);
1128 return get_tarval_overflow(buffer, a->length, a->mode);
1130 case irms_float_number:
1134 fc_mul(a->value, b->value, NULL);
1135 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1143 * floating point division
1145 tarval *tarval_quo(tarval *a, tarval *b) {
1148 assert((a->mode == b->mode) && mode_is_float(a->mode));
1153 if (get_mode_n_vector_elems(a->mode) > 1) {
1154 /* vector arithmetic not implemented yet */
1158 fc_div(a->value, b->value, NULL);
1159 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1164 * overflow is impossible, but look out for division by zero
1166 tarval *tarval_div(tarval *a, tarval *b) {
1169 assert((a->mode == b->mode) && mode_is_int(a->mode));
1171 if (get_mode_n_vector_elems(a->mode) > 1) {
1172 /* vector arithmetic not implemented yet */
1177 if (b == get_mode_null(b->mode)) return tarval_bad;
1178 /* modes of a,b are equal */
1179 sc_div(a->value, b->value, NULL);
1180 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1185 * overflow is impossible, but look out for division by zero
1187 tarval *tarval_mod(tarval *a, tarval *b) {
1190 assert((a->mode == b->mode) && mode_is_int(a->mode));
1192 if (get_mode_n_vector_elems(a->mode) > 1) {
1193 /* vector arithmetic not implemented yet */
1198 if (b == get_mode_null(b->mode)) return tarval_bad;
1199 /* modes of a,b are equal */
1200 sc_mod(a->value, b->value, NULL);
1201 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1205 * integer division AND remainder
1206 * overflow is impossible, but look out for division by zero
1208 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1209 int len = sc_get_buffer_length();
1210 char *div_res = alloca(len);
1211 char *mod_res = alloca(len);
1215 assert((a->mode == b->mode) && mode_is_int(a->mode));
1217 if (get_mode_n_vector_elems(a->mode) > 1) {
1218 /* vector arithmetic not implemented yet */
1224 if (b == get_mode_null(b->mode)) return tarval_bad;
1225 /* modes of a,b are equal */
1226 sc_divmod(a->value, b->value, div_res, mod_res);
1227 *mod = get_tarval(mod_res, len, a->mode);
1228 return get_tarval(div_res, len, a->mode);
1234 tarval *tarval_abs(tarval *a) {
1238 assert(mode_is_num(a->mode));
1240 if (get_mode_n_vector_elems(a->mode) > 1) {
1241 /* vector arithmetic not implemented yet */
1245 switch (get_mode_sort(a->mode)) {
1246 case irms_int_number:
1247 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1248 buffer = alloca(sc_get_buffer_length());
1249 sc_neg(a->value, buffer);
1250 return get_tarval_overflow(buffer, a->length, a->mode);
1254 case irms_float_number:
1255 /* it should be safe to enable this even if other arithmetic is disabled */
1257 return tarval_bad;*/
1259 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1260 fc_neg(a->value, NULL);
1261 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1274 tarval *tarval_and(tarval *a, tarval *b) {
1277 assert(a->mode == b->mode);
1279 /* works even for vector modes */
1281 switch (get_mode_sort(a->mode)) {
1282 case irms_internal_boolean:
1283 return (a == tarval_b_false) ? a : b;
1285 case irms_int_number:
1286 sc_and(a->value, b->value, NULL);
1287 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1290 assert(0 && "operation not defined on mode");
1298 tarval *tarval_or(tarval *a, tarval *b) {
1301 assert(a->mode == b->mode);
1303 /* works even for vector modes */
1305 switch (get_mode_sort(a->mode)) {
1306 case irms_internal_boolean:
1307 return (a == tarval_b_true) ? a : b;
1309 case irms_int_number:
1310 sc_or(a->value, b->value, NULL);
1311 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1314 assert(0 && "operation not defined on mode");
1320 * bitwise exclusive or (xor)
1322 tarval *tarval_eor(tarval *a, tarval *b) {
1325 assert((a->mode == b->mode));
1327 /* works even for vector modes */
1329 switch (get_mode_sort(a->mode)) {
1330 case irms_internal_boolean:
1331 return (a == b)? tarval_b_false : tarval_b_true;
1333 case irms_int_number:
1334 sc_xor(a->value, b->value, NULL);
1335 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1338 assert(0 && "operation not defined on mode");
1344 * bitwise left shift
1346 tarval *tarval_shl(tarval *a, tarval *b) {
1347 char *temp_val = NULL;
1351 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1353 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1354 /* vector arithmetic not implemented yet */
1358 if (get_mode_modulo_shift(a->mode) != 0) {
1359 temp_val = alloca(sc_get_buffer_length());
1361 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1362 sc_mod(b->value, temp_val, temp_val);
1364 temp_val = (char*)b->value;
1366 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1367 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1371 * bitwise unsigned right shift
1373 tarval *tarval_shr(tarval *a, tarval *b) {
1374 char *temp_val = NULL;
1378 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1380 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1381 /* vector arithmetic not implemented yet */
1385 if (get_mode_modulo_shift(a->mode) != 0) {
1386 temp_val = alloca(sc_get_buffer_length());
1388 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1389 sc_mod(b->value, temp_val, temp_val);
1391 temp_val = (char*)b->value;
1393 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1394 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1398 * bitwise signed right shift
1400 tarval *tarval_shrs(tarval *a, tarval *b) {
1401 char *temp_val = NULL;
1405 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1407 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1408 /* vector arithmetic not implemented yet */
1412 if (get_mode_modulo_shift(a->mode) != 0) {
1413 temp_val = alloca(sc_get_buffer_length());
1415 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1416 sc_mod(b->value, temp_val, temp_val);
1418 temp_val = (char*)b->value;
1420 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1421 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1425 * bitwise rotation to left
1427 tarval *tarval_rotl(tarval *a, tarval *b) {
1428 char *temp_val = NULL;
1432 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1434 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1435 /* vector arithmetic not implemented yet */
1439 if (get_mode_modulo_shift(a->mode) != 0) {
1440 temp_val = alloca(sc_get_buffer_length());
1442 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1443 sc_mod(b->value, temp_val, temp_val);
1445 temp_val = (char*)b->value;
1447 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1448 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1452 * carry flag of the last operation
1454 int tarval_carry(void) {
1455 panic("tarval_carry() requetsed: not implemented on all operations");
1456 return sc_had_carry();
1462 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1463 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1467 const tarval_mode_info *mode_info;
1468 const char *prefix, *suffix;
1470 mode_info = tv->mode->tv_priv;
1472 mode_info = &default_info;
1473 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1474 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1476 switch (get_mode_sort(tv->mode)) {
1477 case irms_reference:
1478 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1480 case irms_int_number:
1481 switch (mode_info->mode_output) {
1484 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1488 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1494 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1497 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1499 case irms_float_number:
1500 switch (mode_info->mode_output) {
1502 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1505 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1510 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1514 case irms_internal_boolean:
1515 switch (mode_info->mode_output) {
1521 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1525 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1528 case irms_control_flow:
1530 case irms_auxiliary:
1531 if (tv == tarval_bad)
1532 return snprintf(buf, len, "<TV_BAD>");
1533 if (tv == tarval_undefined)
1534 return snprintf(buf, len, "<TV_UNDEF>");
1535 if (tv == tarval_unreachable)
1536 return snprintf(buf, len, "<TV_UNREACHABLE>");
1537 if (tv == tarval_reachable)
1538 return snprintf(buf, len, "<TV_REACHABLE>");
1539 return snprintf(buf, len, "<TV_??""?>");
1546 * Output of tarvals to stdio.
1548 int tarval_printf(tarval *tv) {
1552 res = tarval_snprintf(buf, sizeof(buf), tv);
1553 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1558 char *get_tarval_bitpattern(tarval *tv) {
1560 int n = get_mode_size_bits(tv->mode);
1561 int bytes = (n + 7) / 8;
1562 char *res = xmalloc((n + 1) * sizeof(char));
1565 for(i = 0; i < bytes; i++) {
1566 byte = get_tarval_sub_bits(tv, i);
1567 for(j = 1; j < 256; j <<= 1)
1569 res[pos++] = j & byte ? '1' : '0';
1578 * access to the bitpattern
1580 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1581 switch (get_mode_arithmetic(tv->mode)) {
1582 case irma_twos_complement:
1583 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1585 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1587 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1592 * Specify the output options of one mode.
1594 * This functions stores the modinfo, so DO NOT DESTROY it.
1596 * Returns zero on success.
1598 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1601 mode->tv_priv = modeinfo;
1606 * Returns the output options of one mode.
1608 * This functions returns the mode info of a given mode.
1610 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1613 return mode->tv_priv;
1617 * Returns non-zero if a given (integer) tarval has only one single bit
1620 int tarval_is_single_bit(tarval *tv) {
1624 if (!tv || tv == tarval_bad) return 0;
1625 if (! mode_is_int(tv->mode)) return 0;
1627 l = get_mode_size_bytes(tv->mode);
1628 for (bits = 0, i = l - 1; i >= 0; --i) {
1629 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1631 /* check for more than one bit in these */
1643 * Returns non-zero if the mantissa of a floating point IEEE-754
1644 * tarval is zero (i.e. 1.0Exxx)
1646 int tarval_ieee754_zero_mantissa(tarval *tv) {
1647 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1648 return fc_zero_mantissa(tv->value);
1651 /* Returns the exponent of a floating point IEEE-754 tarval. */
1652 int tarval_ieee754_get_exponent(tarval *tv) {
1653 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1654 return fc_get_exponent(tv->value);
1658 * Check if the tarval can be converted to the given mode without
1661 int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode) {
1662 const ieee_descriptor_t *desc = get_descriptor(mode);
1663 return fc_can_lossless_conv_to(tv->value, desc);
1666 /* Set the immediate precision for IEEE-754 results. */
1667 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1668 return fc_set_immediate_precision(bits);
1671 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1672 unsigned tarval_ieee754_get_exact(void) {
1673 return fc_is_exact();
1676 /* Return the size of the mantissa in bits (including possible
1677 implicit bits) for the given mode. */
1678 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode) {
1679 const ieee_descriptor_t *desc;
1681 assert(get_mode_arithmetic(mode) == irma_ieee754);
1682 desc = get_descriptor(mode);
1684 return desc->mantissa_size + desc->explicit_one;
1687 /* check if its the a floating point NaN */
1688 int tarval_is_NaN(tarval *tv) {
1689 if (! mode_is_float(tv->mode))
1691 return fc_is_nan(tv->value);
1694 /* check if its the a floating point +inf */
1695 int tarval_is_plus_inf(tarval *tv) {
1696 if (! mode_is_float(tv->mode))
1698 return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1701 /* check if its the a floating point -inf */
1702 int tarval_is_minus_inf(tarval *tv) {
1703 if (! mode_is_float(tv->mode))
1705 return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1708 /* check if the tarval represents a finite value */
1709 int tarval_is_finite(tarval *tv) {
1710 if (mode_is_float(tv->mode))
1711 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1716 * Sets the overflow mode for integer operations.
1718 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1719 int_overflow_mode = ov_mode;
1722 /* Get the overflow mode for integer operations. */
1723 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1724 return int_overflow_mode;
1727 /* Enable/Disable floating point constant folding. */
1728 int tarval_enable_fp_ops(int enable) {
1729 int old = !no_float;
1736 * default mode_info for output as HEX
1738 static const tarval_mode_info hex_output = {
1745 * Initialization of the tarval module: called before init_mode()
1747 void init_tarval_1(long null_value, int support_quad_precision) {
1748 /* if these assertion fail, tarval_is_constant() will follow ... */
1749 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1750 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1752 _null_value = null_value;
1754 /* initialize the sets holding the tarvals with a comparison function and
1755 * an initial size, which is the expected number of constants */
1756 tarvals = new_set(cmp_tv, N_CONSTANTS);
1757 values = new_set(memcmp, N_CONSTANTS);
1758 /* calls init_strcalc() with needed size */
1759 init_fltcalc(support_quad_precision ? 112 : 64);
1763 * Initialization of the tarval module: called after init_mode()
1765 void init_tarval_2(void) {
1766 tarval_bad->kind = k_tarval;
1767 tarval_bad->mode = mode_BAD;
1768 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1770 tarval_undefined->kind = k_tarval;
1771 tarval_undefined->mode = mode_ANY;
1772 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1774 tarval_b_true->kind = k_tarval;
1775 tarval_b_true->mode = mode_b;
1776 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1778 tarval_b_false->kind = k_tarval;
1779 tarval_b_false->mode = mode_b;
1780 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1782 tarval_unreachable->kind = k_tarval;
1783 tarval_unreachable->mode = mode_X;
1784 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1786 tarval_reachable->kind = k_tarval;
1787 tarval_reachable->mode = mode_X;
1788 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1791 * assign output modes that are compatible with the
1792 * old implementation: Hex output
1794 set_tarval_mode_output_option(mode_Bs, &hex_output);
1795 set_tarval_mode_output_option(mode_Bu, &hex_output);
1796 set_tarval_mode_output_option(mode_Hs, &hex_output);
1797 set_tarval_mode_output_option(mode_Hu, &hex_output);
1798 set_tarval_mode_output_option(mode_Is, &hex_output);
1799 set_tarval_mode_output_option(mode_Iu, &hex_output);
1800 set_tarval_mode_output_option(mode_Ls, &hex_output);
1801 set_tarval_mode_output_option(mode_Lu, &hex_output);
1802 set_tarval_mode_output_option(mode_P, &hex_output);
1805 /* free all memory occupied by tarval. */
1806 void finish_tarval(void) {
1809 del_set(tarvals); tarvals = NULL;
1810 del_set(values); values = NULL;
1813 int (is_tarval)(const void *thing) {
1814 return _is_tarval(thing);
1817 /****************************************************************************
1819 ****************************************************************************/