2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Representation of and static computations on target machine
25 * @author Mathias Heil
29 * Values are stored in a format depending upon chosen arithmetic
30 * module. Default uses strcalc and fltcalc.
31 * This implementation assumes:
32 * - target has IEEE-754 floating-point arithmetic.
38 #include <assert.h> /* assertions */
39 #include <stdlib.h> /* atoi() */
41 # include <string.h> /* nice things for strings */
44 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
51 #include "set.h" /* to store tarvals in */
52 #include "entity_t.h" /* needed to store pointers to entities */
54 #include "irnode.h" /* defines boolean return values (pnc_number)*/
59 #include "firm_common.h"
62 /** Size of hash tables. Should correspond to average number of distinct constant
64 #define N_CONSTANTS 2048
66 /* get the integer overflow mode */
67 #define GET_OVERFLOW_MODE() int_overflow_mode
69 /* unused, float to int doesn't work yet */
70 enum float_to_int_mode {
75 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
77 #define SWITCH_NOINFINITY 0
78 #define SWITCH_NODENORMALS 0
80 /****************************************************************************
81 * local definitions and macros
82 ****************************************************************************/
84 # define TARVAL_VERIFY(a) tarval_verify((a))
86 # define TARVAL_VERIFY(a) ((void)0)
89 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
90 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
92 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
93 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
95 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
97 /****************************************************************************
99 ****************************************************************************/
100 static struct set *tarvals = NULL; /* container for tarval structs */
101 static struct set *values = NULL; /* container for values */
102 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
104 /** if this is set non-zero, the constant folding for floating point is OFF */
105 static int no_float = 0;
107 /****************************************************************************
109 ****************************************************************************/
111 static int hash_val(const void *value, unsigned int length);
112 static int hash_tv(tarval *tv);
113 static void _fail_verify(tarval *tv, const char* file, int line)
115 /* print a memory image of the tarval and throw an assertion */
117 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
119 printf("%s:%d: Invalid tarval (null)", file, line);
123 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
126 INLINE static void tarval_verify(tarval *tv)
132 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
133 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
135 if (!FIND_TARVAL(tv)) fail_verify(tv);
136 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
142 /** Hash a tarval. */
143 static int hash_tv(tarval *tv) {
144 return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
147 /** Hash a value. Treat it as a byte array. */
148 static int hash_val(const void *value, unsigned int length) {
150 unsigned int hash = 0;
152 /* scramble the byte - array */
153 for (i = 0; i < length; ++i) {
154 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
155 hash += (hash << 11) ^ (hash >> 17);
161 /** finds tarval with value/mode or creates new tarval */
162 static tarval *get_tarval(const void *value, int length, ir_mode *mode) {
169 /* if there already is such a value, it is returned, else value
170 * is copied into the set */
171 char *temp = alloca(length);
172 memcpy(temp, value, length);
173 if (get_mode_arithmetic(mode) == irma_twos_complement) {
174 sign_extend(temp, mode);
176 tv.value = INSERT_VALUE(temp, length);
180 /* if there is such a tarval, it is returned, else tv is copied
182 return (tarval *)INSERT_TARVAL(&tv);
188 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
192 switch (get_mode_sort(mode)) {
194 /* addresses always wrap around */
195 temp = alloca(sc_get_buffer_length());
196 sc_val_from_ulong(-1, temp);
197 sc_and(temp, value, temp);
198 /* the sc_ module expects that all bits are set ... */
199 sign_extend(temp, mode);
200 return get_tarval(temp, length, mode);
202 case irms_int_number:
203 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
204 switch (GET_OVERFLOW_MODE()) {
205 case TV_OVERFLOW_SATURATE:
206 return get_mode_max(mode);
207 case TV_OVERFLOW_WRAP:
208 temp = alloca(sc_get_buffer_length());
209 sc_val_from_ulong(-1, temp);
210 sc_and(temp, value, temp);
211 /* the sc_ module expects that all bits are set ... */
212 sign_extend(temp, mode);
213 return get_tarval(temp, length, mode);
214 case TV_OVERFLOW_BAD:
217 return get_tarval(value, length, mode);
220 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
221 switch (GET_OVERFLOW_MODE()) {
222 case TV_OVERFLOW_SATURATE:
223 return get_mode_min(mode);
224 case TV_OVERFLOW_WRAP: {
225 char *temp = alloca(sc_get_buffer_length());
226 sc_val_from_ulong(-1, temp);
227 sc_and(temp, value, temp);
228 return get_tarval(temp, length, mode);
230 case TV_OVERFLOW_BAD:
233 return get_tarval(value, length, mode);
238 case irms_float_number:
239 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
240 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
243 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
244 return get_mode_null(mode);
251 return get_tarval(value, length, mode);
255 * public variables declared in tv.h
257 static tarval reserved_tv[4];
259 tarval *tarval_bad = &reserved_tv[0];
260 tarval *tarval_undefined = &reserved_tv[1];
261 tarval *tarval_b_false = &reserved_tv[2];
262 tarval *tarval_b_true = &reserved_tv[3];
265 * public functions declared in tv.h
269 * Constructors =============================================================
271 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
277 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);
307 /* same as integer modes */
308 case irms_int_number:
309 sc_val_from_str(str, len, NULL, mode);
310 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
313 assert(0); /* can't be reached, can it? */
318 * helper function, create a tarval from long
320 tarval *new_tarval_from_long(long l, ir_mode *mode) {
323 switch (get_mode_sort(mode)) {
324 case irms_internal_boolean:
325 /* XXX C semantics ! */
326 return l ? tarval_b_true : tarval_b_false ;
329 /* same as integer modes */
330 case irms_int_number:
331 sc_val_from_long(l, NULL);
332 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
334 case irms_float_number:
335 return new_tarval_from_double((long double)l, mode);
338 assert(0 && "unsupported mode sort");
343 /* returns non-zero if can be converted to long */
344 int tarval_is_long(tarval *tv) {
345 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
348 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
349 /* the value might be too big to fit in a long */
350 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
351 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
352 /* really doesn't fit */
359 /* this might overflow the machine's long, so use only with small values */
360 long get_tarval_long(tarval* tv) {
361 assert(tarval_is_long(tv) && "tarval too big to fit in long");
363 return sc_val_to_long(tv->value);
366 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
367 assert(mode && (get_mode_sort(mode) == irms_float_number));
369 switch (get_mode_size_bits(mode)) {
371 fc_val_from_ieee754(d, 8, 23, NULL);
374 fc_val_from_ieee754(d, 11, 52, NULL);
377 fc_val_from_ieee754(d, 15, 64, NULL);
380 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
383 /* returns non-zero if can be converted to double */
384 int tarval_is_double(tarval *tv) {
387 return (get_mode_sort(tv->mode) == irms_float_number);
390 long double get_tarval_double(tarval *tv) {
391 assert(tarval_is_double(tv));
393 return fc_val_to_ieee754(tv->value);
398 * Access routines for tarval fields ========================================
401 /* get the mode of the tarval */
402 ir_mode *(get_tarval_mode)(const tarval *tv) {
403 return _get_tarval_mode(tv);
407 * Special value query functions ============================================
409 * These functions calculate and return a tarval representing the requested
411 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
412 * functions, but these are stored on initialization of the irmode module and
413 * therefore the irmode functions should be preferred to the functions below.
416 tarval *(get_tarval_bad)(void) {
417 return _get_tarval_bad();
420 tarval *(get_tarval_undefined)(void) {
421 return _get_tarval_undefined();
424 tarval *(get_tarval_b_false)(void) {
425 return _get_tarval_b_false();
428 tarval *(get_tarval_b_true)(void) {
429 return _get_tarval_b_true();
432 tarval *get_tarval_max(ir_mode *mode) {
435 if (get_mode_n_vector_elems(mode) > 1) {
436 /* vector arithmetic not implemented yet */
440 switch(get_mode_sort(mode)) {
441 case irms_control_flow:
447 case irms_internal_boolean:
448 return tarval_b_true;
450 case irms_float_number:
451 switch(get_mode_size_bits(mode)) {
453 fc_get_max(8, 23, NULL);
456 fc_get_max(11, 52, NULL);
459 fc_get_max(15, 64, NULL);
462 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
465 case irms_int_number:
466 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
467 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
472 tarval *get_tarval_min(ir_mode *mode) {
475 if (get_mode_n_vector_elems(mode) > 1) {
476 /* vector arithmetic not implemented yet */
480 switch(get_mode_sort(mode)) {
481 case irms_control_flow:
487 case irms_internal_boolean:
488 return tarval_b_false;
490 case irms_float_number:
491 switch(get_mode_size_bits(mode)) {
493 fc_get_min(8, 23, NULL);
496 fc_get_min(11, 52, NULL);
499 fc_get_min(15, 64, NULL);
502 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
505 case irms_int_number:
506 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
507 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
512 /** The bit pattern for the pointer NULL */
513 static long _null_value = 0;
515 tarval *get_tarval_null(ir_mode *mode) {
518 if (get_mode_n_vector_elems(mode) > 1) {
519 /* vector arithmetic not implemented yet */
523 switch(get_mode_sort(mode)) {
524 case irms_control_flow:
527 case irms_internal_boolean:
531 case irms_float_number:
532 return new_tarval_from_double(0.0, mode);
534 case irms_int_number:
535 return new_tarval_from_long(0l, mode);
538 return new_tarval_from_long(_null_value, mode);
543 tarval *get_tarval_one(ir_mode *mode) {
546 if (get_mode_n_vector_elems(mode) > 1) {
547 /* vector arithmetic not implemented yet */
552 switch(get_mode_sort(mode)) {
553 case irms_control_flow:
559 case irms_internal_boolean:
560 return tarval_b_true;
562 case irms_float_number:
563 return new_tarval_from_double(1.0, mode);
566 case irms_int_number:
567 return new_tarval_from_long(1l, mode);
572 tarval *get_tarval_all_one(ir_mode *mode) {
575 if (get_mode_n_vector_elems(mode) > 1) {
576 /* vector arithmetic not implemented yet */
581 switch(get_mode_sort(mode)) {
582 case irms_control_flow:
588 case irms_int_number:
589 case irms_internal_boolean:
591 return tarval_not(get_mode_null(mode));
594 case irms_float_number:
595 return new_tarval_from_double(1.0, mode);
600 tarval *get_tarval_minus_one(ir_mode *mode) {
603 if (get_mode_n_vector_elems(mode) > 1) {
604 /* vector arithmetic not implemented yet */
608 switch(get_mode_sort(mode)) {
609 case irms_control_flow:
612 case irms_internal_boolean:
619 case irms_float_number:
620 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
622 case irms_int_number:
623 return new_tarval_from_long(-1l, mode);
628 tarval *get_tarval_nan(ir_mode *mode) {
631 if (get_mode_n_vector_elems(mode) > 1) {
632 /* vector arithmetic not implemented yet */
636 if (get_mode_sort(mode) == irms_float_number) {
637 switch(get_mode_size_bits(mode)) {
639 fc_get_qnan(8, 23, NULL);
642 fc_get_qnan(11, 52, NULL);
645 fc_get_qnan(15, 64, NULL);
648 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
650 assert(0 && "tarval is not floating point");
655 tarval *get_tarval_plus_inf(ir_mode *mode) {
658 if (get_mode_n_vector_elems(mode) > 1) {
659 /* vector arithmetic not implemented yet */
663 if (get_mode_sort(mode) == irms_float_number) {
664 switch(get_mode_size_bits(mode)) {
666 fc_get_plusinf(8, 23, NULL);
669 fc_get_plusinf(11, 52, NULL);
672 fc_get_plusinf(15, 64, NULL);
675 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
677 assert(0 && "tarval is not floating point");
682 tarval *get_tarval_minus_inf(ir_mode *mode) {
685 if (get_mode_n_vector_elems(mode) > 1) {
686 /* vector arithmetic not implemented yet */
690 if (get_mode_sort(mode) == irms_float_number) {
691 switch(get_mode_size_bits(mode)) {
693 fc_get_minusinf(8, 23, NULL);
696 fc_get_minusinf(11, 52, NULL);
699 fc_get_minusinf(15, 64, NULL);
702 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
704 assert(0 && "tarval is not floating point");
710 * Arithmethic operations on tarvals ========================================
714 * test if negative number, 1 means 'yes'
716 int tarval_is_negative(tarval *a) {
719 if (get_mode_n_vector_elems(a->mode) > 1) {
720 /* vector arithmetic not implemented yet */
721 assert(0 && "tarval_is_negative is not allowed for vector modes");
725 switch (get_mode_sort(a->mode)) {
726 case irms_int_number:
727 if (!mode_is_signed(a->mode)) return 0;
729 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
731 case irms_float_number:
732 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
735 assert(0 && "not implemented");
741 * test if null, 1 means 'yes'
743 int tarval_is_null(tarval *a) {
746 a == get_tarval_null(get_tarval_mode(a));
750 * test if one, 1 means 'yes'
752 int tarval_is_one(tarval *a) {
755 a == get_tarval_one(get_tarval_mode(a));
758 int tarval_is_all_one(tarval *tv) {
761 tv == get_mode_all_one(get_tarval_mode(tv));
767 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
771 if (a == tarval_bad || b == tarval_bad) {
772 assert(0 && "Comparison with tarval_bad");
776 if (a == tarval_undefined || b == tarval_undefined)
779 if (a->mode != b->mode)
782 if (get_mode_n_vector_elems(a->mode) > 1) {
783 /* vector arithmetic not implemented yet */
784 assert(0 && "cmp not implemented for vector modes");
787 /* Here the two tarvals are unequal and of the same mode */
788 switch (get_mode_sort(a->mode)) {
789 case irms_control_flow:
796 case irms_float_number:
800 * BEWARE: we cannot compare a == b here, because
801 * a NaN is always Unordered to any other value, even to itself!
803 switch (fc_comp(a->value, b->value)) {
804 case -1: return pn_Cmp_Lt;
805 case 0: return pn_Cmp_Eq;
806 case 1: return pn_Cmp_Gt;
807 case 2: return pn_Cmp_Uo;
808 default: return pn_Cmp_False;
811 case irms_int_number:
814 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
816 case irms_internal_boolean:
819 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
825 * convert to other mode
827 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
834 if (src->mode == dst_mode) return src;
836 if (get_mode_n_vector_elems(src->mode) > 1) {
837 /* vector arithmetic not implemented yet */
841 switch (get_mode_sort(src->mode)) {
842 case irms_control_flow:
847 /* cast float to something */
848 case irms_float_number:
849 switch (get_mode_sort(dst_mode)) {
850 case irms_float_number:
851 switch (get_mode_size_bits(dst_mode)) {
853 fc_cast(src->value, 8, 23, NULL);
856 fc_cast(src->value, 11, 52, NULL);
859 fc_cast(src->value, 15, 64, NULL);
864 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
866 case irms_int_number:
867 switch (GET_FLOAT_TO_INT_MODE()) {
869 res = fc_int(src->value, NULL);
872 res = fc_rnd(src->value, NULL);
878 if (! fc_flt2int(res, sc_get_buffer(), dst_mode))
880 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), dst_mode);
883 /* the rest can't be converted */
888 /* cast int/characters to something */
889 case irms_int_number:
890 switch (get_mode_sort(dst_mode)) {
893 case irms_int_number:
894 buffer = alloca(sc_get_buffer_length());
895 memcpy(buffer, src->value, sc_get_buffer_length());
896 sign_extend(buffer, dst_mode);
897 return get_tarval_overflow(buffer, src->length, dst_mode);
899 case irms_internal_boolean:
900 /* XXX C semantics */
901 if (src == get_mode_null(src->mode)) return tarval_b_false;
902 else return tarval_b_true;
904 case irms_float_number:
905 /* XXX floating point unit does not understand internal integer
906 * representation, convert to string first, then create float from
908 buffer = alloca(100);
909 /* decimal string representation because hexadecimal output is
910 * interpreted unsigned by fc_val_from_str, so this is a HACK */
911 snprintf(buffer, 100, "%s",
912 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
913 buffer[100 - 1] = '\0';
914 switch (get_mode_size_bits(dst_mode)) {
916 fc_val_from_str(buffer, 0, 8, 23, NULL);
919 fc_val_from_str(buffer, 0, 11, 52, NULL);
922 fc_val_from_str(buffer, 0, 15, 64, NULL);
925 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
932 case irms_internal_boolean:
933 /* beware: this is C semantic for the INTERNAL boolean mode */
934 if (get_mode_sort(dst_mode) == irms_int_number)
935 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
939 if (get_mode_sort(dst_mode) == irms_int_number) {
940 buffer = alloca(sc_get_buffer_length());
941 memcpy(buffer, src->value, sc_get_buffer_length());
942 sign_extend(buffer, src->mode);
943 return get_tarval_overflow(buffer, src->length, dst_mode);
954 tarval *tarval_not(tarval *a) {
959 /* works for vector mode without changes */
961 switch (get_mode_sort(a->mode)) {
963 case irms_int_number:
964 buffer = alloca(sc_get_buffer_length());
965 sc_not(a->value, buffer);
966 return get_tarval(buffer, a->length, a->mode);
968 case irms_internal_boolean:
969 if (a == tarval_b_true)
970 return tarval_b_false;
971 if (a == tarval_b_false)
972 return tarval_b_true;
976 assert(0 && "bitwise negation is only allowed for integer and boolean");
982 * arithmetic negation
984 tarval *tarval_neg(tarval *a) {
988 assert(mode_is_num(a->mode)); /* negation only for numerical values */
990 /* note: negation is allowed even for unsigned modes. */
992 if (get_mode_n_vector_elems(a->mode) > 1) {
993 /* vector arithmetic not implemented yet */
997 switch (get_mode_sort(a->mode)) {
998 case irms_int_number:
999 buffer = alloca(sc_get_buffer_length());
1000 sc_neg(a->value, buffer);
1001 return get_tarval_overflow(buffer, a->length, a->mode);
1003 case irms_float_number:
1007 fc_neg(a->value, NULL);
1008 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1018 tarval *tarval_add(tarval *a, tarval *b) {
1023 assert(a->mode == b->mode);
1025 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1026 /* vector arithmetic not implemented yet */
1030 switch (get_mode_sort(a->mode)) {
1031 case irms_int_number:
1032 /* modes of a,b are equal, so result has mode of a as this might be the character */
1033 buffer = alloca(sc_get_buffer_length());
1034 sc_add(a->value, b->value, buffer);
1035 return get_tarval_overflow(buffer, a->length, a->mode);
1037 case irms_float_number:
1041 fc_add(a->value, b->value, NULL);
1042 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1052 tarval *tarval_sub(tarval *a, tarval *b) {
1057 assert(a->mode == b->mode);
1059 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1060 /* vector arithmetic not implemented yet */
1063 switch (get_mode_sort(a->mode)) {
1064 case irms_int_number:
1065 /* modes of a,b are equal, so result has mode of a as this might be the character */
1066 buffer = alloca(sc_get_buffer_length());
1067 sc_sub(a->value, b->value, buffer);
1068 return get_tarval_overflow(buffer, a->length, a->mode);
1070 case irms_float_number:
1074 fc_sub(a->value, b->value, NULL);
1075 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1085 tarval *tarval_mul(tarval *a, tarval *b) {
1090 assert(a->mode == b->mode);
1092 if (get_mode_n_vector_elems(a->mode) > 1) {
1093 /* vector arithmetic not implemented yet */
1097 switch (get_mode_sort(a->mode)) {
1098 case irms_int_number:
1099 /* modes of a,b are equal */
1100 buffer = alloca(sc_get_buffer_length());
1101 sc_mul(a->value, b->value, buffer);
1102 return get_tarval_overflow(buffer, a->length, a->mode);
1104 case irms_float_number:
1108 fc_mul(a->value, b->value, NULL);
1109 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1117 * floating point division
1119 tarval *tarval_quo(tarval *a, tarval *b) {
1122 assert((a->mode == b->mode) && mode_is_float(a->mode));
1127 if (get_mode_n_vector_elems(a->mode) > 1) {
1128 /* vector arithmetic not implemented yet */
1132 fc_div(a->value, b->value, NULL);
1133 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1138 * overflow is impossible, but look out for division by zero
1140 tarval *tarval_div(tarval *a, tarval *b) {
1143 assert((a->mode == b->mode) && mode_is_int(a->mode));
1145 if (get_mode_n_vector_elems(a->mode) > 1) {
1146 /* vector arithmetic not implemented yet */
1151 if (b == get_mode_null(b->mode)) return tarval_bad;
1152 /* modes of a,b are equal */
1153 sc_div(a->value, b->value, NULL);
1154 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1159 * overflow is impossible, but look out for division by zero
1161 tarval *tarval_mod(tarval *a, tarval *b) {
1164 assert((a->mode == b->mode) && mode_is_int(a->mode));
1166 if (get_mode_n_vector_elems(a->mode) > 1) {
1167 /* vector arithmetic not implemented yet */
1172 if (b == get_mode_null(b->mode)) return tarval_bad;
1173 /* modes of a,b are equal */
1174 sc_mod(a->value, b->value, NULL);
1175 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1179 * integer division AND remainder
1180 * overflow is impossible, but look out for division by zero
1182 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1183 int len = sc_get_buffer_length();
1184 char *div_res = alloca(len);
1185 char *mod_res = alloca(len);
1189 assert((a->mode == b->mode) && mode_is_int(a->mode));
1191 if (get_mode_n_vector_elems(a->mode) > 1) {
1192 /* vector arithmetic not implemented yet */
1198 if (b == get_mode_null(b->mode)) return tarval_bad;
1199 /* modes of a,b are equal */
1200 sc_divmod(a->value, b->value, div_res, mod_res);
1201 *mod = get_tarval(mod_res, len, a->mode);
1202 return get_tarval(div_res, len, a->mode);
1208 tarval *tarval_abs(tarval *a) {
1212 assert(mode_is_num(a->mode));
1214 if (get_mode_n_vector_elems(a->mode) > 1) {
1215 /* vector arithmetic not implemented yet */
1219 switch (get_mode_sort(a->mode)) {
1220 case irms_int_number:
1221 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1222 buffer = alloca(sc_get_buffer_length());
1223 sc_neg(a->value, buffer);
1224 return get_tarval_overflow(buffer, a->length, a->mode);
1228 case irms_float_number:
1232 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1233 fc_neg(a->value, NULL);
1234 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1247 tarval *tarval_and(tarval *a, tarval *b) {
1250 assert(a->mode == b->mode);
1252 /* works even for vector modes */
1254 switch(get_mode_sort(a->mode)) {
1255 case irms_internal_boolean:
1256 return (a == tarval_b_false) ? a : b;
1258 case irms_int_number:
1259 sc_and(a->value, b->value, NULL);
1260 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1263 assert(0 && "operation not defined on mode");
1271 tarval *tarval_or (tarval *a, tarval *b) {
1274 assert(a->mode == b->mode);
1276 /* works even for vector modes */
1278 switch (get_mode_sort(a->mode)) {
1279 case irms_internal_boolean:
1280 return (a == tarval_b_true) ? a : b;
1282 case irms_int_number:
1283 sc_or(a->value, b->value, NULL);
1284 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1287 assert(0 && "operation not defined on mode");
1293 * bitwise exclusive or (xor)
1295 tarval *tarval_eor(tarval *a, tarval *b) {
1298 assert((a->mode == b->mode));
1300 /* works even for vector modes */
1302 switch (get_mode_sort(a->mode)) {
1303 case irms_internal_boolean:
1304 return (a == b)? tarval_b_false : tarval_b_true;
1306 case irms_int_number:
1307 sc_xor(a->value, b->value, NULL);
1308 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1311 assert(0 && "operation not defined on mode");
1317 * bitwise left shift
1319 tarval *tarval_shl(tarval *a, tarval *b) {
1320 char *temp_val = NULL;
1324 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1326 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1327 /* vector arithmetic not implemented yet */
1331 if (get_mode_modulo_shift(a->mode) != 0) {
1332 temp_val = alloca(sc_get_buffer_length());
1334 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1335 sc_mod(b->value, temp_val, temp_val);
1337 temp_val = (char*)b->value;
1339 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1340 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1344 * bitwise unsigned right shift
1346 tarval *tarval_shr(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) {
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);
1364 temp_val = (char*)b->value;
1366 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1367 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1371 * bitwise signed right shift
1373 tarval *tarval_shrs(tarval *a, tarval *b) {
1374 char *temp_val = NULL;
1378 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1380 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1381 /* vector arithmetic not implemented yet */
1385 if (get_mode_modulo_shift(a->mode) != 0) {
1386 temp_val = alloca(sc_get_buffer_length());
1388 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1389 sc_mod(b->value, temp_val, temp_val);
1391 temp_val = (char*)b->value;
1393 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1394 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1400 tarval *tarval_rot(tarval *a, tarval *b) {
1401 char *temp_val = NULL;
1405 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1407 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1408 /* vector arithmetic not implemented yet */
1412 if (get_mode_modulo_shift(a->mode) != 0) {
1413 temp_val = alloca(sc_get_buffer_length());
1415 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1416 sc_mod(b->value, temp_val, temp_val);
1418 temp_val = (char*)b->value;
1420 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1421 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1425 * carry flag of the last operation
1427 int tarval_carry(void) {
1428 return sc_had_carry();
1434 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1435 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1439 const tarval_mode_info *mode_info;
1440 const char *prefix, *suffix;
1442 mode_info = tv->mode->tv_priv;
1444 mode_info = &default_info;
1445 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1446 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1448 switch (get_mode_sort(tv->mode)) {
1449 case irms_reference:
1450 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1452 case irms_int_number:
1453 switch (mode_info->mode_output) {
1456 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1460 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1466 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1469 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1471 case irms_float_number:
1472 switch (mode_info->mode_output) {
1474 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1477 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1482 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1486 case irms_internal_boolean:
1487 switch (mode_info->mode_output) {
1493 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1497 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1500 case irms_control_flow:
1502 case irms_auxiliary:
1503 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1510 * Output of tarvals to stdio.
1512 int tarval_printf(tarval *tv) {
1516 res = tarval_snprintf(buf, sizeof(buf), tv);
1517 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1522 char *get_tarval_bitpattern(tarval *tv) {
1524 int n = get_mode_size_bits(tv->mode);
1525 int bytes = (n + 7) / 8;
1526 char *res = xmalloc((n + 1) * sizeof(char));
1529 for(i = 0; i < bytes; i++) {
1530 byte = get_tarval_sub_bits(tv, i);
1531 for(j = 1; j < 256; j <<= 1)
1533 res[pos++] = j & byte ? '1' : '0';
1542 * access to the bitpattern
1544 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1545 switch (get_mode_arithmetic(tv->mode)) {
1546 case irma_twos_complement:
1547 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1549 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1551 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1556 * Specify the output options of one mode.
1558 * This functions stores the modinfo, so DO NOT DESTROY it.
1560 * Returns zero on success.
1562 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1565 mode->tv_priv = modeinfo;
1570 * Returns the output options of one mode.
1572 * This functions returns the mode info of a given mode.
1574 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1577 return mode->tv_priv;
1581 * Returns non-zero if a given (integer) tarval has only one single bit
1584 int tarval_is_single_bit(tarval *tv) {
1588 if (!tv || tv == tarval_bad) return 0;
1589 if (! mode_is_int(tv->mode)) return 0;
1591 l = get_mode_size_bytes(tv->mode);
1592 for (bits = 0, i = l - 1; i >= 0; --i) {
1593 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1595 /* check for more than one bit in these */
1607 * Returns non-zero if the mantissa of a floating point IEEE-754
1608 * tarval is zero (i.e. 1.0Exxx)
1610 int tarval_ieee754_zero_mantissa(tarval *tv) {
1611 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1612 return fc_zero_mantissa(tv->value);
1615 /* Returns the exponent of a floating point IEEE-754 tarval. */
1616 int tarval_ieee754_get_exponent(tarval *tv) {
1617 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1618 return fc_get_exponent(tv->value);
1621 /* Set the immediate precision for IEEE-754 results. */
1622 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1623 return fc_set_immediate_precision(bits);
1626 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1627 unsigned tarval_ieee754_get_exact(void) {
1628 return fc_is_exact();
1632 * Sets the overflow mode for integer operations.
1634 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1635 int_overflow_mode = ov_mode;
1638 /* Get the overflow mode for integer operations. */
1639 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1640 return int_overflow_mode;
1643 /* Enable/Disable floating point constant folding. */
1644 int tarval_enable_fp_ops(int enable) {
1645 int old = !no_float;
1652 * default mode_info for output as HEX
1654 static const tarval_mode_info hex_output = {
1661 * Initialization of the tarval module: called before init_mode()
1663 void init_tarval_1(long null_value) {
1664 _null_value = null_value;
1666 /* initialize the sets holding the tarvals with a comparison function and
1667 * an initial size, which is the expected number of constants */
1668 tarvals = new_set(memcmp, N_CONSTANTS);
1669 values = new_set(memcmp, N_CONSTANTS);
1670 /* init strcalc with precision of 68 to support floating point values with 64
1671 * bit mantissa (needs extra bits for rounding and overflow) */
1677 * Initialization of the tarval module: called after init_mode()
1679 void init_tarval_2(void) {
1680 tarval_bad->kind = k_tarval;
1681 tarval_bad->mode = mode_BAD;
1682 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1684 tarval_undefined->kind = k_tarval;
1685 tarval_undefined->mode = mode_ANY;
1686 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1688 tarval_b_true->kind = k_tarval;
1689 tarval_b_true->mode = mode_b;
1690 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1692 tarval_b_false->kind = k_tarval;
1693 tarval_b_false->mode = mode_b;
1694 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1697 * assign output modes that are compatible with the
1698 * old implementation: Hex output
1700 set_tarval_mode_output_option(mode_Bs, &hex_output);
1701 set_tarval_mode_output_option(mode_Bu, &hex_output);
1702 set_tarval_mode_output_option(mode_Hs, &hex_output);
1703 set_tarval_mode_output_option(mode_Hu, &hex_output);
1704 set_tarval_mode_output_option(mode_Is, &hex_output);
1705 set_tarval_mode_output_option(mode_Iu, &hex_output);
1706 set_tarval_mode_output_option(mode_Ls, &hex_output);
1707 set_tarval_mode_output_option(mode_Lu, &hex_output);
1708 set_tarval_mode_output_option(mode_P, &hex_output);
1711 /* free all memory occupied by tarval. */
1712 void finish_tarval(void) {
1715 del_set(tarvals); tarvals = NULL;
1716 del_set(values); values = NULL;
1719 /****************************************************************************
1721 ****************************************************************************/