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) {
1034 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1035 /* vector arithmetic not implemented yet */
1039 if (mode_is_reference(a->mode) && a->mode != b->mode) {
1040 b = tarval_convert_to(b, a->mode);
1041 } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
1042 a = tarval_convert_to(a, b->mode);
1045 assert(a->mode == b->mode);
1047 switch (get_mode_sort(a->mode)) {
1048 case irms_reference:
1049 case irms_int_number:
1050 /* modes of a,b are equal, so result has mode of a as this might be the character */
1051 buffer = alloca(sc_get_buffer_length());
1052 sc_add(a->value, b->value, buffer);
1053 return get_tarval_overflow(buffer, a->length, a->mode);
1055 case irms_float_number:
1059 fc_add(a->value, b->value, NULL);
1060 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1070 tarval *tarval_sub(tarval *a, tarval *b, ir_mode *dst_mode) {
1076 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1077 /* vector arithmetic not implemented yet */
1081 if (dst_mode != NULL) {
1082 if (a->mode != dst_mode)
1083 a = tarval_convert_to(a, dst_mode);
1084 if (b->mode != dst_mode)
1085 b = tarval_convert_to(b, dst_mode);
1087 assert(a->mode == b->mode);
1089 switch (get_mode_sort(a->mode)) {
1090 case irms_reference:
1091 case irms_int_number:
1092 /* modes of a,b are equal, so result has mode of a as this might be the character */
1093 buffer = alloca(sc_get_buffer_length());
1094 sc_sub(a->value, b->value, buffer);
1095 return get_tarval_overflow(buffer, a->length, a->mode);
1097 case irms_float_number:
1101 fc_sub(a->value, b->value, NULL);
1102 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1112 tarval *tarval_mul(tarval *a, tarval *b) {
1117 assert(a->mode == b->mode);
1119 if (get_mode_n_vector_elems(a->mode) > 1) {
1120 /* vector arithmetic not implemented yet */
1124 switch (get_mode_sort(a->mode)) {
1125 case irms_int_number:
1126 /* modes of a,b are equal */
1127 buffer = alloca(sc_get_buffer_length());
1128 sc_mul(a->value, b->value, buffer);
1129 return get_tarval_overflow(buffer, a->length, a->mode);
1131 case irms_float_number:
1135 fc_mul(a->value, b->value, NULL);
1136 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1144 * floating point division
1146 tarval *tarval_quo(tarval *a, tarval *b) {
1149 assert((a->mode == b->mode) && mode_is_float(a->mode));
1154 if (get_mode_n_vector_elems(a->mode) > 1) {
1155 /* vector arithmetic not implemented yet */
1159 fc_div(a->value, b->value, NULL);
1160 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1165 * overflow is impossible, but look out for division by zero
1167 tarval *tarval_div(tarval *a, tarval *b) {
1170 assert((a->mode == b->mode) && mode_is_int(a->mode));
1172 if (get_mode_n_vector_elems(a->mode) > 1) {
1173 /* vector arithmetic not implemented yet */
1178 if (b == get_mode_null(b->mode)) return tarval_bad;
1179 /* modes of a,b are equal */
1180 sc_div(a->value, b->value, NULL);
1181 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1186 * overflow is impossible, but look out for division by zero
1188 tarval *tarval_mod(tarval *a, tarval *b) {
1191 assert((a->mode == b->mode) && mode_is_int(a->mode));
1193 if (get_mode_n_vector_elems(a->mode) > 1) {
1194 /* vector arithmetic not implemented yet */
1199 if (b == get_mode_null(b->mode)) return tarval_bad;
1200 /* modes of a,b are equal */
1201 sc_mod(a->value, b->value, NULL);
1202 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1206 * integer division AND remainder
1207 * overflow is impossible, but look out for division by zero
1209 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1210 int len = sc_get_buffer_length();
1211 char *div_res = alloca(len);
1212 char *mod_res = alloca(len);
1216 assert((a->mode == b->mode) && mode_is_int(a->mode));
1218 if (get_mode_n_vector_elems(a->mode) > 1) {
1219 /* vector arithmetic not implemented yet */
1225 if (b == get_mode_null(b->mode)) return tarval_bad;
1226 /* modes of a,b are equal */
1227 sc_divmod(a->value, b->value, div_res, mod_res);
1228 *mod = get_tarval(mod_res, len, a->mode);
1229 return get_tarval(div_res, len, a->mode);
1235 tarval *tarval_abs(tarval *a) {
1239 assert(mode_is_num(a->mode));
1241 if (get_mode_n_vector_elems(a->mode) > 1) {
1242 /* vector arithmetic not implemented yet */
1246 switch (get_mode_sort(a->mode)) {
1247 case irms_int_number:
1248 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1249 buffer = alloca(sc_get_buffer_length());
1250 sc_neg(a->value, buffer);
1251 return get_tarval_overflow(buffer, a->length, a->mode);
1255 case irms_float_number:
1256 /* it should be safe to enable this even if other arithmetic is disabled */
1258 return tarval_bad;*/
1260 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1261 fc_neg(a->value, NULL);
1262 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1275 tarval *tarval_and(tarval *a, tarval *b) {
1278 assert(a->mode == b->mode);
1280 /* works even for vector modes */
1282 switch (get_mode_sort(a->mode)) {
1283 case irms_internal_boolean:
1284 return (a == tarval_b_false) ? a : b;
1286 case irms_int_number:
1287 sc_and(a->value, b->value, NULL);
1288 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1291 assert(0 && "operation not defined on mode");
1299 tarval *tarval_or(tarval *a, tarval *b) {
1302 assert(a->mode == b->mode);
1304 /* works even for vector modes */
1306 switch (get_mode_sort(a->mode)) {
1307 case irms_internal_boolean:
1308 return (a == tarval_b_true) ? a : b;
1310 case irms_int_number:
1311 sc_or(a->value, b->value, NULL);
1312 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1315 assert(0 && "operation not defined on mode");
1321 * bitwise exclusive or (xor)
1323 tarval *tarval_eor(tarval *a, tarval *b) {
1326 assert((a->mode == b->mode));
1328 /* works even for vector modes */
1330 switch (get_mode_sort(a->mode)) {
1331 case irms_internal_boolean:
1332 return (a == b)? tarval_b_false : tarval_b_true;
1334 case irms_int_number:
1335 sc_xor(a->value, b->value, NULL);
1336 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1339 assert(0 && "operation not defined on mode");
1345 * bitwise left shift
1347 tarval *tarval_shl(tarval *a, tarval *b) {
1348 char *temp_val = NULL;
1352 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1354 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1355 /* vector arithmetic not implemented yet */
1359 if (get_mode_modulo_shift(a->mode) != 0) {
1360 temp_val = alloca(sc_get_buffer_length());
1362 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1363 sc_mod(b->value, temp_val, temp_val);
1365 temp_val = (char*)b->value;
1367 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1368 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1372 * bitwise unsigned right shift
1374 tarval *tarval_shr(tarval *a, tarval *b) {
1375 char *temp_val = NULL;
1379 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1381 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1382 /* vector arithmetic not implemented yet */
1386 if (get_mode_modulo_shift(a->mode) != 0) {
1387 temp_val = alloca(sc_get_buffer_length());
1389 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1390 sc_mod(b->value, temp_val, temp_val);
1392 temp_val = (char*)b->value;
1394 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1395 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1399 * bitwise signed right shift
1401 tarval *tarval_shrs(tarval *a, tarval *b) {
1402 char *temp_val = NULL;
1406 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1408 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1409 /* vector arithmetic not implemented yet */
1413 if (get_mode_modulo_shift(a->mode) != 0) {
1414 temp_val = alloca(sc_get_buffer_length());
1416 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1417 sc_mod(b->value, temp_val, temp_val);
1419 temp_val = (char*)b->value;
1421 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1422 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1426 * bitwise rotation to left
1428 tarval *tarval_rotl(tarval *a, tarval *b) {
1429 char *temp_val = NULL;
1433 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1435 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1436 /* vector arithmetic not implemented yet */
1440 if (get_mode_modulo_shift(a->mode) != 0) {
1441 temp_val = alloca(sc_get_buffer_length());
1443 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1444 sc_mod(b->value, temp_val, temp_val);
1446 temp_val = (char*)b->value;
1448 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1449 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1453 * carry flag of the last operation
1455 int tarval_carry(void) {
1456 panic("tarval_carry() requetsed: not implemented on all operations");
1457 return sc_had_carry();
1463 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1464 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1468 const tarval_mode_info *mode_info;
1469 const char *prefix, *suffix;
1471 mode_info = tv->mode->tv_priv;
1473 mode_info = &default_info;
1474 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1475 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1477 switch (get_mode_sort(tv->mode)) {
1478 case irms_reference:
1479 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1481 case irms_int_number:
1482 switch (mode_info->mode_output) {
1485 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1489 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1495 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1498 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1500 case irms_float_number:
1501 switch (mode_info->mode_output) {
1503 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1506 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1511 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1515 case irms_internal_boolean:
1516 switch (mode_info->mode_output) {
1522 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1526 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1529 case irms_control_flow:
1531 case irms_auxiliary:
1532 if (tv == tarval_bad)
1533 return snprintf(buf, len, "<TV_BAD>");
1534 if (tv == tarval_undefined)
1535 return snprintf(buf, len, "<TV_UNDEF>");
1536 if (tv == tarval_unreachable)
1537 return snprintf(buf, len, "<TV_UNREACHABLE>");
1538 if (tv == tarval_reachable)
1539 return snprintf(buf, len, "<TV_REACHABLE>");
1540 return snprintf(buf, len, "<TV_??""?>");
1547 * Output of tarvals to stdio.
1549 int tarval_printf(tarval *tv) {
1553 res = tarval_snprintf(buf, sizeof(buf), tv);
1554 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1559 char *get_tarval_bitpattern(tarval *tv) {
1561 int n = get_mode_size_bits(tv->mode);
1562 int bytes = (n + 7) / 8;
1563 char *res = xmalloc((n + 1) * sizeof(char));
1566 for(i = 0; i < bytes; i++) {
1567 byte = get_tarval_sub_bits(tv, i);
1568 for(j = 1; j < 256; j <<= 1)
1570 res[pos++] = j & byte ? '1' : '0';
1579 * access to the bitpattern
1581 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1582 switch (get_mode_arithmetic(tv->mode)) {
1583 case irma_twos_complement:
1584 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1586 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1588 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1593 * Specify the output options of one mode.
1595 * This functions stores the modinfo, so DO NOT DESTROY it.
1597 * Returns zero on success.
1599 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1602 mode->tv_priv = modeinfo;
1607 * Returns the output options of one mode.
1609 * This functions returns the mode info of a given mode.
1611 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1614 return mode->tv_priv;
1618 * Returns non-zero if a given (integer) tarval has only one single bit
1621 int tarval_is_single_bit(tarval *tv) {
1625 if (!tv || tv == tarval_bad) return 0;
1626 if (! mode_is_int(tv->mode)) return 0;
1628 l = get_mode_size_bytes(tv->mode);
1629 for (bits = 0, i = l - 1; i >= 0; --i) {
1630 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1632 /* check for more than one bit in these */
1644 * Returns non-zero if the mantissa of a floating point IEEE-754
1645 * tarval is zero (i.e. 1.0Exxx)
1647 int tarval_ieee754_zero_mantissa(tarval *tv) {
1648 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1649 return fc_zero_mantissa(tv->value);
1652 /* Returns the exponent of a floating point IEEE-754 tarval. */
1653 int tarval_ieee754_get_exponent(tarval *tv) {
1654 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1655 return fc_get_exponent(tv->value);
1659 * Check if the tarval can be converted to the given mode without
1662 int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode) {
1663 const ieee_descriptor_t *desc = get_descriptor(mode);
1664 return fc_can_lossless_conv_to(tv->value, desc);
1667 /* Set the immediate precision for IEEE-754 results. */
1668 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1669 return fc_set_immediate_precision(bits);
1672 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1673 unsigned tarval_ieee754_get_exact(void) {
1674 return fc_is_exact();
1677 /* Return the size of the mantissa in bits (including possible
1678 implicit bits) for the given mode. */
1679 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode) {
1680 const ieee_descriptor_t *desc;
1682 assert(get_mode_arithmetic(mode) == irma_ieee754);
1683 desc = get_descriptor(mode);
1685 return desc->mantissa_size + desc->explicit_one;
1688 /* check if its the a floating point NaN */
1689 int tarval_is_NaN(tarval *tv) {
1690 if (! mode_is_float(tv->mode))
1692 return fc_is_nan(tv->value);
1695 /* check if its the a floating point +inf */
1696 int tarval_is_plus_inf(tarval *tv) {
1697 if (! mode_is_float(tv->mode))
1699 return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1702 /* check if its the a floating point -inf */
1703 int tarval_is_minus_inf(tarval *tv) {
1704 if (! mode_is_float(tv->mode))
1706 return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1709 /* check if the tarval represents a finite value */
1710 int tarval_is_finite(tarval *tv) {
1711 if (mode_is_float(tv->mode))
1712 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1717 * Sets the overflow mode for integer operations.
1719 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1720 int_overflow_mode = ov_mode;
1723 /* Get the overflow mode for integer operations. */
1724 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1725 return int_overflow_mode;
1728 /* Enable/Disable floating point constant folding. */
1729 int tarval_enable_fp_ops(int enable) {
1730 int old = !no_float;
1737 * default mode_info for output as HEX
1739 static const tarval_mode_info hex_output = {
1746 * Initialization of the tarval module: called before init_mode()
1748 void init_tarval_1(long null_value, int support_quad_precision) {
1749 /* if these assertion fail, tarval_is_constant() will follow ... */
1750 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1751 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1753 _null_value = null_value;
1755 /* initialize the sets holding the tarvals with a comparison function and
1756 * an initial size, which is the expected number of constants */
1757 tarvals = new_set(cmp_tv, N_CONSTANTS);
1758 values = new_set(memcmp, N_CONSTANTS);
1759 /* calls init_strcalc() with needed size */
1760 init_fltcalc(support_quad_precision ? 112 : 64);
1764 * Initialization of the tarval module: called after init_mode()
1766 void init_tarval_2(void) {
1767 tarval_bad->kind = k_tarval;
1768 tarval_bad->mode = mode_BAD;
1769 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1771 tarval_undefined->kind = k_tarval;
1772 tarval_undefined->mode = mode_ANY;
1773 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1775 tarval_b_true->kind = k_tarval;
1776 tarval_b_true->mode = mode_b;
1777 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1779 tarval_b_false->kind = k_tarval;
1780 tarval_b_false->mode = mode_b;
1781 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1783 tarval_unreachable->kind = k_tarval;
1784 tarval_unreachable->mode = mode_X;
1785 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1787 tarval_reachable->kind = k_tarval;
1788 tarval_reachable->mode = mode_X;
1789 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1792 * assign output modes that are compatible with the
1793 * old implementation: Hex output
1795 set_tarval_mode_output_option(mode_Bs, &hex_output);
1796 set_tarval_mode_output_option(mode_Bu, &hex_output);
1797 set_tarval_mode_output_option(mode_Hs, &hex_output);
1798 set_tarval_mode_output_option(mode_Hu, &hex_output);
1799 set_tarval_mode_output_option(mode_Is, &hex_output);
1800 set_tarval_mode_output_option(mode_Iu, &hex_output);
1801 set_tarval_mode_output_option(mode_Ls, &hex_output);
1802 set_tarval_mode_output_option(mode_Lu, &hex_output);
1803 set_tarval_mode_output_option(mode_P, &hex_output);
1806 /* free all memory occupied by tarval. */
1807 void finish_tarval(void) {
1810 del_set(tarvals); tarvals = NULL;
1811 del_set(values); values = NULL;
1814 int (is_tarval)(const void *thing) {
1815 return _is_tarval(thing);
1818 /****************************************************************************
1820 ****************************************************************************/