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);
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);
955 /* allow 0 to be casted */
956 if (src == get_mode_null(src->mode))
957 return get_mode_null(m);
965 case irms_internal_boolean:
966 switch (get_mode_sort(m))
968 case irms_int_number:
969 if (src == tarval_b_true) return get_mode_one(m);
970 else return get_mode_null(m);
989 tarval *tarval_not(tarval *a)
996 /* works for vector mode without changes */
998 switch (get_mode_sort(a->mode))
1000 case irms_int_number:
1001 buffer = alloca(sc_get_buffer_length());
1002 sc_not(a->value, buffer);
1003 return get_tarval(buffer, a->length, a->mode);
1005 case irms_internal_boolean:
1006 if (a == tarval_b_true)
1007 return tarval_b_false;
1008 if (a == tarval_b_false)
1009 return tarval_b_true;
1013 assert(0 && "bitwise negation is only allowed for integer and boolean");
1019 * arithmetic negation
1021 tarval *tarval_neg(tarval *a)
1027 assert(mode_is_num(a->mode)); /* negation only for numerical values */
1029 /* note: negation is allowed even for unsigned modes. */
1031 if (get_mode_n_vector_elems(a->mode) > 1) {
1032 /* vector arithmetic not implemented yet */
1036 switch (get_mode_sort(a->mode))
1038 case irms_int_number:
1039 buffer = alloca(sc_get_buffer_length());
1040 sc_neg(a->value, buffer);
1041 return get_tarval_overflow(buffer, a->length, a->mode);
1043 case irms_float_number:
1044 fc_neg(a->value, NULL);
1045 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1055 tarval *tarval_add(tarval *a, tarval *b)
1062 assert(a->mode == b->mode);
1064 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1065 /* vector arithmetic not implemented yet */
1069 switch (get_mode_sort(a->mode))
1071 case irms_character:
1072 case irms_int_number:
1073 /* modes of a,b are equal, so result has mode of a as this might be the character */
1074 buffer = alloca(sc_get_buffer_length());
1075 sc_add(a->value, b->value, buffer);
1076 return get_tarval_overflow(buffer, a->length, a->mode);
1078 case irms_float_number:
1079 fc_add(a->value, b->value, NULL);
1080 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1090 tarval *tarval_sub(tarval *a, tarval *b)
1097 assert(a->mode == b->mode);
1099 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1100 /* vector arithmetic not implemented yet */
1103 switch (get_mode_sort(a->mode))
1105 case irms_character:
1106 case irms_int_number:
1107 /* modes of a,b are equal, so result has mode of a as this might be the character */
1108 buffer = alloca(sc_get_buffer_length());
1109 sc_sub(a->value, b->value, buffer);
1110 return get_tarval_overflow(buffer, a->length, a->mode);
1112 case irms_float_number:
1113 fc_sub(a->value, b->value, NULL);
1114 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1124 tarval *tarval_mul(tarval *a, tarval *b)
1131 assert(a->mode == b->mode);
1133 if (get_mode_n_vector_elems(a->mode) > 1) {
1134 /* vector arithmetic not implemented yet */
1138 switch (get_mode_sort(a->mode))
1140 case irms_int_number:
1141 /* modes of a,b are equal */
1142 buffer = alloca(sc_get_buffer_length());
1143 sc_mul(a->value, b->value, buffer);
1144 return get_tarval_overflow(buffer, a->length, a->mode);
1146 case irms_float_number:
1147 fc_mul(a->value, b->value, NULL);
1148 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1156 * floating point division
1158 tarval *tarval_quo(tarval *a, tarval *b)
1163 assert((a->mode == b->mode) && mode_is_float(a->mode));
1165 if (get_mode_n_vector_elems(a->mode) > 1) {
1166 /* vector arithmetic not implemented yet */
1170 fc_div(a->value, b->value, NULL);
1171 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1176 * overflow is impossible, but look out for division by zero
1178 tarval *tarval_div(tarval *a, tarval *b)
1183 assert((a->mode == b->mode) && mode_is_int(a->mode));
1185 if (get_mode_n_vector_elems(a->mode) > 1) {
1186 /* vector arithmetic not implemented yet */
1191 if (b == get_mode_null(b->mode)) return tarval_bad;
1192 /* modes of a,b are equal */
1193 sc_div(a->value, b->value, NULL);
1194 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1199 * overflow is impossible, but look out for division by zero
1201 tarval *tarval_mod(tarval *a, tarval *b)
1206 assert((a->mode == b->mode) && mode_is_int(a->mode));
1208 if (get_mode_n_vector_elems(a->mode) > 1) {
1209 /* vector arithmetic not implemented yet */
1214 if (b == get_mode_null(b->mode)) return tarval_bad;
1215 /* modes of a,b are equal */
1216 sc_mod(a->value, b->value, NULL);
1217 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1223 tarval *tarval_abs(tarval *a)
1229 assert(mode_is_num(a->mode));
1231 if (get_mode_n_vector_elems(a->mode) > 1) {
1232 /* vector arithmetic not implemented yet */
1236 switch (get_mode_sort(a->mode))
1238 case irms_int_number:
1239 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1241 buffer = alloca(sc_get_buffer_length());
1242 sc_neg(a->value, buffer);
1243 return get_tarval_overflow(buffer, a->length, a->mode);
1247 case irms_float_number:
1248 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1250 fc_neg(a->value, NULL);
1251 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1264 tarval *tarval_and(tarval *a, tarval *b)
1269 assert(a->mode == b->mode);
1271 /* works even for vector modes */
1273 switch(get_mode_sort(a->mode))
1275 case irms_internal_boolean:
1276 return (a == tarval_b_false) ? a : b;
1278 case irms_int_number:
1279 sc_and(a->value, b->value, NULL);
1280 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1283 assert(0 && "operation not defined on mode");
1291 tarval *tarval_or (tarval *a, tarval *b)
1296 assert(a->mode == b->mode);
1298 /* works even for vector modes */
1300 switch (get_mode_sort(a->mode))
1302 case irms_internal_boolean:
1303 return (a == tarval_b_true) ? a : b;
1305 case irms_int_number:
1306 sc_or(a->value, b->value, NULL);
1307 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1310 assert(0 && "operation not defined on mode");
1316 * bitwise exclusive or (xor)
1318 tarval *tarval_eor(tarval *a, tarval *b)
1323 assert((a->mode == b->mode));
1325 /* works even for vector modes */
1327 switch (get_mode_sort(a->mode))
1329 case irms_internal_boolean:
1330 return (a == b)? tarval_b_false : tarval_b_true;
1332 case irms_int_number:
1333 sc_xor(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 left shift
1345 tarval *tarval_shl(tarval *a, tarval *b)
1347 char *temp_val = NULL;
1351 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1353 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1354 /* vector arithmetic not implemented yet */
1358 if (get_mode_modulo_shift(a->mode) != 0)
1360 temp_val = alloca(sc_get_buffer_length());
1362 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1363 sc_mod(b->value, temp_val, temp_val);
1366 temp_val = (char*)b->value;
1368 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1369 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1373 * bitwise unsigned right shift
1375 tarval *tarval_shr(tarval *a, tarval *b)
1377 char *temp_val = NULL;
1381 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1383 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1384 /* vector arithmetic not implemented yet */
1388 if (get_mode_modulo_shift(a->mode) != 0)
1390 temp_val = alloca(sc_get_buffer_length());
1392 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1393 sc_mod(b->value, temp_val, temp_val);
1396 temp_val = (char*)b->value;
1398 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1399 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1403 * bitwise signed right shift
1405 tarval *tarval_shrs(tarval *a, tarval *b)
1407 char *temp_val = NULL;
1411 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1413 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1414 /* vector arithmetic not implemented yet */
1418 if (get_mode_modulo_shift(a->mode) != 0)
1420 temp_val = alloca(sc_get_buffer_length());
1422 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1423 sc_mod(b->value, temp_val, temp_val);
1426 temp_val = (char*)b->value;
1428 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1429 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1435 tarval *tarval_rot(tarval *a, tarval *b)
1437 char *temp_val = NULL;
1441 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1443 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1444 /* vector arithmetic not implemented yet */
1448 if (get_mode_modulo_shift(a->mode) != 0)
1450 temp_val = alloca(sc_get_buffer_length());
1452 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1453 sc_mod(b->value, temp_val, temp_val);
1456 temp_val = (char*)b->value;
1458 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1459 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1463 * carry flag of the last operation
1465 int tarval_carry(void)
1467 return sc_had_carry();
1473 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1475 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1479 const tarval_mode_info *mode_info;
1480 const char *prefix, *suffix;
1484 mode_info = tv->mode->tv_priv;
1486 mode_info = &default_info;
1487 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1488 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1490 switch (get_mode_sort(tv->mode))
1492 case irms_int_number:
1493 case irms_character:
1494 switch (mode_info->mode_output) {
1497 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1501 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1507 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1510 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1512 case irms_float_number:
1513 switch (mode_info->mode_output) {
1515 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1518 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1523 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1527 case irms_reference:
1528 if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1529 if (tv->value != NULL){
1530 if (len > tv->length) {
1531 memcpy(buf, tv->value, tv->length);
1532 buf[tv->length] = '\0';
1536 memcpy(buf, tv->value, len-1);
1542 return snprintf(buf, len, "void");
1544 case irms_internal_boolean:
1545 switch (mode_info->mode_output) {
1551 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1555 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1558 case irms_control_flow:
1560 case irms_auxiliary:
1561 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1569 * Output of tarvals to stdio.
1571 int tarval_printf(tarval *tv) {
1575 res = tarval_snprintf(buf, sizeof(buf), tv);
1576 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1582 char *get_tarval_bitpattern(tarval *tv)
1585 int n = get_mode_size_bits(tv->mode);
1586 int bytes = (n + 7) / 8;
1587 char *res = malloc((n + 1) * sizeof(char));
1590 for(i = 0; i < bytes; i++) {
1591 byte = get_tarval_sub_bits(tv, i);
1592 for(j = 1; j < 256; j <<= 1)
1594 res[pos++] = j & byte ? '1' : '0';
1603 * access to the bitpattern
1605 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1607 switch (get_mode_sort(tv->mode)) {
1608 case irms_int_number:
1609 case irms_character:
1610 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1612 case irms_float_number:
1613 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1621 * Specify the output options of one mode.
1623 * This functions stores the modinfo, so DO NOT DESTROY it.
1625 * Returns zero on success.
1627 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1631 mode->tv_priv = modeinfo;
1636 * Returns the output options of one mode.
1638 * This functions returns the mode info of a given mode.
1640 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1644 return mode->tv_priv;
1648 * Identifying tarvals values for algebraic simplifications.
1651 * - TV_CLASSIFY_NULL for additive neutral,
1652 * - TV_CLASSIFY_ONE for multiplicative neutral,
1653 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1654 * - TV_CLASSIFY_OTHER else
1656 tarval_classification_t classify_tarval(tarval *tv)
1659 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1661 if (tv == get_mode_null(tv->mode))
1662 return TV_CLASSIFY_NULL;
1663 else if (tv == get_mode_one(tv->mode))
1664 return TV_CLASSIFY_ONE;
1665 else if ((get_mode_sort(tv->mode) == irms_int_number)
1666 && (tv == new_tarval_from_long(-1, tv->mode)))
1667 return TV_CLASSIFY_ALL_ONE;
1669 return TV_CLASSIFY_OTHER;
1673 * Sets the overflow mode for integer operations.
1675 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1676 int_overflow_mode = ov_mode;
1680 * Get the overflow mode for integer operations.
1682 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1683 return int_overflow_mode;
1687 * default mode_info for output as HEX
1689 static const tarval_mode_info hex_output = {
1696 * default mode_info for output as reference
1698 static const tarval_mode_info reference_output = {
1706 * Initialization of the tarval module: called before init_mode()
1708 void init_tarval_1(void)
1711 /* initialize the sets holding the tarvals with a comparison function and
1712 * an initial size, which is the expected number of constants */
1713 tarvals = new_set(memcmp, N_CONSTANTS);
1714 values = new_set(memcmp, N_CONSTANTS);
1715 /* init strcalc with precision of 68 to support floating point values with 64
1716 * bit mantissa (needs extra bits for rounding and overflow) */
1722 * Initialization of the tarval module: called after init_mode()
1724 void init_tarval_2(void)
1728 tarval_bad->kind = k_tarval;
1729 tarval_bad->mode = mode_BAD;
1731 tarval_undefined->kind = k_tarval;
1732 tarval_undefined->mode = mode_ANY;
1734 tarval_b_true->kind = k_tarval;
1735 tarval_b_true->mode = mode_b;
1737 tarval_b_false->kind = k_tarval;
1738 tarval_b_false->mode = mode_b;
1740 tarval_P_void->kind = k_tarval;
1741 tarval_P_void->mode = mode_P;
1744 * assign output modes that are compatible with the
1745 * old implementation: Hex output
1747 set_tarval_mode_output_option(mode_U, &hex_output);
1748 set_tarval_mode_output_option(mode_C, &hex_output);
1749 set_tarval_mode_output_option(mode_Bs, &hex_output);
1750 set_tarval_mode_output_option(mode_Bu, &hex_output);
1751 set_tarval_mode_output_option(mode_Hs, &hex_output);
1752 set_tarval_mode_output_option(mode_Hu, &hex_output);
1753 set_tarval_mode_output_option(mode_Is, &hex_output);
1754 set_tarval_mode_output_option(mode_Iu, &hex_output);
1755 set_tarval_mode_output_option(mode_Ls, &hex_output);
1756 set_tarval_mode_output_option(mode_Lu, &hex_output);
1757 set_tarval_mode_output_option(mode_P, &reference_output);
1760 /* free all memory occupied by tarval. */
1761 void finish_tarval(void) {
1764 del_set(tarvals); tarvals = NULL;
1765 del_set(values); values = NULL;
1768 /****************************************************************************
1770 ****************************************************************************/