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) {
1030 ir_mode *imm_mode, *dst_mode = NULL;
1035 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1036 /* vector arithmetic not implemented yet */
1040 if (mode_is_reference(a->mode)) {
1042 imm_mode = find_unsigned_mode(a->mode);
1044 if (imm_mode == NULL)
1047 a = tarval_convert_to(a, imm_mode);
1048 b = tarval_convert_to(b, imm_mode);
1050 if (mode_is_reference(b->mode)) {
1052 imm_mode = find_unsigned_mode(b->mode);
1057 a = tarval_convert_to(a, imm_mode);
1058 b = tarval_convert_to(b, imm_mode);
1061 assert(a->mode == b->mode);
1063 switch (get_mode_sort(a->mode)) {
1064 case irms_int_number:
1065 /* modes of a,b are equal, so result has mode of a as this might be the character */
1066 buffer = alloca(sc_get_buffer_length());
1067 sc_add(a->value, b->value, buffer);
1068 res = get_tarval_overflow(buffer, a->length, a->mode);
1071 case irms_float_number:
1075 fc_add(a->value, b->value, NULL);
1076 res = get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1082 if (dst_mode != NULL)
1083 return tarval_convert_to(res, dst_mode);
1090 tarval *tarval_sub(tarval *a, tarval *b, ir_mode *dst_mode) {
1096 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1097 /* vector arithmetic not implemented yet */
1101 if (dst_mode != NULL) {
1102 if (mode_is_reference(a->mode)) {
1103 a = tarval_convert_to(a, dst_mode);
1105 if (mode_is_reference(b->mode)) {
1106 b = tarval_convert_to(b, dst_mode);
1108 assert(a->mode == dst_mode);
1110 assert(a->mode == b->mode);
1112 switch (get_mode_sort(a->mode)) {
1113 case irms_int_number:
1114 /* modes of a,b are equal, so result has mode of a as this might be the character */
1115 buffer = alloca(sc_get_buffer_length());
1116 sc_sub(a->value, b->value, buffer);
1117 return get_tarval_overflow(buffer, a->length, a->mode);
1119 case irms_float_number:
1123 fc_sub(a->value, b->value, NULL);
1124 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1134 tarval *tarval_mul(tarval *a, tarval *b) {
1139 assert(a->mode == b->mode);
1141 if (get_mode_n_vector_elems(a->mode) > 1) {
1142 /* vector arithmetic not implemented yet */
1146 switch (get_mode_sort(a->mode)) {
1147 case irms_int_number:
1148 /* modes of a,b are equal */
1149 buffer = alloca(sc_get_buffer_length());
1150 sc_mul(a->value, b->value, buffer);
1151 return get_tarval_overflow(buffer, a->length, a->mode);
1153 case irms_float_number:
1157 fc_mul(a->value, b->value, NULL);
1158 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1166 * floating point division
1168 tarval *tarval_quo(tarval *a, tarval *b) {
1171 assert((a->mode == b->mode) && mode_is_float(a->mode));
1176 if (get_mode_n_vector_elems(a->mode) > 1) {
1177 /* vector arithmetic not implemented yet */
1181 fc_div(a->value, b->value, NULL);
1182 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1187 * overflow is impossible, but look out for division by zero
1189 tarval *tarval_div(tarval *a, tarval *b) {
1192 assert((a->mode == b->mode) && mode_is_int(a->mode));
1194 if (get_mode_n_vector_elems(a->mode) > 1) {
1195 /* vector arithmetic not implemented yet */
1200 if (b == get_mode_null(b->mode)) return tarval_bad;
1201 /* modes of a,b are equal */
1202 sc_div(a->value, b->value, NULL);
1203 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1208 * overflow is impossible, but look out for division by zero
1210 tarval *tarval_mod(tarval *a, tarval *b) {
1213 assert((a->mode == b->mode) && mode_is_int(a->mode));
1215 if (get_mode_n_vector_elems(a->mode) > 1) {
1216 /* vector arithmetic not implemented yet */
1221 if (b == get_mode_null(b->mode)) return tarval_bad;
1222 /* modes of a,b are equal */
1223 sc_mod(a->value, b->value, NULL);
1224 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1228 * integer division AND remainder
1229 * overflow is impossible, but look out for division by zero
1231 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1232 int len = sc_get_buffer_length();
1233 char *div_res = alloca(len);
1234 char *mod_res = alloca(len);
1238 assert((a->mode == b->mode) && mode_is_int(a->mode));
1240 if (get_mode_n_vector_elems(a->mode) > 1) {
1241 /* vector arithmetic not implemented yet */
1247 if (b == get_mode_null(b->mode)) return tarval_bad;
1248 /* modes of a,b are equal */
1249 sc_divmod(a->value, b->value, div_res, mod_res);
1250 *mod = get_tarval(mod_res, len, a->mode);
1251 return get_tarval(div_res, len, a->mode);
1257 tarval *tarval_abs(tarval *a) {
1261 assert(mode_is_num(a->mode));
1263 if (get_mode_n_vector_elems(a->mode) > 1) {
1264 /* vector arithmetic not implemented yet */
1268 switch (get_mode_sort(a->mode)) {
1269 case irms_int_number:
1270 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1271 buffer = alloca(sc_get_buffer_length());
1272 sc_neg(a->value, buffer);
1273 return get_tarval_overflow(buffer, a->length, a->mode);
1277 case irms_float_number:
1278 /* it should be safe to enable this even if other arithmetic is disabled */
1280 return tarval_bad;*/
1282 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1283 fc_neg(a->value, NULL);
1284 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1297 tarval *tarval_and(tarval *a, tarval *b) {
1300 assert(a->mode == b->mode);
1302 /* works even for vector modes */
1304 switch (get_mode_sort(a->mode)) {
1305 case irms_internal_boolean:
1306 return (a == tarval_b_false) ? a : b;
1308 case irms_int_number:
1309 sc_and(a->value, b->value, NULL);
1310 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1313 assert(0 && "operation not defined on mode");
1321 tarval *tarval_or(tarval *a, tarval *b) {
1324 assert(a->mode == b->mode);
1326 /* works even for vector modes */
1328 switch (get_mode_sort(a->mode)) {
1329 case irms_internal_boolean:
1330 return (a == tarval_b_true) ? a : b;
1332 case irms_int_number:
1333 sc_or(a->value, b->value, NULL);
1334 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1337 assert(0 && "operation not defined on mode");
1343 * bitwise exclusive or (xor)
1345 tarval *tarval_eor(tarval *a, tarval *b) {
1348 assert((a->mode == b->mode));
1350 /* works even for vector modes */
1352 switch (get_mode_sort(a->mode)) {
1353 case irms_internal_boolean:
1354 return (a == b)? tarval_b_false : tarval_b_true;
1356 case irms_int_number:
1357 sc_xor(a->value, b->value, NULL);
1358 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1361 assert(0 && "operation not defined on mode");
1367 * bitwise left shift
1369 tarval *tarval_shl(tarval *a, tarval *b) {
1370 char *temp_val = NULL;
1374 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1376 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1377 /* vector arithmetic not implemented yet */
1381 if (get_mode_modulo_shift(a->mode) != 0) {
1382 temp_val = alloca(sc_get_buffer_length());
1384 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1385 sc_mod(b->value, temp_val, temp_val);
1387 temp_val = (char*)b->value;
1389 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1390 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1394 * bitwise unsigned right shift
1396 tarval *tarval_shr(tarval *a, tarval *b) {
1397 char *temp_val = NULL;
1401 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1403 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1404 /* vector arithmetic not implemented yet */
1408 if (get_mode_modulo_shift(a->mode) != 0) {
1409 temp_val = alloca(sc_get_buffer_length());
1411 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1412 sc_mod(b->value, temp_val, temp_val);
1414 temp_val = (char*)b->value;
1416 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1417 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1421 * bitwise signed right shift
1423 tarval *tarval_shrs(tarval *a, tarval *b) {
1424 char *temp_val = NULL;
1428 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1430 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1431 /* vector arithmetic not implemented yet */
1435 if (get_mode_modulo_shift(a->mode) != 0) {
1436 temp_val = alloca(sc_get_buffer_length());
1438 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1439 sc_mod(b->value, temp_val, temp_val);
1441 temp_val = (char*)b->value;
1443 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1444 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1448 * bitwise rotation to left
1450 tarval *tarval_rotl(tarval *a, tarval *b) {
1451 char *temp_val = NULL;
1455 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1457 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1458 /* vector arithmetic not implemented yet */
1462 if (get_mode_modulo_shift(a->mode) != 0) {
1463 temp_val = alloca(sc_get_buffer_length());
1465 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1466 sc_mod(b->value, temp_val, temp_val);
1468 temp_val = (char*)b->value;
1470 sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1471 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1475 * carry flag of the last operation
1477 int tarval_carry(void) {
1478 panic("tarval_carry() requetsed: not implemented on all operations");
1479 return sc_had_carry();
1485 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1486 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1490 const tarval_mode_info *mode_info;
1491 const char *prefix, *suffix;
1493 mode_info = tv->mode->tv_priv;
1495 mode_info = &default_info;
1496 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1497 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1499 switch (get_mode_sort(tv->mode)) {
1500 case irms_reference:
1501 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1503 case irms_int_number:
1504 switch (mode_info->mode_output) {
1507 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1511 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1517 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1520 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1522 case irms_float_number:
1523 switch (mode_info->mode_output) {
1525 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1528 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1533 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1537 case irms_internal_boolean:
1538 switch (mode_info->mode_output) {
1544 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1548 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1551 case irms_control_flow:
1553 case irms_auxiliary:
1554 if (tv == tarval_bad)
1555 return snprintf(buf, len, "<TV_BAD>");
1556 if (tv == tarval_undefined)
1557 return snprintf(buf, len, "<TV_UNDEF>");
1558 if (tv == tarval_unreachable)
1559 return snprintf(buf, len, "<TV_UNREACHABLE>");
1560 if (tv == tarval_reachable)
1561 return snprintf(buf, len, "<TV_REACHABLE>");
1562 return snprintf(buf, len, "<TV_??""?>");
1569 * Output of tarvals to stdio.
1571 int tarval_printf(tarval *tv) {
1575 res = tarval_snprintf(buf, sizeof(buf), tv);
1576 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1581 char *get_tarval_bitpattern(tarval *tv) {
1583 int n = get_mode_size_bits(tv->mode);
1584 int bytes = (n + 7) / 8;
1585 char *res = xmalloc((n + 1) * sizeof(char));
1588 for(i = 0; i < bytes; i++) {
1589 byte = get_tarval_sub_bits(tv, i);
1590 for(j = 1; j < 256; j <<= 1)
1592 res[pos++] = j & byte ? '1' : '0';
1601 * access to the bitpattern
1603 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1604 switch (get_mode_arithmetic(tv->mode)) {
1605 case irma_twos_complement:
1606 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1608 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1610 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1615 * Specify the output options of one mode.
1617 * This functions stores the modinfo, so DO NOT DESTROY it.
1619 * Returns zero on success.
1621 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1624 mode->tv_priv = modeinfo;
1629 * Returns the output options of one mode.
1631 * This functions returns the mode info of a given mode.
1633 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1636 return mode->tv_priv;
1640 * Returns non-zero if a given (integer) tarval has only one single bit
1643 int tarval_is_single_bit(tarval *tv) {
1647 if (!tv || tv == tarval_bad) return 0;
1648 if (! mode_is_int(tv->mode)) return 0;
1650 l = get_mode_size_bytes(tv->mode);
1651 for (bits = 0, i = l - 1; i >= 0; --i) {
1652 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1654 /* check for more than one bit in these */
1666 * Returns non-zero if the mantissa of a floating point IEEE-754
1667 * tarval is zero (i.e. 1.0Exxx)
1669 int tarval_ieee754_zero_mantissa(tarval *tv) {
1670 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1671 return fc_zero_mantissa(tv->value);
1674 /* Returns the exponent of a floating point IEEE-754 tarval. */
1675 int tarval_ieee754_get_exponent(tarval *tv) {
1676 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1677 return fc_get_exponent(tv->value);
1681 * Check if the tarval can be converted to the given mode without
1684 int tarval_ieee754_can_conv_lossless(tarval *tv, ir_mode *mode) {
1685 const ieee_descriptor_t *desc = get_descriptor(mode);
1686 return fc_can_lossless_conv_to(tv->value, desc);
1689 /* Set the immediate precision for IEEE-754 results. */
1690 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1691 return fc_set_immediate_precision(bits);
1694 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1695 unsigned tarval_ieee754_get_exact(void) {
1696 return fc_is_exact();
1699 /* Return the size of the mantissa in bits (including possible
1700 implicit bits) for the given mode. */
1701 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode) {
1702 const ieee_descriptor_t *desc;
1704 assert(get_mode_arithmetic(mode) == irma_ieee754);
1705 desc = get_descriptor(mode);
1707 return desc->mantissa_size + desc->explicit_one;
1710 /* check if its the a floating point NaN */
1711 int tarval_is_NaN(tarval *tv) {
1712 if (! mode_is_float(tv->mode))
1714 return fc_is_nan(tv->value);
1717 /* check if its the a floating point +inf */
1718 int tarval_is_plus_inf(tarval *tv) {
1719 if (! mode_is_float(tv->mode))
1721 return fc_is_inf(tv->value) && !fc_is_negative(tv->value);
1724 /* check if its the a floating point -inf */
1725 int tarval_is_minus_inf(tarval *tv) {
1726 if (! mode_is_float(tv->mode))
1728 return fc_is_inf(tv->value) && fc_is_negative(tv->value);
1731 /* check if the tarval represents a finite value */
1732 int tarval_is_finite(tarval *tv) {
1733 if (mode_is_float(tv->mode))
1734 return !fc_is_nan(tv->value) && !fc_is_inf(tv->value);
1739 * Sets the overflow mode for integer operations.
1741 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1742 int_overflow_mode = ov_mode;
1745 /* Get the overflow mode for integer operations. */
1746 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1747 return int_overflow_mode;
1750 /* Enable/Disable floating point constant folding. */
1751 int tarval_enable_fp_ops(int enable) {
1752 int old = !no_float;
1759 * default mode_info for output as HEX
1761 static const tarval_mode_info hex_output = {
1768 * Initialization of the tarval module: called before init_mode()
1770 void init_tarval_1(long null_value, int support_quad_precision) {
1771 /* if these assertion fail, tarval_is_constant() will follow ... */
1772 assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1773 assert(tarval_b_true == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1775 _null_value = null_value;
1777 /* initialize the sets holding the tarvals with a comparison function and
1778 * an initial size, which is the expected number of constants */
1779 tarvals = new_set(cmp_tv, N_CONSTANTS);
1780 values = new_set(memcmp, N_CONSTANTS);
1781 /* calls init_strcalc() with needed size */
1782 init_fltcalc(support_quad_precision ? 112 : 64);
1786 * Initialization of the tarval module: called after init_mode()
1788 void init_tarval_2(void) {
1789 tarval_bad->kind = k_tarval;
1790 tarval_bad->mode = mode_BAD;
1791 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1793 tarval_undefined->kind = k_tarval;
1794 tarval_undefined->mode = mode_ANY;
1795 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1797 tarval_b_true->kind = k_tarval;
1798 tarval_b_true->mode = mode_b;
1799 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1801 tarval_b_false->kind = k_tarval;
1802 tarval_b_false->mode = mode_b;
1803 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1805 tarval_unreachable->kind = k_tarval;
1806 tarval_unreachable->mode = mode_X;
1807 tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1809 tarval_reachable->kind = k_tarval;
1810 tarval_reachable->mode = mode_X;
1811 tarval_reachable->value = INT_TO_PTR(resid_tarval_reachable);
1814 * assign output modes that are compatible with the
1815 * old implementation: Hex output
1817 set_tarval_mode_output_option(mode_Bs, &hex_output);
1818 set_tarval_mode_output_option(mode_Bu, &hex_output);
1819 set_tarval_mode_output_option(mode_Hs, &hex_output);
1820 set_tarval_mode_output_option(mode_Hu, &hex_output);
1821 set_tarval_mode_output_option(mode_Is, &hex_output);
1822 set_tarval_mode_output_option(mode_Iu, &hex_output);
1823 set_tarval_mode_output_option(mode_Ls, &hex_output);
1824 set_tarval_mode_output_option(mode_Lu, &hex_output);
1825 set_tarval_mode_output_option(mode_P, &hex_output);
1828 /* free all memory occupied by tarval. */
1829 void finish_tarval(void) {
1832 del_set(tarvals); tarvals = NULL;
1833 del_set(values); values = NULL;
1836 int (is_tarval)(const void *thing) {
1837 return _is_tarval(thing);
1840 /****************************************************************************
1842 ****************************************************************************/