3 * File name: ir/tv/tv.c
4 * Purpose: Representation of and static computations on target machine
10 * Copyright: (c) 2003 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)*/
55 #include "firm_common.h"
57 /** Size of hash tables. Should correspond to average number of distinct constant
59 #define N_CONSTANTS 2048
61 /* get the integer overflow mode */
62 #define GET_OVERFLOW_MODE() int_overflow_mode
64 /* unused, float to int doesn't work yet */
65 enum float_to_int_mode {
70 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
72 #define SWITCH_NOINFINITY 0
73 #define SWITCH_NODENORMALS 0
75 /****************************************************************************
76 * local definitions and macros
77 ****************************************************************************/
79 # define TARVAL_VERIFY(a) tarval_verify((a))
81 # define TARVAL_VERIFY(a) ((void)0)
84 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
85 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
87 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
88 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
90 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
92 static long long count = 0;
93 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
95 # define ANNOUNCE() ((void)0)
97 /****************************************************************************
99 ****************************************************************************/
100 static struct set *tarvals; /* container for tarval structs */
101 static struct set *values; /* container for values */
102 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
104 /****************************************************************************
106 ****************************************************************************/
108 static int hash_val(const void *value, unsigned int length);
109 static int hash_tv(tarval *tv);
110 static void _fail_verify(tarval *tv, const char* file, int line)
112 /* print a memory image of the tarval and throw an assertion */
114 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
116 printf("%s:%d: Invalid tarval (null)", file, line);
120 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
123 INLINE static void tarval_verify(tarval *tv)
129 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
130 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
132 if (!FIND_TARVAL(tv)) fail_verify(tv);
133 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
139 static int hash_tv(tarval *tv)
141 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
144 static int hash_val(const void *value, unsigned int length)
147 unsigned int hash = 0;
149 /* scramble the byte - array */
150 for (i = 0; i < length; i++)
152 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
153 hash += (hash << 11) ^ (hash >> 17);
159 /** finds tarval with value/mode or creates new tarval */
160 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
168 /* if there already is such a value, it is returned, else value
169 * is copied into the set */
170 tv.value = INSERT_VALUE(value, length);
174 /* if there is such a tarval, it is returned, else tv is copied
176 return (tarval *)INSERT_TARVAL(&tv);
182 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
184 switch (get_mode_sort(mode))
186 case irms_int_number:
187 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
188 switch (GET_OVERFLOW_MODE()) {
189 case TV_OVERFLOW_SATURATE:
190 return get_mode_max(mode);
191 case TV_OVERFLOW_WRAP:
193 char *temp = alloca(sc_get_buffer_length());
194 char *diff = alloca(sc_get_buffer_length());
195 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
196 sc_val_from_ulong(1, temp);
197 sc_add(diff, temp, diff);
198 sc_sub(value, diff, temp);
199 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
200 sc_sub(temp, diff, temp);
201 return get_tarval(temp, length, mode);
203 case TV_OVERFLOW_BAD:
206 return get_tarval(value, length, mode);
209 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
210 switch (GET_OVERFLOW_MODE()) {
211 case TV_OVERFLOW_SATURATE:
212 return get_mode_min(mode);
213 case TV_OVERFLOW_WRAP:
215 char *temp = alloca(sc_get_buffer_length());
216 char *diff = alloca(sc_get_buffer_length());
217 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
218 sc_val_from_ulong(1, temp);
219 sc_add(diff, temp, diff);
220 sc_add(value, diff, temp);
221 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
222 sc_add(temp, diff, temp);
223 return get_tarval(temp, length, mode);
225 case TV_OVERFLOW_BAD:
228 return get_tarval(value, length, mode);
233 case irms_float_number:
234 if (SWITCH_NOINFINITY && fc_is_inf(value))
236 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
239 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
241 return get_mode_null(mode);
247 return get_tarval(value, length, mode);
252 * public variables declared in tv.h
254 static tarval reserved_tv[5];
256 tarval *tarval_bad = &reserved_tv[0];
257 tarval *tarval_undefined = &reserved_tv[1];
258 tarval *tarval_b_false = &reserved_tv[2];
259 tarval *tarval_b_true = &reserved_tv[3];
260 tarval *tarval_P_void = &reserved_tv[4];
263 * public functions declared in tv.h
267 * Constructors =============================================================
269 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
276 switch (get_mode_sort(mode))
278 case irms_control_flow:
284 case irms_internal_boolean:
285 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
286 if (strcasecmp(str, "true")) return tarval_b_true;
287 else if (strcasecmp(str, "false")) return tarval_b_true;
289 /* XXX This is C semantics */
290 return atoi(str) ? tarval_b_true : tarval_b_false;
292 case irms_float_number:
293 switch(get_mode_size_bits(mode)) {
295 fc_val_from_str(str, len, 8, 23, NULL);
298 fc_val_from_str(str, len, 11, 52, NULL);
301 fc_val_from_str(str, len, 15, 64, NULL);
304 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
306 case irms_int_number:
308 sc_val_from_str(str, len, NULL, mode);
309 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
312 return get_tarval(str, len, mode);
315 assert(0); /* can't be reached, can it? */
320 * helper function, create a tarval from long
322 tarval *new_tarval_from_long(long l, ir_mode *mode)
327 switch(get_mode_sort(mode))
329 case irms_internal_boolean:
330 /* XXX C semantics ! */
331 return l ? tarval_b_true : tarval_b_false ;
333 case irms_int_number:
335 sc_val_from_long(l, NULL);
336 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
338 case irms_float_number:
339 return new_tarval_from_double((long double)l, mode);
342 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
345 assert(0 && "unsupported mode sort");
350 /* returns non-zero if can be converted to long */
351 int tarval_is_long(tarval *tv)
353 mode_sort sort = get_mode_sort(tv->mode);
356 if (sort != irms_int_number && sort != irms_character) return 0;
358 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
360 /* the value might be too big to fit in a long */
361 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
362 if (sc_comp(sc_get_buffer(), tv->value) == -1)
364 /* really doesn't fit */
371 /* this might overflow the machine's long, so use only with small values */
372 long get_tarval_long(tarval* tv)
375 assert(tarval_is_long(tv) && "tarval too big to fit in long");
377 return sc_val_to_long(tv->value);
380 tarval *new_tarval_from_double(long double d, ir_mode *mode)
383 assert(mode && (get_mode_sort(mode) == irms_float_number));
385 switch (get_mode_size_bits(mode)) {
387 fc_val_from_float(d, 8, 23, NULL);
390 fc_val_from_float(d, 11, 52, NULL);
393 fc_val_from_float(d, 15, 64, NULL);
396 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
399 /* returns non-zero if can be converted to double */
400 int tarval_is_double(tarval *tv)
405 return (get_mode_sort(tv->mode) == irms_float_number);
408 long double get_tarval_double(tarval *tv)
411 assert(tarval_is_double(tv));
413 return fc_val_to_float(tv->value);
418 * Access routines for tarval fields ========================================
421 /* get the mode of the tarval */
422 ir_mode *(get_tarval_mode)(const tarval *tv) {
423 return _get_tarval_mode(tv);
427 * Special value query functions ============================================
429 * These functions calculate and return a tarval representing the requested
431 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
432 * functions, but these are stored on initialization of the irmode module and
433 * therefore the irmode functions should be prefered to the functions below.
436 tarval *(get_tarval_bad)(void) {
437 return _get_tarval_bad();
440 tarval *(get_tarval_undefined)(void) {
441 return _get_tarval_undefined();
444 tarval *(get_tarval_b_false)(void) {
445 return _get_tarval_b_false();
448 tarval *(get_tarval_b_true)(void) {
449 return _get_tarval_b_true();
452 tarval *(get_tarval_P_void)(void) {
453 return _get_tarval_P_void();
456 tarval *get_tarval_max(ir_mode *mode)
461 if (get_mode_n_vector_elems(mode) > 1) {
462 /* vector arithmetic not implemented yet */
466 switch(get_mode_sort(mode))
469 case irms_control_flow:
475 case irms_internal_boolean:
476 return tarval_b_true;
478 case irms_float_number:
479 switch(get_mode_size_bits(mode))
482 fc_get_max(8, 23, NULL);
485 fc_get_max(11, 52, NULL);
488 fc_get_max(15, 64, NULL);
491 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
493 case irms_int_number:
495 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
496 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
501 tarval *get_tarval_min(ir_mode *mode)
506 if (get_mode_n_vector_elems(mode) > 1) {
507 /* vector arithmetic not implemented yet */
511 switch(get_mode_sort(mode))
514 case irms_control_flow:
520 case irms_internal_boolean:
521 return tarval_b_false;
523 case irms_float_number:
524 switch(get_mode_size_bits(mode))
527 fc_get_min(8, 23, NULL);
530 fc_get_min(11, 52, NULL);
533 fc_get_min(15, 64, NULL);
536 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
538 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 tarval *get_tarval_null(ir_mode *mode)
551 if (get_mode_n_vector_elems(mode) > 1) {
552 /* vector arithmetic not implemented yet */
556 switch(get_mode_sort(mode))
558 case irms_control_flow:
561 case irms_internal_boolean:
565 case irms_float_number:
566 return new_tarval_from_double(0.0, mode);
568 case irms_int_number:
570 return new_tarval_from_long(0l, mode);
573 return tarval_P_void;
578 tarval *get_tarval_one(ir_mode *mode)
583 if (get_mode_n_vector_elems(mode) > 1) {
584 /* vector arithmetic not implemented yet */
588 switch(get_mode_sort(mode))
590 case irms_control_flow:
593 case irms_internal_boolean:
598 case irms_float_number:
599 return new_tarval_from_double(1.0, mode);
601 case irms_int_number:
603 return new_tarval_from_long(1l, mode);
609 tarval *get_tarval_minus_one(ir_mode *mode)
614 if (get_mode_n_vector_elems(mode) > 1) {
615 /* vector arithmetic not implemented yet */
619 switch(get_mode_sort(mode))
621 case irms_control_flow:
624 case irms_internal_boolean:
629 case irms_float_number:
630 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
632 case irms_int_number:
634 return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
639 tarval *get_tarval_nan(ir_mode *mode)
644 if (get_mode_n_vector_elems(mode) > 1) {
645 /* vector arithmetic not implemented yet */
649 if (get_mode_sort(mode) == irms_float_number) {
650 switch(get_mode_size_bits(mode))
653 fc_get_qnan(8, 23, NULL);
656 fc_get_qnan(11, 52, NULL);
659 fc_get_qnan(15, 64, NULL);
662 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
665 assert(0 && "tarval is not floating point");
670 tarval *get_tarval_plus_inf(ir_mode *mode)
675 if (get_mode_n_vector_elems(mode) > 1) {
676 /* vector arithmetic not implemented yet */
680 if (get_mode_sort(mode) == irms_float_number) {
681 switch(get_mode_size_bits(mode))
684 fc_get_plusinf(8, 23, NULL);
687 fc_get_plusinf(11, 52, NULL);
690 fc_get_plusinf(15, 64, NULL);
693 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
696 assert(0 && "tarval is not floating point");
701 tarval *get_tarval_minus_inf(ir_mode *mode)
706 if (get_mode_n_vector_elems(mode) > 1) {
707 /* vector arithmetic not implemented yet */
711 if (get_mode_sort(mode) == irms_float_number) {
712 switch(get_mode_size_bits(mode))
715 fc_get_minusinf(8, 23, NULL);
718 fc_get_minusinf(11, 52, NULL);
721 fc_get_minusinf(15, 64, NULL);
724 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
727 assert(0 && "tarval is not floating point");
733 * Arithmethic operations on tarvals ========================================
737 * test if negative number, 1 means 'yes'
739 int tarval_is_negative(tarval *a)
744 if (get_mode_n_vector_elems(a->mode) > 1) {
745 /* vector arithmetic not implemented yet */
746 assert(0 && "tarval_is_negative is not allowed for vector modes");
750 switch (get_mode_sort(a->mode))
752 case irms_int_number:
753 if (!mode_is_signed(a->mode)) return 0;
755 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
757 case irms_float_number:
758 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
761 assert(0 && "not implemented");
767 * test if null, 1 means 'yes'
769 int tarval_is_null(tarval *a)
771 ir_mode *m = get_tarval_mode(a);
773 return a == get_tarval_null(m);
777 * test if one, 1 means 'yes'
779 int tarval_is_one(tarval *a)
781 ir_mode *m = get_tarval_mode(a);
783 return a == get_tarval_one(m);
789 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))
814 case irms_control_flow:
822 case irms_float_number:
824 * BEWARE: we cannot compare a == b here, because
825 * a NaN is always Unordered to any other value, even to itself!
827 switch (fc_comp(a->value, b->value)) {
828 case -1: return pn_Cmp_Lt;
829 case 0: return pn_Cmp_Eq;
830 case 1: return pn_Cmp_Gt;
831 case 2: return pn_Cmp_Uo;
832 default: return pn_Cmp_False;
834 case irms_int_number:
838 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
840 case irms_internal_boolean:
843 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
849 * convert to other mode
851 tarval *tarval_convert_to(tarval *src, ir_mode *m)
859 if (src->mode == m) return src;
861 if (get_mode_n_vector_elems(src->mode) > 1) {
862 /* vector arithmetic not implemented yet */
866 switch (get_mode_sort(src->mode))
868 case irms_control_flow:
873 /* cast float to something */
874 case irms_float_number:
875 switch (get_mode_sort(m)) {
876 case irms_float_number:
877 switch (get_mode_size_bits(m))
880 fc_cast(src->value, 8, 23, NULL);
883 fc_cast(src->value, 11, 52, NULL);
886 fc_cast(src->value, 15, 64, NULL);
891 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
893 case irms_int_number:
894 switch (GET_FLOAT_TO_INT_MODE())
897 fc_int(src->value, NULL);
900 fc_rnd(src->value, NULL);
906 /* FIXME: floating point unit can't produce a value in integer
908 * an intermediate representation is needed here first. */
909 /* return get_tarval(); */
913 /* the rest can't be converted */
918 /* cast int to something */
919 case irms_int_number:
920 switch (get_mode_sort(m)) {
921 case irms_int_number:
923 return get_tarval_overflow(src->value, src->length, m);
925 case irms_internal_boolean:
926 /* XXX C semantics */
927 if (src == get_mode_null(src->mode)) return tarval_b_false;
928 else return tarval_b_true;
930 case irms_float_number:
931 /* XXX floating point unit does not understand internal integer
932 * representation, convert to string first, then create float from
934 buffer = alloca(100);
935 /* decimal string representation because hexadecimal output is
936 * interpreted unsigned by fc_val_from_str, so this is a HACK */
937 snprintf(buffer, 100, "%s",
938 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
939 switch (get_mode_size_bits(m))
942 fc_val_from_str(buffer, 0, 8, 23, NULL);
945 fc_val_from_str(buffer, 0, 11, 52, NULL);
948 fc_val_from_str(buffer, 0, 15, 64, NULL);
951 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
954 /* allow 0 to be casted */
955 if (src == get_mode_null(src->mode))
956 return get_mode_null(m);
964 case irms_internal_boolean:
965 switch (get_mode_sort(m))
967 case irms_int_number:
968 if (src == tarval_b_true) return get_mode_one(m);
969 else return get_mode_null(m);
988 tarval *tarval_not(tarval *a)
995 /* works for vector mode without changes */
997 switch (get_mode_sort(a->mode))
999 case irms_int_number:
1000 buffer = alloca(sc_get_buffer_length());
1001 sc_not(a->value, buffer);
1002 return get_tarval(buffer, a->length, a->mode);
1004 case irms_internal_boolean:
1005 if (a == tarval_b_true)
1006 return tarval_b_false;
1007 if (a == tarval_b_false)
1008 return tarval_b_true;
1012 assert(0 && "bitwise negation is only allowed for integer and boolean");
1018 * arithmetic negation
1020 tarval *tarval_neg(tarval *a)
1026 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1028 /* note: negation is allowed even for unsigned modes. */
1030 if (get_mode_n_vector_elems(a->mode) > 1) {
1031 /* vector arithmetic not implemented yet */
1035 switch (get_mode_sort(a->mode))
1037 case irms_int_number:
1038 buffer = alloca(sc_get_buffer_length());
1039 sc_neg(a->value, buffer);
1040 return get_tarval_overflow(buffer, a->length, a->mode);
1042 case irms_float_number:
1043 fc_neg(a->value, NULL);
1044 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1054 tarval *tarval_add(tarval *a, tarval *b)
1061 assert(a->mode == b->mode);
1063 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1064 /* vector arithmetic not implemented yet */
1068 switch (get_mode_sort(a->mode))
1070 case irms_character:
1071 case irms_int_number:
1072 /* modes of a,b are equal, so result has mode of a as this might be the character */
1073 buffer = alloca(sc_get_buffer_length());
1074 sc_add(a->value, b->value, buffer);
1075 return get_tarval_overflow(buffer, a->length, a->mode);
1077 case irms_float_number:
1078 fc_add(a->value, b->value, NULL);
1079 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1089 tarval *tarval_sub(tarval *a, tarval *b)
1096 assert(a->mode == b->mode);
1098 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1099 /* vector arithmetic not implemented yet */
1102 switch (get_mode_sort(a->mode))
1104 case irms_character:
1105 case irms_int_number:
1106 /* modes of a,b are equal, so result has mode of a as this might be the character */
1107 buffer = alloca(sc_get_buffer_length());
1108 sc_sub(a->value, b->value, buffer);
1109 return get_tarval_overflow(buffer, a->length, a->mode);
1111 case irms_float_number:
1112 fc_sub(a->value, b->value, NULL);
1113 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1123 tarval *tarval_mul(tarval *a, tarval *b)
1130 assert(a->mode == b->mode);
1132 if (get_mode_n_vector_elems(a->mode) > 1) {
1133 /* vector arithmetic not implemented yet */
1137 switch (get_mode_sort(a->mode))
1139 case irms_int_number:
1140 /* modes of a,b are equal */
1141 buffer = alloca(sc_get_buffer_length());
1142 sc_mul(a->value, b->value, buffer);
1143 return get_tarval_overflow(buffer, a->length, a->mode);
1145 case irms_float_number:
1146 fc_mul(a->value, b->value, NULL);
1147 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1155 * floating point division
1157 tarval *tarval_quo(tarval *a, tarval *b)
1162 assert((a->mode == b->mode) && mode_is_float(a->mode));
1164 if (get_mode_n_vector_elems(a->mode) > 1) {
1165 /* vector arithmetic not implemented yet */
1169 fc_div(a->value, b->value, NULL);
1170 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1175 * overflow is impossible, but look out for division by zero
1177 tarval *tarval_div(tarval *a, tarval *b)
1182 assert((a->mode == b->mode) && mode_is_int(a->mode));
1184 if (get_mode_n_vector_elems(a->mode) > 1) {
1185 /* vector arithmetic not implemented yet */
1190 if (b == get_mode_null(b->mode)) return tarval_bad;
1191 /* modes of a,b are equal */
1192 sc_div(a->value, b->value, NULL);
1193 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1198 * overflow is impossible, but look out for division by zero
1200 tarval *tarval_mod(tarval *a, tarval *b)
1205 assert((a->mode == b->mode) && mode_is_int(a->mode));
1207 if (get_mode_n_vector_elems(a->mode) > 1) {
1208 /* vector arithmetic not implemented yet */
1213 if (b == get_mode_null(b->mode)) return tarval_bad;
1214 /* modes of a,b are equal */
1215 sc_mod(a->value, b->value, NULL);
1216 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1222 tarval *tarval_abs(tarval *a)
1228 assert(mode_is_num(a->mode));
1230 if (get_mode_n_vector_elems(a->mode) > 1) {
1231 /* vector arithmetic not implemented yet */
1235 switch (get_mode_sort(a->mode))
1237 case irms_int_number:
1238 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1240 buffer = alloca(sc_get_buffer_length());
1241 sc_neg(a->value, buffer);
1242 return get_tarval_overflow(buffer, a->length, a->mode);
1246 case irms_float_number:
1247 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1249 fc_neg(a->value, NULL);
1250 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1263 tarval *tarval_and(tarval *a, tarval *b)
1268 assert(a->mode == b->mode);
1270 /* works even for vector modes */
1272 switch(get_mode_sort(a->mode))
1274 case irms_internal_boolean:
1275 return (a == tarval_b_false) ? a : b;
1277 case irms_int_number:
1278 sc_and(a->value, b->value, NULL);
1279 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1282 assert(0 && "operation not defined on mode");
1290 tarval *tarval_or (tarval *a, tarval *b)
1295 assert(a->mode == b->mode);
1297 /* works even for vector modes */
1299 switch (get_mode_sort(a->mode))
1301 case irms_internal_boolean:
1302 return (a == tarval_b_true) ? a : b;
1304 case irms_int_number:
1305 sc_or(a->value, b->value, NULL);
1306 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1309 assert(0 && "operation not defined on mode");
1315 * bitwise exclusive or (xor)
1317 tarval *tarval_eor(tarval *a, tarval *b)
1322 assert((a->mode == b->mode));
1324 /* works even for vector modes */
1326 switch (get_mode_sort(a->mode))
1328 case irms_internal_boolean:
1329 return (a == b)? tarval_b_false : tarval_b_true;
1331 case irms_int_number:
1332 sc_xor(a->value, b->value, NULL);
1333 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1336 assert(0 && "operation not defined on mode");
1342 * bitwise left shift
1344 tarval *tarval_shl(tarval *a, tarval *b)
1346 char *temp_val = NULL;
1350 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1352 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1353 /* vector arithmetic not implemented yet */
1357 if (get_mode_modulo_shift(a->mode) != 0)
1359 temp_val = alloca(sc_get_buffer_length());
1361 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1362 sc_mod(b->value, temp_val, temp_val);
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)
1376 char *temp_val = NULL;
1380 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1382 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1383 /* vector arithmetic not implemented yet */
1387 if (get_mode_modulo_shift(a->mode) != 0)
1389 temp_val = alloca(sc_get_buffer_length());
1391 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1392 sc_mod(b->value, temp_val, temp_val);
1395 temp_val = (char*)b->value;
1397 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1398 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1402 * bitwise signed right shift
1404 tarval *tarval_shrs(tarval *a, tarval *b)
1406 char *temp_val = NULL;
1410 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1412 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1413 /* vector arithmetic not implemented yet */
1417 if (get_mode_modulo_shift(a->mode) != 0)
1419 temp_val = alloca(sc_get_buffer_length());
1421 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1422 sc_mod(b->value, temp_val, temp_val);
1425 temp_val = (char*)b->value;
1427 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1428 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1434 tarval *tarval_rot(tarval *a, tarval *b)
1436 char *temp_val = NULL;
1440 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1442 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1443 /* vector arithmetic not implemented yet */
1447 if (get_mode_modulo_shift(a->mode) != 0)
1449 temp_val = alloca(sc_get_buffer_length());
1451 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1452 sc_mod(b->value, temp_val, temp_val);
1455 temp_val = (char*)b->value;
1457 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1458 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1462 * carry flag of the last operation
1464 int tarval_carry(void)
1466 return sc_had_carry();
1472 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1474 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1478 const tarval_mode_info *mode_info;
1479 const char *prefix, *suffix;
1483 mode_info = tv->mode->tv_priv;
1485 mode_info = &default_info;
1486 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1487 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1489 switch (get_mode_sort(tv->mode))
1491 case irms_int_number:
1492 case irms_character:
1493 switch (mode_info->mode_output) {
1496 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1500 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1506 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1509 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1511 case irms_float_number:
1512 switch (mode_info->mode_output) {
1514 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1517 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1522 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1526 case irms_reference:
1527 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1528 if (tv->value != NULL){
1529 if (len > tv->length) {
1530 memcpy(buf, tv->value, tv->length);
1531 buf[tv->length] = '\0';
1535 memcpy(buf, tv->value, len-1);
1541 return snprintf(buf, len, "void");
1543 case irms_internal_boolean:
1544 switch (mode_info->mode_output) {
1550 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1554 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1557 case irms_control_flow:
1559 case irms_auxiliary:
1560 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1568 * Output of tarvals to stdio.
1570 int tarval_printf(tarval *tv) {
1574 res = tarval_snprintf(buf, sizeof(buf), tv);
1575 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1581 char *get_tarval_bitpattern(tarval *tv)
1584 int n = get_mode_size_bits(tv->mode);
1585 int bytes = (n + 7) / 8;
1586 char *res = malloc((n + 1) * sizeof(char));
1589 for(i = 0; i < bytes; i++) {
1590 byte = get_tarval_sub_bits(tv, i);
1591 for(j = 1; j < 256; j <<= 1)
1593 res[pos++] = j & byte ? '1' : '0';
1602 * access to the bitpattern
1604 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1606 switch (get_mode_sort(tv->mode)) {
1607 case irms_int_number:
1608 case irms_character:
1609 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1611 case irms_float_number:
1612 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1620 * Specify the output options of one mode.
1622 * This functions stores the modinfo, so DO NOT DESTROY it.
1624 * Returns zero on success.
1626 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1630 mode->tv_priv = modeinfo;
1635 * Returns the output options of one mode.
1637 * This functions returns the mode info of a given mode.
1639 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1643 return mode->tv_priv;
1647 * Identifying tarvals values for algebraic simplifications.
1650 * - TV_CLASSIFY_NULL for additive neutral,
1651 * - TV_CLASSIFY_ONE for multiplicative neutral,
1652 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1653 * - TV_CLASSIFY_OTHER else
1655 tarval_classification_t classify_tarval(tarval *tv)
1658 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1660 if (tv == get_mode_null(tv->mode))
1661 return TV_CLASSIFY_NULL;
1662 else if (tv == get_mode_one(tv->mode))
1663 return TV_CLASSIFY_ONE;
1664 else if ((get_mode_sort(tv->mode) == irms_int_number)
1665 && (tv == new_tarval_from_long(-1, tv->mode)))
1666 return TV_CLASSIFY_ALL_ONE;
1668 return TV_CLASSIFY_OTHER;
1672 * Sets the overflow mode for integer operations.
1674 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1675 int_overflow_mode = ov_mode;
1679 * Get the overflow mode for integer operations.
1681 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1682 return int_overflow_mode;
1686 * default mode_info for output as HEX
1688 static const tarval_mode_info hex_output = {
1695 * default mode_info for output as reference
1697 static const tarval_mode_info reference_output = {
1705 * Initialization of the tarval module: called before init_mode()
1707 void init_tarval_1(void)
1710 /* initialize the sets holding the tarvals with a comparison function and
1711 * an initial size, which is the expected number of constants */
1712 tarvals = new_set(memcmp, N_CONSTANTS);
1713 values = new_set(memcmp, N_CONSTANTS);
1714 /* init strcalc with precision of 68 to support floating point values with 64
1715 * bit mantissa (needs extra bits for rounding and overflow) */
1721 * Initialization of the tarval module: called after init_mode()
1723 void init_tarval_2(void)
1727 tarval_bad->kind = k_tarval;
1728 tarval_bad->mode = mode_BAD;
1730 tarval_undefined->kind = k_tarval;
1731 tarval_undefined->mode = mode_ANY;
1733 tarval_b_true->kind = k_tarval;
1734 tarval_b_true->mode = mode_b;
1736 tarval_b_false->kind = k_tarval;
1737 tarval_b_false->mode = mode_b;
1739 tarval_P_void->kind = k_tarval;
1740 tarval_P_void->mode = mode_P;
1743 * assign output modes that are compatible with the
1744 * old implementation: Hex output
1746 set_tarval_mode_output_option(mode_U, &hex_output);
1747 set_tarval_mode_output_option(mode_C, &hex_output);
1748 set_tarval_mode_output_option(mode_Bs, &hex_output);
1749 set_tarval_mode_output_option(mode_Bu, &hex_output);
1750 set_tarval_mode_output_option(mode_Hs, &hex_output);
1751 set_tarval_mode_output_option(mode_Hu, &hex_output);
1752 set_tarval_mode_output_option(mode_Is, &hex_output);
1753 set_tarval_mode_output_option(mode_Iu, &hex_output);
1754 set_tarval_mode_output_option(mode_Ls, &hex_output);
1755 set_tarval_mode_output_option(mode_Lu, &hex_output);
1756 set_tarval_mode_output_option(mode_P, &reference_output);
1759 /* free all memory occupied by tarval. */
1760 void finish_tarval(void) {
1763 del_set(tarvals); tarvals = NULL;
1764 del_set(values); values = NULL;
1767 /****************************************************************************
1769 ****************************************************************************/