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 tv.value = INSERT_VALUE(value, length);
175 /* if there is such a tarval, it is returned, else tv is copied
177 return (tarval *)INSERT_TARVAL(&tv);
183 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
187 switch (get_mode_sort(mode)) {
189 /* addresses always wrap around */
190 temp = alloca(sc_get_buffer_length());
191 sc_val_from_ulong(-1, temp);
192 sc_and(temp, value, temp);
193 /* the sc_ module expects that all bits are set ... */
194 sign_extend(temp, mode);
195 return get_tarval(temp, length, mode);
197 case irms_int_number:
198 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
199 switch (GET_OVERFLOW_MODE()) {
200 case TV_OVERFLOW_SATURATE:
201 return get_mode_max(mode);
202 case TV_OVERFLOW_WRAP:
203 temp = alloca(sc_get_buffer_length());
204 sc_val_from_ulong(-1, temp);
205 sc_and(temp, value, temp);
206 /* the sc_ module expects that all bits are set ... */
207 sign_extend(temp, mode);
208 return get_tarval(temp, length, mode);
209 case TV_OVERFLOW_BAD:
212 return get_tarval(value, length, mode);
215 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
216 switch (GET_OVERFLOW_MODE()) {
217 case TV_OVERFLOW_SATURATE:
218 return get_mode_min(mode);
219 case TV_OVERFLOW_WRAP: {
220 char *temp = alloca(sc_get_buffer_length());
221 sc_val_from_ulong(-1, temp);
222 sc_and(temp, value, 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)) {
235 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
238 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
239 return get_mode_null(mode);
246 return get_tarval(value, length, mode);
250 * public variables declared in tv.h
252 static tarval reserved_tv[4];
254 tarval *tarval_bad = &reserved_tv[0];
255 tarval *tarval_undefined = &reserved_tv[1];
256 tarval *tarval_b_false = &reserved_tv[2];
257 tarval *tarval_b_true = &reserved_tv[3];
260 * public functions declared in tv.h
264 * Constructors =============================================================
266 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
272 switch (get_mode_sort(mode)) {
273 case irms_control_flow:
279 case irms_internal_boolean:
280 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
281 if (strcasecmp(str, "true")) return tarval_b_true;
282 else if (strcasecmp(str, "false")) return tarval_b_true;
284 /* XXX This is C semantics */
285 return atoi(str) ? tarval_b_true : tarval_b_false;
287 case irms_float_number:
288 switch (get_mode_size_bits(mode)) {
290 fc_val_from_str(str, len, 8, 23, NULL);
293 fc_val_from_str(str, len, 11, 52, NULL);
296 fc_val_from_str(str, len, 15, 64, NULL);
299 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
302 /* same as integer modes */
303 case irms_int_number:
304 sc_val_from_str(str, len, NULL, mode);
305 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
308 assert(0); /* can't be reached, can it? */
313 * helper function, create a tarval from long
315 tarval *new_tarval_from_long(long l, ir_mode *mode) {
318 switch (get_mode_sort(mode)) {
319 case irms_internal_boolean:
320 /* XXX C semantics ! */
321 return l ? tarval_b_true : tarval_b_false ;
324 /* same as integer modes */
325 case irms_int_number:
326 sc_val_from_long(l, NULL);
327 sign_extend(sc_get_buffer(), mode);
328 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
330 case irms_float_number:
331 return new_tarval_from_double((long double)l, mode);
334 assert(0 && "unsupported mode sort");
339 /* returns non-zero if can be converted to long */
340 int tarval_is_long(tarval *tv) {
341 if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
344 if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
345 /* the value might be too big to fit in a long */
346 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
347 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
348 /* really doesn't fit */
355 /* this might overflow the machine's long, so use only with small values */
356 long get_tarval_long(tarval* tv) {
357 assert(tarval_is_long(tv) && "tarval too big to fit in long");
359 return sc_val_to_long(tv->value);
362 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
363 assert(mode && (get_mode_sort(mode) == irms_float_number));
365 switch (get_mode_size_bits(mode)) {
367 fc_val_from_ieee754(d, 8, 23, NULL);
370 fc_val_from_ieee754(d, 11, 52, NULL);
373 fc_val_from_ieee754(d, 15, 64, NULL);
376 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
379 /* returns non-zero if can be converted to double */
380 int tarval_is_double(tarval *tv) {
383 return (get_mode_sort(tv->mode) == irms_float_number);
386 long double get_tarval_double(tarval *tv) {
387 assert(tarval_is_double(tv));
389 return fc_val_to_ieee754(tv->value);
394 * Access routines for tarval fields ========================================
397 /* get the mode of the tarval */
398 ir_mode *(get_tarval_mode)(const tarval *tv) {
399 return _get_tarval_mode(tv);
403 * Special value query functions ============================================
405 * These functions calculate and return a tarval representing the requested
407 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
408 * functions, but these are stored on initialization of the irmode module and
409 * therefore the irmode functions should be preferred to the functions below.
412 tarval *(get_tarval_bad)(void) {
413 return _get_tarval_bad();
416 tarval *(get_tarval_undefined)(void) {
417 return _get_tarval_undefined();
420 tarval *(get_tarval_b_false)(void) {
421 return _get_tarval_b_false();
424 tarval *(get_tarval_b_true)(void) {
425 return _get_tarval_b_true();
428 tarval *get_tarval_max(ir_mode *mode) {
431 if (get_mode_n_vector_elems(mode) > 1) {
432 /* vector arithmetic not implemented yet */
436 switch(get_mode_sort(mode)) {
437 case irms_control_flow:
443 case irms_internal_boolean:
444 return tarval_b_true;
446 case irms_float_number:
447 switch(get_mode_size_bits(mode)) {
449 fc_get_max(8, 23, NULL);
452 fc_get_max(11, 52, NULL);
455 fc_get_max(15, 64, NULL);
458 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
461 case irms_int_number:
462 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
463 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
468 tarval *get_tarval_min(ir_mode *mode) {
471 if (get_mode_n_vector_elems(mode) > 1) {
472 /* vector arithmetic not implemented yet */
476 switch(get_mode_sort(mode)) {
477 case irms_control_flow:
483 case irms_internal_boolean:
484 return tarval_b_false;
486 case irms_float_number:
487 switch(get_mode_size_bits(mode)) {
489 fc_get_min(8, 23, NULL);
492 fc_get_min(11, 52, NULL);
495 fc_get_min(15, 64, NULL);
498 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
501 case irms_int_number:
502 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
503 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
508 /** The bit pattern for the pointer NULL */
509 static long _null_value = 0;
511 tarval *get_tarval_null(ir_mode *mode) {
514 if (get_mode_n_vector_elems(mode) > 1) {
515 /* vector arithmetic not implemented yet */
519 switch(get_mode_sort(mode)) {
520 case irms_control_flow:
523 case irms_internal_boolean:
527 case irms_float_number:
528 return new_tarval_from_double(0.0, mode);
530 case irms_int_number:
531 return new_tarval_from_long(0l, mode);
534 return new_tarval_from_long(_null_value, mode);
539 tarval *get_tarval_one(ir_mode *mode) {
542 if (get_mode_n_vector_elems(mode) > 1) {
543 /* vector arithmetic not implemented yet */
547 switch(get_mode_sort(mode)) {
548 case irms_control_flow:
551 case irms_internal_boolean:
556 case irms_float_number:
557 return new_tarval_from_double(1.0, mode);
559 case irms_int_number:
560 return new_tarval_from_long(1l, mode);
566 tarval *get_tarval_minus_one(ir_mode *mode) {
569 if (get_mode_n_vector_elems(mode) > 1) {
570 /* vector arithmetic not implemented yet */
574 switch(get_mode_sort(mode)) {
575 case irms_control_flow:
578 case irms_internal_boolean:
583 case irms_float_number:
584 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
586 case irms_int_number:
587 return new_tarval_from_long(-1l, mode);
592 tarval *get_tarval_nan(ir_mode *mode) {
595 if (get_mode_n_vector_elems(mode) > 1) {
596 /* vector arithmetic not implemented yet */
600 if (get_mode_sort(mode) == irms_float_number) {
601 switch(get_mode_size_bits(mode)) {
603 fc_get_qnan(8, 23, NULL);
606 fc_get_qnan(11, 52, NULL);
609 fc_get_qnan(15, 64, NULL);
612 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
614 assert(0 && "tarval is not floating point");
619 tarval *get_tarval_plus_inf(ir_mode *mode) {
622 if (get_mode_n_vector_elems(mode) > 1) {
623 /* vector arithmetic not implemented yet */
627 if (get_mode_sort(mode) == irms_float_number) {
628 switch(get_mode_size_bits(mode)) {
630 fc_get_plusinf(8, 23, NULL);
633 fc_get_plusinf(11, 52, NULL);
636 fc_get_plusinf(15, 64, NULL);
639 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
641 assert(0 && "tarval is not floating point");
646 tarval *get_tarval_minus_inf(ir_mode *mode) {
649 if (get_mode_n_vector_elems(mode) > 1) {
650 /* vector arithmetic not implemented yet */
654 if (get_mode_sort(mode) == irms_float_number) {
655 switch(get_mode_size_bits(mode)) {
657 fc_get_minusinf(8, 23, NULL);
660 fc_get_minusinf(11, 52, NULL);
663 fc_get_minusinf(15, 64, NULL);
666 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
668 assert(0 && "tarval is not floating point");
674 * Arithmethic operations on tarvals ========================================
678 * test if negative number, 1 means 'yes'
680 int tarval_is_negative(tarval *a) {
683 if (get_mode_n_vector_elems(a->mode) > 1) {
684 /* vector arithmetic not implemented yet */
685 assert(0 && "tarval_is_negative is not allowed for vector modes");
689 switch (get_mode_sort(a->mode)) {
690 case irms_int_number:
691 if (!mode_is_signed(a->mode)) return 0;
693 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
695 case irms_float_number:
696 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
699 assert(0 && "not implemented");
705 * test if null, 1 means 'yes'
707 int tarval_is_null(tarval *a) {
708 ir_mode *m = get_tarval_mode(a);
710 return a == get_tarval_null(m);
714 * test if one, 1 means 'yes'
716 int tarval_is_one(tarval *a) {
717 ir_mode *m = get_tarval_mode(a);
719 return a == get_tarval_one(m);
725 pn_Cmp tarval_cmp(tarval *a, tarval *b) {
729 if (a == tarval_bad || b == tarval_bad) {
730 assert(0 && "Comparison with tarval_bad");
734 if (a == tarval_undefined || b == tarval_undefined)
737 if (a->mode != b->mode)
740 if (get_mode_n_vector_elems(a->mode) > 1) {
741 /* vector arithmetic not implemented yet */
742 assert(0 && "cmp not implemented for vector modes");
745 /* Here the two tarvals are unequal and of the same mode */
746 switch (get_mode_sort(a->mode)) {
747 case irms_control_flow:
754 case irms_float_number:
758 * BEWARE: we cannot compare a == b here, because
759 * a NaN is always Unordered to any other value, even to itself!
761 switch (fc_comp(a->value, b->value)) {
762 case -1: return pn_Cmp_Lt;
763 case 0: return pn_Cmp_Eq;
764 case 1: return pn_Cmp_Gt;
765 case 2: return pn_Cmp_Uo;
766 default: return pn_Cmp_False;
769 case irms_int_number:
772 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
774 case irms_internal_boolean:
777 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
783 * convert to other mode
785 tarval *tarval_convert_to(tarval *src, ir_mode *dst_mode) {
792 if (src->mode == dst_mode) return src;
794 if (get_mode_n_vector_elems(src->mode) > 1) {
795 /* vector arithmetic not implemented yet */
799 switch (get_mode_sort(src->mode)) {
800 case irms_control_flow:
805 /* cast float to something */
806 case irms_float_number:
807 switch (get_mode_sort(dst_mode)) {
808 case irms_float_number:
809 switch (get_mode_size_bits(dst_mode)) {
811 fc_cast(src->value, 8, 23, NULL);
814 fc_cast(src->value, 11, 52, NULL);
817 fc_cast(src->value, 15, 64, NULL);
822 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
824 case irms_int_number:
825 switch (GET_FLOAT_TO_INT_MODE()) {
827 res = fc_int(src->value, NULL);
830 res = fc_rnd(src->value, NULL);
836 if (! fc_flt2int(res, sc_get_buffer(), dst_mode))
838 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), dst_mode);
841 /* the rest can't be converted */
846 /* cast int/characters to something */
847 case irms_int_number:
848 switch (get_mode_sort(dst_mode)) {
851 case irms_int_number:
852 buffer = alloca(sc_get_buffer_length());
853 memcpy(buffer, src->value, sc_get_buffer_length());
854 sign_extend(buffer, dst_mode);
855 return get_tarval_overflow(buffer, src->length, dst_mode);
857 case irms_internal_boolean:
858 /* XXX C semantics */
859 if (src == get_mode_null(src->mode)) return tarval_b_false;
860 else return tarval_b_true;
862 case irms_float_number:
863 /* XXX floating point unit does not understand internal integer
864 * representation, convert to string first, then create float from
866 buffer = alloca(100);
867 /* decimal string representation because hexadecimal output is
868 * interpreted unsigned by fc_val_from_str, so this is a HACK */
869 snprintf(buffer, 100, "%s",
870 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
871 buffer[100 - 1] = '\0';
872 switch (get_mode_size_bits(dst_mode)) {
874 fc_val_from_str(buffer, 0, 8, 23, NULL);
877 fc_val_from_str(buffer, 0, 11, 52, NULL);
880 fc_val_from_str(buffer, 0, 15, 64, NULL);
883 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
890 case irms_internal_boolean:
891 /* beware: this is C semantic for the INTERNAL boolean mode */
892 if (get_mode_sort(dst_mode) == irms_int_number)
893 return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
897 if (get_mode_sort(dst_mode) == irms_int_number) {
898 buffer = alloca(sc_get_buffer_length());
899 memcpy(buffer, src->value, sc_get_buffer_length());
900 sign_extend(buffer, src->mode);
901 return get_tarval_overflow(buffer, src->length, dst_mode);
912 tarval *tarval_not(tarval *a) {
917 /* works for vector mode without changes */
919 switch (get_mode_sort(a->mode)) {
920 case irms_int_number:
921 buffer = alloca(sc_get_buffer_length());
922 sc_not(a->value, buffer);
923 return get_tarval(buffer, a->length, a->mode);
925 case irms_internal_boolean:
926 if (a == tarval_b_true)
927 return tarval_b_false;
928 if (a == tarval_b_false)
929 return tarval_b_true;
933 assert(0 && "bitwise negation is only allowed for integer and boolean");
939 * arithmetic negation
941 tarval *tarval_neg(tarval *a) {
945 assert(mode_is_num(a->mode)); /* negation only for numerical values */
947 /* note: negation is allowed even for unsigned modes. */
949 if (get_mode_n_vector_elems(a->mode) > 1) {
950 /* vector arithmetic not implemented yet */
954 switch (get_mode_sort(a->mode)) {
955 case irms_int_number:
956 buffer = alloca(sc_get_buffer_length());
957 sc_neg(a->value, buffer);
958 return get_tarval_overflow(buffer, a->length, a->mode);
960 case irms_float_number:
964 fc_neg(a->value, NULL);
965 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
975 tarval *tarval_add(tarval *a, tarval *b) {
980 assert(a->mode == b->mode);
982 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
983 /* vector arithmetic not implemented yet */
987 switch (get_mode_sort(a->mode)) {
988 case irms_int_number:
989 /* modes of a,b are equal, so result has mode of a as this might be the character */
990 buffer = alloca(sc_get_buffer_length());
991 sc_add(a->value, b->value, buffer);
992 return get_tarval_overflow(buffer, a->length, a->mode);
994 case irms_float_number:
998 fc_add(a->value, b->value, NULL);
999 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1009 tarval *tarval_sub(tarval *a, tarval *b) {
1014 assert(a->mode == b->mode);
1016 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1017 /* vector arithmetic not implemented yet */
1020 switch (get_mode_sort(a->mode)) {
1021 case irms_int_number:
1022 /* modes of a,b are equal, so result has mode of a as this might be the character */
1023 buffer = alloca(sc_get_buffer_length());
1024 sc_sub(a->value, b->value, buffer);
1025 return get_tarval_overflow(buffer, a->length, a->mode);
1027 case irms_float_number:
1031 fc_sub(a->value, b->value, NULL);
1032 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1042 tarval *tarval_mul(tarval *a, tarval *b) {
1047 assert(a->mode == b->mode);
1049 if (get_mode_n_vector_elems(a->mode) > 1) {
1050 /* vector arithmetic not implemented yet */
1054 switch (get_mode_sort(a->mode)) {
1055 case irms_int_number:
1056 /* modes of a,b are equal */
1057 buffer = alloca(sc_get_buffer_length());
1058 sc_mul(a->value, b->value, buffer);
1059 return get_tarval_overflow(buffer, a->length, a->mode);
1061 case irms_float_number:
1065 fc_mul(a->value, b->value, NULL);
1066 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1074 * floating point division
1076 tarval *tarval_quo(tarval *a, tarval *b) {
1079 assert((a->mode == b->mode) && mode_is_float(a->mode));
1084 if (get_mode_n_vector_elems(a->mode) > 1) {
1085 /* vector arithmetic not implemented yet */
1089 fc_div(a->value, b->value, NULL);
1090 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1095 * overflow is impossible, but look out for division by zero
1097 tarval *tarval_div(tarval *a, tarval *b) {
1100 assert((a->mode == b->mode) && mode_is_int(a->mode));
1102 if (get_mode_n_vector_elems(a->mode) > 1) {
1103 /* vector arithmetic not implemented yet */
1108 if (b == get_mode_null(b->mode)) return tarval_bad;
1109 /* modes of a,b are equal */
1110 sc_div(a->value, b->value, NULL);
1111 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1116 * overflow is impossible, but look out for division by zero
1118 tarval *tarval_mod(tarval *a, tarval *b) {
1121 assert((a->mode == b->mode) && mode_is_int(a->mode));
1123 if (get_mode_n_vector_elems(a->mode) > 1) {
1124 /* vector arithmetic not implemented yet */
1129 if (b == get_mode_null(b->mode)) return tarval_bad;
1130 /* modes of a,b are equal */
1131 sc_mod(a->value, b->value, NULL);
1132 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1136 * integer division AND remainder
1137 * overflow is impossible, but look out for division by zero
1139 tarval *tarval_divmod(tarval *a, tarval *b, tarval **mod) {
1140 int len = sc_get_buffer_length();
1141 char *div_res = alloca(len);
1142 char *mod_res = alloca(len);
1146 assert((a->mode == b->mode) && mode_is_int(a->mode));
1148 if (get_mode_n_vector_elems(a->mode) > 1) {
1149 /* vector arithmetic not implemented yet */
1155 if (b == get_mode_null(b->mode)) return tarval_bad;
1156 /* modes of a,b are equal */
1157 sc_divmod(a->value, b->value, div_res, mod_res);
1158 *mod = get_tarval(mod_res, len, a->mode);
1159 return get_tarval(div_res, len, a->mode);
1165 tarval *tarval_abs(tarval *a) {
1169 assert(mode_is_num(a->mode));
1171 if (get_mode_n_vector_elems(a->mode) > 1) {
1172 /* vector arithmetic not implemented yet */
1176 switch (get_mode_sort(a->mode)) {
1177 case irms_int_number:
1178 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1179 buffer = alloca(sc_get_buffer_length());
1180 sc_neg(a->value, buffer);
1181 return get_tarval_overflow(buffer, a->length, a->mode);
1185 case irms_float_number:
1189 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1190 fc_neg(a->value, NULL);
1191 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1204 tarval *tarval_and(tarval *a, tarval *b) {
1207 assert(a->mode == b->mode);
1209 /* works even for vector modes */
1211 switch(get_mode_sort(a->mode)) {
1212 case irms_internal_boolean:
1213 return (a == tarval_b_false) ? a : b;
1215 case irms_int_number:
1216 sc_and(a->value, b->value, NULL);
1217 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1220 assert(0 && "operation not defined on mode");
1228 tarval *tarval_or (tarval *a, tarval *b) {
1231 assert(a->mode == b->mode);
1233 /* works even for vector modes */
1235 switch (get_mode_sort(a->mode)) {
1236 case irms_internal_boolean:
1237 return (a == tarval_b_true) ? a : b;
1239 case irms_int_number:
1240 sc_or(a->value, b->value, NULL);
1241 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1244 assert(0 && "operation not defined on mode");
1250 * bitwise exclusive or (xor)
1252 tarval *tarval_eor(tarval *a, tarval *b) {
1255 assert((a->mode == b->mode));
1257 /* works even for vector modes */
1259 switch (get_mode_sort(a->mode)) {
1260 case irms_internal_boolean:
1261 return (a == b)? tarval_b_false : tarval_b_true;
1263 case irms_int_number:
1264 sc_xor(a->value, b->value, NULL);
1265 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1268 assert(0 && "operation not defined on mode");
1274 * bitwise left shift
1276 tarval *tarval_shl(tarval *a, tarval *b) {
1277 char *temp_val = NULL;
1281 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1283 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1284 /* vector arithmetic not implemented yet */
1288 if (get_mode_modulo_shift(a->mode) != 0) {
1289 temp_val = alloca(sc_get_buffer_length());
1291 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1292 sc_mod(b->value, temp_val, temp_val);
1294 temp_val = (char*)b->value;
1296 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1297 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1301 * bitwise unsigned right shift
1303 tarval *tarval_shr(tarval *a, tarval *b) {
1304 char *temp_val = NULL;
1308 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1310 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1311 /* vector arithmetic not implemented yet */
1315 if (get_mode_modulo_shift(a->mode) != 0) {
1316 temp_val = alloca(sc_get_buffer_length());
1318 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1319 sc_mod(b->value, temp_val, temp_val);
1321 temp_val = (char*)b->value;
1323 sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1324 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1328 * bitwise signed right shift
1330 tarval *tarval_shrs(tarval *a, tarval *b) {
1331 char *temp_val = NULL;
1335 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1337 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1338 /* vector arithmetic not implemented yet */
1342 if (get_mode_modulo_shift(a->mode) != 0) {
1343 temp_val = alloca(sc_get_buffer_length());
1345 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1346 sc_mod(b->value, temp_val, temp_val);
1348 temp_val = (char*)b->value;
1350 sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1351 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1357 tarval *tarval_rot(tarval *a, tarval *b) {
1358 char *temp_val = NULL;
1362 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1364 if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1365 /* vector arithmetic not implemented yet */
1369 if (get_mode_modulo_shift(a->mode) != 0) {
1370 temp_val = alloca(sc_get_buffer_length());
1372 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1373 sc_mod(b->value, temp_val, temp_val);
1375 temp_val = (char*)b->value;
1377 sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1378 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1382 * carry flag of the last operation
1384 int tarval_carry(void) {
1385 return sc_had_carry();
1391 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1392 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1396 const tarval_mode_info *mode_info;
1397 const char *prefix, *suffix;
1399 mode_info = tv->mode->tv_priv;
1401 mode_info = &default_info;
1402 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1403 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1405 switch (get_mode_sort(tv->mode)) {
1406 case irms_reference:
1407 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1409 case irms_int_number:
1410 switch (mode_info->mode_output) {
1413 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1417 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1423 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1426 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1428 case irms_float_number:
1429 switch (mode_info->mode_output) {
1431 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1434 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1439 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1443 case irms_internal_boolean:
1444 switch (mode_info->mode_output) {
1450 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1454 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1457 case irms_control_flow:
1459 case irms_auxiliary:
1460 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1467 * Output of tarvals to stdio.
1469 int tarval_printf(tarval *tv) {
1473 res = tarval_snprintf(buf, sizeof(buf), tv);
1474 assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1479 char *get_tarval_bitpattern(tarval *tv) {
1481 int n = get_mode_size_bits(tv->mode);
1482 int bytes = (n + 7) / 8;
1483 char *res = xmalloc((n + 1) * sizeof(char));
1486 for(i = 0; i < bytes; i++) {
1487 byte = get_tarval_sub_bits(tv, i);
1488 for(j = 1; j < 256; j <<= 1)
1490 res[pos++] = j & byte ? '1' : '0';
1499 * access to the bitpattern
1501 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1502 switch (get_mode_arithmetic(tv->mode)) {
1503 case irma_twos_complement:
1504 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1506 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1508 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1513 * Specify the output options of one mode.
1515 * This functions stores the modinfo, so DO NOT DESTROY it.
1517 * Returns zero on success.
1519 int set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1522 mode->tv_priv = modeinfo;
1527 * Returns the output options of one mode.
1529 * This functions returns the mode info of a given mode.
1531 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1534 return mode->tv_priv;
1538 * Identifying tarvals values for algebraic simplifications.
1541 * - TV_CLASSIFY_NULL for additive neutral or the NULL tarval for reference modes,
1542 * - TV_CLASSIFY_ONE for multiplicative neutral,
1543 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1544 * - TV_CLASSIFY_OTHER else
1546 tarval_classification_t classify_tarval(tarval *tv) {
1547 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1549 if (tv == get_mode_null(tv->mode))
1550 return TV_CLASSIFY_NULL;
1551 else if (tv == get_mode_one(tv->mode))
1552 return TV_CLASSIFY_ONE;
1553 else if ((get_mode_sort(tv->mode) == irms_int_number)
1554 && (tv == get_mode_minus_one(tv->mode)))
1555 return TV_CLASSIFY_ALL_ONE;
1557 return TV_CLASSIFY_OTHER;
1561 * Returns non-zero if a given (integer) tarval has only one single bit
1564 int tarval_is_single_bit(tarval *tv) {
1568 if (!tv || tv == tarval_bad) return 0;
1569 if (! mode_is_int(tv->mode)) return 0;
1571 l = get_mode_size_bytes(tv->mode);
1572 for (bits = 0, i = l - 1; i >= 0; --i) {
1573 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1575 /* check for more than one bit in these */
1587 * Returns non-zero if the mantissa of a floating point IEEE-754
1588 * tarval is zero (i.e. 1.0Exxx)
1590 int tarval_ieee754_zero_mantissa(tarval *tv) {
1591 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1592 return fc_zero_mantissa(tv->value);
1595 /* Returns the exponent of a floating point IEEE-754 tarval. */
1596 int tarval_ieee754_get_exponent(tarval *tv) {
1597 assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1598 return fc_get_exponent(tv->value);
1601 /* Set the immediate precision for IEEE-754 results. */
1602 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1603 return fc_set_immediate_precision(bits);
1606 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1607 unsigned tarval_ieee754_get_exact(void) {
1608 return fc_is_exact();
1612 * Sets the overflow mode for integer operations.
1614 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1615 int_overflow_mode = ov_mode;
1618 /* Get the overflow mode for integer operations. */
1619 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1620 return int_overflow_mode;
1623 /* Enable/Disable floating point constant folding. */
1624 int tarval_enable_fp_ops(int enable) {
1625 int old = !no_float;
1632 * default mode_info for output as HEX
1634 static const tarval_mode_info hex_output = {
1641 * Initialization of the tarval module: called before init_mode()
1643 void init_tarval_1(long null_value) {
1644 _null_value = null_value;
1646 /* initialize the sets holding the tarvals with a comparison function and
1647 * an initial size, which is the expected number of constants */
1648 tarvals = new_set(memcmp, N_CONSTANTS);
1649 values = new_set(memcmp, N_CONSTANTS);
1650 /* init strcalc with precision of 68 to support floating point values with 64
1651 * bit mantissa (needs extra bits for rounding and overflow) */
1657 * Initialization of the tarval module: called after init_mode()
1659 void init_tarval_2(void) {
1660 tarval_bad->kind = k_tarval;
1661 tarval_bad->mode = mode_BAD;
1662 tarval_bad->value = INT_TO_PTR(resid_tarval_bad);
1664 tarval_undefined->kind = k_tarval;
1665 tarval_undefined->mode = mode_ANY;
1666 tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1668 tarval_b_true->kind = k_tarval;
1669 tarval_b_true->mode = mode_b;
1670 tarval_b_true->value = INT_TO_PTR(resid_tarval_b_true);
1672 tarval_b_false->kind = k_tarval;
1673 tarval_b_false->mode = mode_b;
1674 tarval_b_false->value = INT_TO_PTR(resid_tarval_b_false);
1677 * assign output modes that are compatible with the
1678 * old implementation: Hex output
1680 set_tarval_mode_output_option(mode_Bs, &hex_output);
1681 set_tarval_mode_output_option(mode_Bu, &hex_output);
1682 set_tarval_mode_output_option(mode_Hs, &hex_output);
1683 set_tarval_mode_output_option(mode_Hu, &hex_output);
1684 set_tarval_mode_output_option(mode_Is, &hex_output);
1685 set_tarval_mode_output_option(mode_Iu, &hex_output);
1686 set_tarval_mode_output_option(mode_Ls, &hex_output);
1687 set_tarval_mode_output_option(mode_Lu, &hex_output);
1688 set_tarval_mode_output_option(mode_P, &hex_output);
1691 /* free all memory occupied by tarval. */
1692 void finish_tarval(void) {
1695 del_set(tarvals); tarvals = NULL;
1696 del_set(values); values = NULL;
1699 /****************************************************************************
1701 ****************************************************************************/