3 * File name: ir/tv/tv.c
4 * Purpose: Representation of and static computations on target machine
10 * Copyright: (c) 2003-2007 Universität Karlsruhe
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * Values are stored in a format depending upon chosen arithmetic
16 * module. Default uses strcalc and fltcalc.
20 /* This implementation assumes:
21 * - target has IEEE-754 floating-point arithmetic. */
29 #include <assert.h> /* assertions */
30 #include <stdlib.h> /* atoi() */
32 # include <string.h> /* nice things for strings */
35 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
48 #include "set.h" /* to store tarvals in */
49 #include "entity_t.h" /* needed to store pointers to entities */
51 #include "irnode.h" /* defines boolean return values (pnc_number)*/
56 #include "firm_common.h"
58 /** Size of hash tables. Should correspond to average number of distinct constant
60 #define N_CONSTANTS 2048
62 /* get the integer overflow mode */
63 #define GET_OVERFLOW_MODE() int_overflow_mode
65 /* unused, float to int doesn't work yet */
66 enum float_to_int_mode {
71 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
73 #define SWITCH_NOINFINITY 0
74 #define SWITCH_NODENORMALS 0
76 /****************************************************************************
77 * local definitions and macros
78 ****************************************************************************/
80 # define TARVAL_VERIFY(a) tarval_verify((a))
82 # define TARVAL_VERIFY(a) ((void)0)
85 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
86 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
88 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
89 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
91 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
93 /****************************************************************************
95 ****************************************************************************/
96 static struct set *tarvals = NULL; /* container for tarval structs */
97 static struct set *values = NULL; /* container for values */
98 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
100 /****************************************************************************
102 ****************************************************************************/
104 static int hash_val(const void *value, unsigned int length);
105 static int hash_tv(tarval *tv);
106 static void _fail_verify(tarval *tv, const char* file, int line)
108 /* print a memory image of the tarval and throw an assertion */
110 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
112 printf("%s:%d: Invalid tarval (null)", file, line);
116 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
119 INLINE static void tarval_verify(tarval *tv)
125 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
126 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
128 if (!FIND_TARVAL(tv)) fail_verify(tv);
129 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
135 /** Hash a tarval. */
136 static int hash_tv(tarval *tv) {
137 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
140 /** Hash a value. Treat it as a byte array. */
141 static int hash_val(const void *value, unsigned int length) {
143 unsigned int hash = 0;
145 /* scramble the byte - array */
146 for (i = 0; i < length; ++i) {
147 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
148 hash += (hash << 11) ^ (hash >> 17);
154 /** finds tarval with value/mode or creates new tarval */
155 static tarval *get_tarval(const void *value, int length, ir_mode *mode) {
162 /* if there already is such a value, it is returned, else value
163 * is copied into the set */
164 tv.value = INSERT_VALUE(value, length);
168 /* if there is such a tarval, it is returned, else tv is copied
170 return (tarval *)INSERT_TARVAL(&tv);
176 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
178 switch (get_mode_sort(mode)) {
179 case irms_int_number:
180 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
181 switch (GET_OVERFLOW_MODE()) {
182 case TV_OVERFLOW_SATURATE:
183 return get_mode_max(mode);
184 case TV_OVERFLOW_WRAP:
186 char *temp = alloca(sc_get_buffer_length());
187 sc_val_from_ulong(-1, temp);
188 sc_and(temp, value, temp);
189 /* the sc_ module expects that all bits are set ... */
190 sign_extend(temp, mode);
191 return get_tarval(temp, length, mode);
193 case TV_OVERFLOW_BAD:
196 return get_tarval(value, length, mode);
199 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
200 switch (GET_OVERFLOW_MODE()) {
201 case TV_OVERFLOW_SATURATE:
202 return get_mode_min(mode);
203 case TV_OVERFLOW_WRAP: {
204 char *temp = alloca(sc_get_buffer_length());
205 sc_val_from_ulong(-1, temp);
206 sc_and(temp, value, temp);
207 return get_tarval(temp, length, mode);
209 case TV_OVERFLOW_BAD:
212 return get_tarval(value, length, mode);
217 case irms_float_number:
218 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
219 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
222 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
223 return get_mode_null(mode);
230 return get_tarval(value, length, mode);
234 * public variables declared in tv.h
236 static tarval reserved_tv[4];
238 tarval *tarval_bad = &reserved_tv[0];
239 tarval *tarval_undefined = &reserved_tv[1];
240 tarval *tarval_b_false = &reserved_tv[2];
241 tarval *tarval_b_true = &reserved_tv[3];
244 * public functions declared in tv.h
248 * Constructors =============================================================
250 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
256 switch (get_mode_sort(mode)) {
257 case irms_control_flow:
263 case irms_internal_boolean:
264 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
265 if (strcasecmp(str, "true")) return tarval_b_true;
266 else if (strcasecmp(str, "false")) return tarval_b_true;
268 /* XXX This is C semantics */
269 return atoi(str) ? tarval_b_true : tarval_b_false;
271 case irms_float_number:
272 switch(get_mode_size_bits(mode)) {
274 fc_val_from_str(str, len, 8, 23, NULL);
277 fc_val_from_str(str, len, 11, 52, NULL);
280 fc_val_from_str(str, len, 15, 64, NULL);
283 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
286 /* same as integer modes */
287 case irms_int_number:
289 sc_val_from_str(str, len, NULL, mode);
290 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
293 assert(0); /* can't be reached, can it? */
298 * helper function, create a tarval from long
300 tarval *new_tarval_from_long(long l, ir_mode *mode) {
303 switch(get_mode_sort(mode)) {
304 case irms_internal_boolean:
305 /* XXX C semantics ! */
306 return l ? tarval_b_true : tarval_b_false ;
309 /* same as integer modes */
310 case irms_int_number:
312 sc_val_from_long(l, NULL);
313 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
315 case irms_float_number:
316 return new_tarval_from_double((long double)l, mode);
319 assert(0 && "unsupported mode sort");
324 /* returns non-zero if can be converted to long */
325 int tarval_is_long(tarval *tv) {
326 mode_sort sort = get_mode_sort(tv->mode);
328 if (sort != irms_int_number && sort != irms_character) return 0;
330 if (get_mode_size_bits(tv->mode) > (sizeof(long) << 3)) {
331 /* the value might be too big to fit in a long */
332 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
333 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
334 /* really doesn't fit */
341 /* this might overflow the machine's long, so use only with small values */
342 long get_tarval_long(tarval* tv) {
343 assert(tarval_is_long(tv) && "tarval too big to fit in long");
345 return sc_val_to_long(tv->value);
348 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
349 assert(mode && (get_mode_sort(mode) == irms_float_number));
351 switch (get_mode_size_bits(mode)) {
353 fc_val_from_float(d, 8, 23, NULL);
356 fc_val_from_float(d, 11, 52, NULL);
359 fc_val_from_float(d, 15, 64, NULL);
362 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
365 /* returns non-zero if can be converted to double */
366 int tarval_is_double(tarval *tv) {
369 return (get_mode_sort(tv->mode) == irms_float_number);
372 long double get_tarval_double(tarval *tv) {
373 assert(tarval_is_double(tv));
375 return fc_val_to_float(tv->value);
380 * Access routines for tarval fields ========================================
383 /* get the mode of the tarval */
384 ir_mode *(get_tarval_mode)(const tarval *tv) {
385 return _get_tarval_mode(tv);
389 * Special value query functions ============================================
391 * These functions calculate and return a tarval representing the requested
393 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
394 * functions, but these are stored on initialization of the irmode module and
395 * therefore the irmode functions should be prefered to the functions below.
398 tarval *(get_tarval_bad)(void) {
399 return _get_tarval_bad();
402 tarval *(get_tarval_undefined)(void) {
403 return _get_tarval_undefined();
406 tarval *(get_tarval_b_false)(void) {
407 return _get_tarval_b_false();
410 tarval *(get_tarval_b_true)(void) {
411 return _get_tarval_b_true();
414 tarval *get_tarval_max(ir_mode *mode) {
417 if (get_mode_n_vector_elems(mode) > 1) {
418 /* vector arithmetic not implemented yet */
422 switch(get_mode_sort(mode)) {
424 case irms_control_flow:
430 case irms_internal_boolean:
431 return tarval_b_true;
433 case irms_float_number:
434 switch(get_mode_size_bits(mode)) {
436 fc_get_max(8, 23, NULL);
439 fc_get_max(11, 52, NULL);
442 fc_get_max(15, 64, NULL);
445 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
447 case irms_int_number:
449 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
450 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
455 tarval *get_tarval_min(ir_mode *mode) {
458 if (get_mode_n_vector_elems(mode) > 1) {
459 /* vector arithmetic not implemented yet */
463 switch(get_mode_sort(mode)) {
465 case irms_control_flow:
471 case irms_internal_boolean:
472 return tarval_b_false;
474 case irms_float_number:
475 switch(get_mode_size_bits(mode)) {
477 fc_get_min(8, 23, NULL);
480 fc_get_min(11, 52, NULL);
483 fc_get_min(15, 64, NULL);
486 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
488 case irms_int_number:
490 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
491 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
496 /** The bit pattern for the pointer NULL */
497 static long _null_value;
499 tarval *get_tarval_null(ir_mode *mode) {
502 if (get_mode_n_vector_elems(mode) > 1) {
503 /* vector arithmetic not implemented yet */
507 switch(get_mode_sort(mode)) {
508 case irms_control_flow:
511 case irms_internal_boolean:
515 case irms_float_number:
516 return new_tarval_from_double(0.0, mode);
518 case irms_int_number:
520 return new_tarval_from_long(0l, mode);
523 return new_tarval_from_long(_null_value, mode);
528 tarval *get_tarval_one(ir_mode *mode) {
531 if (get_mode_n_vector_elems(mode) > 1) {
532 /* vector arithmetic not implemented yet */
536 switch(get_mode_sort(mode)) {
537 case irms_control_flow:
540 case irms_internal_boolean:
545 case irms_float_number:
546 return new_tarval_from_double(1.0, mode);
548 case irms_int_number:
550 return new_tarval_from_long(1l, mode);
556 tarval *get_tarval_minus_one(ir_mode *mode) {
559 if (get_mode_n_vector_elems(mode) > 1) {
560 /* vector arithmetic not implemented yet */
564 switch(get_mode_sort(mode)) {
565 case irms_control_flow:
568 case irms_internal_boolean:
573 case irms_float_number:
574 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
576 case irms_int_number:
578 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
583 tarval *get_tarval_nan(ir_mode *mode) {
586 if (get_mode_n_vector_elems(mode) > 1) {
587 /* vector arithmetic not implemented yet */
591 if (get_mode_sort(mode) == irms_float_number) {
592 switch(get_mode_size_bits(mode)) {
594 fc_get_qnan(8, 23, NULL);
597 fc_get_qnan(11, 52, NULL);
600 fc_get_qnan(15, 64, NULL);
603 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
605 assert(0 && "tarval is not floating point");
610 tarval *get_tarval_plus_inf(ir_mode *mode) {
613 if (get_mode_n_vector_elems(mode) > 1) {
614 /* vector arithmetic not implemented yet */
618 if (get_mode_sort(mode) == irms_float_number) {
619 switch(get_mode_size_bits(mode)) {
621 fc_get_plusinf(8, 23, NULL);
624 fc_get_plusinf(11, 52, NULL);
627 fc_get_plusinf(15, 64, NULL);
630 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
632 assert(0 && "tarval is not floating point");
637 tarval *get_tarval_minus_inf(ir_mode *mode) {
640 if (get_mode_n_vector_elems(mode) > 1) {
641 /* vector arithmetic not implemented yet */
645 if (get_mode_sort(mode) == irms_float_number) {
646 switch(get_mode_size_bits(mode)) {
648 fc_get_minusinf(8, 23, NULL);
651 fc_get_minusinf(11, 52, NULL);
654 fc_get_minusinf(15, 64, NULL);
657 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
659 assert(0 && "tarval is not floating point");
665 * Arithmethic operations on tarvals ========================================
669 * test if negative number, 1 means 'yes'
671 int tarval_is_negative(tarval *a) {
674 if (get_mode_n_vector_elems(a->mode) > 1) {
675 /* vector arithmetic not implemented yet */
676 assert(0 && "tarval_is_negative is not allowed for vector modes");
680 switch (get_mode_sort(a->mode)) {
681 case irms_int_number:
682 if (!mode_is_signed(a->mode)) return 0;
684 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
686 case irms_float_number:
687 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
690 assert(0 && "not implemented");
696 * test if null, 1 means 'yes'
698 int tarval_is_null(tarval *a) {
699 ir_mode *m = get_tarval_mode(a);
701 return a == get_tarval_null(m);
705 * test if one, 1 means 'yes'
707 int tarval_is_one(tarval *a) {
708 ir_mode *m = get_tarval_mode(a);
710 return a == get_tarval_one(m);
716 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
720 if (a == tarval_bad || b == tarval_bad) {
721 assert(0 && "Comparison with tarval_bad");
725 if (a == tarval_undefined || b == tarval_undefined)
728 if (a->mode != b->mode)
731 if (get_mode_n_vector_elems(a->mode) > 1) {
732 /* vector arithmetic not implemented yet */
733 assert(0 && "cmp not implemented for vector modes");
736 /* Here the two tarvals are unequal and of the same mode */
737 switch (get_mode_sort(a->mode)) {
738 case irms_control_flow:
746 case irms_float_number:
748 * BEWARE: we cannot compare a == b here, because
749 * a NaN is always Unordered to any other value, even to itself!
751 switch (fc_comp(a->value, b->value)) {
752 case -1: return pn_Cmp_Lt;
753 case 0: return pn_Cmp_Eq;
754 case 1: return pn_Cmp_Gt;
755 case 2: return pn_Cmp_Uo;
756 default: return pn_Cmp_False;
758 case irms_int_number:
762 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
764 case irms_internal_boolean:
767 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
773 * convert to other mode
775 tarval *tarval_convert_to(tarval *src, ir_mode *m) {
781 if (src->mode == m) return src;
783 if (get_mode_n_vector_elems(src->mode) > 1) {
784 /* vector arithmetic not implemented yet */
788 switch (get_mode_sort(src->mode)) {
789 case irms_control_flow:
794 /* cast float to something */
795 case irms_float_number:
796 switch (get_mode_sort(m)) {
797 case irms_float_number:
798 switch (get_mode_size_bits(m)) {
800 fc_cast(src->value, 8, 23, NULL);
803 fc_cast(src->value, 11, 52, NULL);
806 fc_cast(src->value, 15, 64, NULL);
811 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
813 case irms_int_number:
814 switch (GET_FLOAT_TO_INT_MODE()) {
816 fc_int(src->value, NULL);
819 fc_rnd(src->value, NULL);
825 /* FIXME: floating point unit can't produce a value in integer
827 * an intermediate representation is needed here first. */
828 /* return get_tarval(); */
832 /* the rest can't be converted */
837 /* cast int to something */
838 case irms_int_number:
839 switch (get_mode_sort(m)) {
840 case irms_int_number:
842 buffer = alloca(sc_get_buffer_length());
843 memcpy(buffer, src->value, sc_get_buffer_length());
844 sign_extend(buffer, src->mode);
845 return get_tarval_overflow(buffer, src->length, m);
847 case irms_internal_boolean:
848 /* XXX C semantics */
849 if (src == get_mode_null(src->mode)) return tarval_b_false;
850 else return tarval_b_true;
852 case irms_float_number:
853 /* XXX floating point unit does not understand internal integer
854 * representation, convert to string first, then create float from
856 buffer = alloca(100);
857 /* decimal string representation because hexadecimal output is
858 * interpreted unsigned by fc_val_from_str, so this is a HACK */
859 snprintf(buffer, 100, "%s",
860 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
861 buffer[100 - 1] = '\0';
862 switch (get_mode_size_bits(m)) {
864 fc_val_from_str(buffer, 0, 8, 23, NULL);
867 fc_val_from_str(buffer, 0, 11, 52, NULL);
870 fc_val_from_str(buffer, 0, 15, 64, NULL);
873 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
876 /* allow 0 to be casted */
877 if (src == get_mode_null(src->mode))
878 return get_mode_null(m);
886 case irms_internal_boolean:
887 switch (get_mode_sort(m)) {
888 case irms_int_number:
889 if (src == tarval_b_true) return get_mode_one(m);
890 else return get_mode_null(m);
909 tarval *tarval_not(tarval *a) {
914 /* works for vector mode without changes */
916 switch (get_mode_sort(a->mode)) {
917 case irms_int_number:
918 buffer = alloca(sc_get_buffer_length());
919 sc_not(a->value, buffer);
920 return get_tarval(buffer, a->length, a->mode);
922 case irms_internal_boolean:
923 if (a == tarval_b_true)
924 return tarval_b_false;
925 if (a == tarval_b_false)
926 return tarval_b_true;
930 assert(0 && "bitwise negation is only allowed for integer and boolean");
936 * arithmetic negation
938 tarval *tarval_neg(tarval *a) {
942 assert(mode_is_num(a->mode)); /* negation only for numerical values */
944 /* note: negation is allowed even for unsigned modes. */
946 if (get_mode_n_vector_elems(a->mode) > 1) {
947 /* vector arithmetic not implemented yet */
951 switch (get_mode_sort(a->mode)) {
952 case irms_int_number:
953 buffer = alloca(sc_get_buffer_length());
954 sc_neg(a->value, buffer);
955 return get_tarval_overflow(buffer, a->length, a->mode);
957 case irms_float_number:
958 fc_neg(a->value, NULL);
959 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
969 tarval *tarval_add(tarval *a, tarval *b) {
974 assert(a->mode == b->mode);
976 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
977 /* vector arithmetic not implemented yet */
981 switch (get_mode_sort(a->mode)) {
983 case irms_int_number:
984 /* modes of a,b are equal, so result has mode of a as this might be the character */
985 buffer = alloca(sc_get_buffer_length());
986 sc_add(a->value, b->value, buffer);
987 return get_tarval_overflow(buffer, a->length, a->mode);
989 case irms_float_number:
990 fc_add(a->value, b->value, NULL);
991 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1001 tarval *tarval_sub(tarval *a, tarval *b) {
1006 assert(a->mode == b->mode);
1008 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1009 /* vector arithmetic not implemented yet */
1012 switch (get_mode_sort(a->mode)) {
1013 case irms_character:
1014 case irms_int_number:
1015 /* modes of a,b are equal, so result has mode of a as this might be the character */
1016 buffer = alloca(sc_get_buffer_length());
1017 sc_sub(a->value, b->value, buffer);
1018 return get_tarval_overflow(buffer, a->length, a->mode);
1020 case irms_float_number:
1021 fc_sub(a->value, b->value, NULL);
1022 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1032 tarval *tarval_mul(tarval *a, tarval *b) {
1037 assert(a->mode == b->mode);
1039 if (get_mode_n_vector_elems(a->mode) > 1) {
1040 /* vector arithmetic not implemented yet */
1044 switch (get_mode_sort(a->mode)) {
1045 case irms_int_number:
1046 /* modes of a,b are equal */
1047 buffer = alloca(sc_get_buffer_length());
1048 sc_mul(a->value, b->value, buffer);
1049 return get_tarval_overflow(buffer, a->length, a->mode);
1051 case irms_float_number:
1052 fc_mul(a->value, b->value, NULL);
1053 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1061 * floating point division
1063 tarval *tarval_quo(tarval *a, tarval *b) {
1066 assert((a->mode == b->mode) && mode_is_float(a->mode));
1068 if (get_mode_n_vector_elems(a->mode) > 1) {
1069 /* vector arithmetic not implemented yet */
1073 fc_div(a->value, b->value, NULL);
1074 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1079 * overflow is impossible, but look out for division by zero
1081 tarval *tarval_div(tarval *a, tarval *b) {
1084 assert((a->mode == b->mode) && mode_is_int(a->mode));
1086 if (get_mode_n_vector_elems(a->mode) > 1) {
1087 /* vector arithmetic not implemented yet */
1092 if (b == get_mode_null(b->mode)) return tarval_bad;
1093 /* modes of a,b are equal */
1094 sc_div(a->value, b->value, NULL);
1095 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1100 * overflow is impossible, but look out for division by zero
1102 tarval *tarval_mod(tarval *a, tarval *b) {
1105 assert((a->mode == b->mode) && mode_is_int(a->mode));
1107 if (get_mode_n_vector_elems(a->mode) > 1) {
1108 /* vector arithmetic not implemented yet */
1113 if (b == get_mode_null(b->mode)) return tarval_bad;
1114 /* modes of a,b are equal */
1115 sc_mod(a->value, b->value, NULL);
1116 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1122 tarval *tarval_abs(tarval *a) {
1126 assert(mode_is_num(a->mode));
1128 if (get_mode_n_vector_elems(a->mode) > 1) {
1129 /* vector arithmetic not implemented yet */
1133 switch (get_mode_sort(a->mode)) {
1134 case irms_int_number:
1135 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1136 buffer = alloca(sc_get_buffer_length());
1137 sc_neg(a->value, buffer);
1138 return get_tarval_overflow(buffer, a->length, a->mode);
1142 case irms_float_number:
1143 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1144 fc_neg(a->value, NULL);
1145 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1158 tarval *tarval_and(tarval *a, tarval *b) {
1161 assert(a->mode == b->mode);
1163 /* works even for vector modes */
1165 switch(get_mode_sort(a->mode)) {
1166 case irms_internal_boolean:
1167 return (a == tarval_b_false) ? a : b;
1169 case irms_int_number:
1170 sc_and(a->value, b->value, NULL);
1171 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1174 assert(0 && "operation not defined on mode");
1182 tarval *tarval_or (tarval *a, tarval *b) {
1185 assert(a->mode == b->mode);
1187 /* works even for vector modes */
1189 switch (get_mode_sort(a->mode)) {
1190 case irms_internal_boolean:
1191 return (a == tarval_b_true) ? a : b;
1193 case irms_int_number:
1194 sc_or(a->value, b->value, NULL);
1195 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1198 assert(0 && "operation not defined on mode");
1204 * bitwise exclusive or (xor)
1206 tarval *tarval_eor(tarval *a, tarval *b) {
1209 assert((a->mode == b->mode));
1211 /* works even for vector modes */
1213 switch (get_mode_sort(a->mode)) {
1214 case irms_internal_boolean:
1215 return (a == b)? tarval_b_false : tarval_b_true;
1217 case irms_int_number:
1218 sc_xor(a->value, b->value, NULL);
1219 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1222 assert(0 && "operation not defined on mode");
1228 * bitwise left shift
1230 tarval *tarval_shl(tarval *a, tarval *b) {
1231 char *temp_val = NULL;
1235 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1237 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1238 /* vector arithmetic not implemented yet */
1242 if (get_mode_modulo_shift(a->mode) != 0) {
1243 temp_val = alloca(sc_get_buffer_length());
1245 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1246 sc_mod(b->value, temp_val, temp_val);
1248 temp_val = (char*)b->value;
1250 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1251 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1255 * bitwise unsigned right shift
1257 tarval *tarval_shr(tarval *a, tarval *b) {
1258 char *temp_val = NULL;
1262 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1264 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1265 /* vector arithmetic not implemented yet */
1269 if (get_mode_modulo_shift(a->mode) != 0) {
1270 temp_val = alloca(sc_get_buffer_length());
1272 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1273 sc_mod(b->value, temp_val, temp_val);
1275 temp_val = (char*)b->value;
1277 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1278 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1282 * bitwise signed right shift
1284 tarval *tarval_shrs(tarval *a, tarval *b) {
1285 char *temp_val = NULL;
1289 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1291 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1292 /* vector arithmetic not implemented yet */
1296 if (get_mode_modulo_shift(a->mode) != 0) {
1297 temp_val = alloca(sc_get_buffer_length());
1299 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1300 sc_mod(b->value, temp_val, temp_val);
1302 temp_val = (char*)b->value;
1304 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1305 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1311 tarval *tarval_rot(tarval *a, tarval *b) {
1312 char *temp_val = NULL;
1316 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1318 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1319 /* vector arithmetic not implemented yet */
1323 if (get_mode_modulo_shift(a->mode) != 0) {
1324 temp_val = alloca(sc_get_buffer_length());
1326 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1327 sc_mod(b->value, temp_val, temp_val);
1329 temp_val = (char*)b->value;
1331 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1332 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1336 * carry flag of the last operation
1338 int tarval_carry(void) {
1339 return sc_had_carry();
1345 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1346 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1350 const tarval_mode_info *mode_info;
1351 const char *prefix, *suffix;
1353 mode_info = tv->mode->tv_priv;
1355 mode_info = &default_info;
1356 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1357 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1359 switch (get_mode_sort(tv->mode)) {
1360 case irms_reference:
1361 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1363 case irms_int_number:
1364 case irms_character:
1365 switch (mode_info->mode_output) {
1368 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1372 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1378 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1381 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1383 case irms_float_number:
1384 switch (mode_info->mode_output) {
1386 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1389 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1394 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1398 case irms_internal_boolean:
1399 switch (mode_info->mode_output) {
1405 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1409 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1412 case irms_control_flow:
1414 case irms_auxiliary:
1415 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1422 * Output of tarvals to stdio.
1424 int tarval_printf(tarval *tv) {
1428 res = tarval_snprintf(buf, sizeof(buf), tv);
1429 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1434 char *get_tarval_bitpattern(tarval *tv) {
1436 int n = get_mode_size_bits(tv->mode);
1437 int bytes = (n + 7) / 8;
1438 char *res = xmalloc((n + 1) * sizeof(char));
1441 for(i = 0; i < bytes; i++) {
1442 byte = get_tarval_sub_bits(tv, i);
1443 for(j = 1; j < 256; j <<= 1)
1445 res[pos++] = j & byte ? '1' : '0';
1454 * access to the bitpattern
1456 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1457 switch (get_mode_sort(tv->mode)) {
1458 case irms_int_number:
1459 case irms_character:
1460 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1462 case irms_float_number:
1463 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1471 * Specify the output options of one mode.
1473 * This functions stores the modinfo, so DO NOT DESTROY it.
1475 * Returns zero on success.
1477 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1480 mode->tv_priv = modeinfo;
1485 * Returns the output options of one mode.
1487 * This functions returns the mode info of a given mode.
1489 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1492 return mode->tv_priv;
1496 * Identifying tarvals values for algebraic simplifications.
1499 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1500 * - TV_CLASSIFY_ONE for multiplicative neutral,
1501 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1502 * - TV_CLASSIFY_OTHER else
1504 tarval_classification_t classify_tarval(tarval *tv) {
1505 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1507 if (tv == get_mode_null(tv->mode))
1508 return TV_CLASSIFY_NULL;
1509 else if (tv == get_mode_one(tv->mode))
1510 return TV_CLASSIFY_ONE;
1511 else if ((get_mode_sort(tv->mode) == irms_int_number)
1512 && (tv == new_tarval_from_long(-1, tv->mode)))
1513 return TV_CLASSIFY_ALL_ONE;
1515 return TV_CLASSIFY_OTHER;
1519 * Returns non-zero if a given (integer) tarval has only one single bit
1522 int is_single_bit_tarval(tarval *tv) {
1526 if (!tv || tv == tarval_bad) return 0;
1527 if (! mode_is_int(tv->mode)) return 0;
1529 l = get_mode_size_bytes(tv->mode);
1530 for (bits = 0, i = l - 1; i >= 0; --i) {
1531 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1533 /* check for more than one bit in these */
1545 * Sets the overflow mode for integer operations.
1547 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1548 int_overflow_mode = ov_mode;
1552 * Get the overflow mode for integer operations.
1554 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1555 return int_overflow_mode;
1559 * default mode_info for output as HEX
1561 static const tarval_mode_info hex_output = {
1568 * Initialization of the tarval module: called before init_mode()
1570 void init_tarval_1(long null_value) {
1571 _null_value = null_value;
1573 /* initialize the sets holding the tarvals with a comparison function and
1574 * an initial size, which is the expected number of constants */
1575 tarvals = new_set(memcmp, N_CONSTANTS);
1576 values = new_set(memcmp, N_CONSTANTS);
1577 /* init strcalc with precision of 68 to support floating point values with 64
1578 * bit mantissa (needs extra bits for rounding and overflow) */
1584 * Initialization of the tarval module: called after init_mode()
1586 void init_tarval_2(void) {
1587 tarval_bad->kind = k_tarval;
1588 tarval_bad->mode = mode_BAD;
1589 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1591 tarval_undefined->kind = k_tarval;
1592 tarval_undefined->mode = mode_ANY;
1593 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1595 tarval_b_true->kind = k_tarval;
1596 tarval_b_true->mode = mode_b;
1597 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1599 tarval_b_false->kind = k_tarval;
1600 tarval_b_false->mode = mode_b;
1601 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1604 * assign output modes that are compatible with the
1605 * old implementation: Hex output
1607 set_tarval_mode_output_option(mode_U, &hex_output);
1608 set_tarval_mode_output_option(mode_C, &hex_output);
1609 set_tarval_mode_output_option(mode_Bs, &hex_output);
1610 set_tarval_mode_output_option(mode_Bu, &hex_output);
1611 set_tarval_mode_output_option(mode_Hs, &hex_output);
1612 set_tarval_mode_output_option(mode_Hu, &hex_output);
1613 set_tarval_mode_output_option(mode_Is, &hex_output);
1614 set_tarval_mode_output_option(mode_Iu, &hex_output);
1615 set_tarval_mode_output_option(mode_Ls, &hex_output);
1616 set_tarval_mode_output_option(mode_Lu, &hex_output);
1617 set_tarval_mode_output_option(mode_P, &hex_output);
1620 /* free all memory occupied by tarval. */
1621 void finish_tarval(void) {
1624 del_set(tarvals); tarvals = NULL;
1625 del_set(values); values = NULL;
1628 /****************************************************************************
1630 ****************************************************************************/