1 /* TV --- Target Values, aka Constant Table.
2 Copyright (C) 1995, 1996 Christian von Roques */
6 /****i* tv/implementation
12 * Values are stored in a format depending upon chosen arithmetic
13 * module. Default uses strcalc and fltcalc.
16 /* This implementation assumes:
17 * - target has IEEE-754 floating-point arithmetic. */
19 #include <assert.h> /* assertions */
20 #include <stdlib.h> /* atoi() */
21 #include <string.h> /* nice things for strings */
22 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
27 #include "set.h" /* to store tarvals in */
28 #include "tune.h" /* some constants */
29 #include "entity_t.h" /* needed to store pointers to entities */
30 #include "irmode.h" /* defines modes etc */
32 #include "irnode.h" /* defines boolean return values (pnc_number)*/
37 /* XXX hack until theres's a proper interface */
41 #define GET_OVERFLOW_MODE() BAD
43 /* unused, float to int doesn't work yet */
46 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
48 #define SWITCH_NOINFINITY 0
49 #define SWITCH_NODENORMALS 0
51 /****************************************************************************
52 * local definitions and macros
53 ****************************************************************************/
55 # define TARVAL_VERIFY(a) tarval_verify((a))
57 # define TARVAL_VERIFY(a) ((void)0)
60 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
61 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
63 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
64 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
66 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
68 static long long count = 0;
69 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
71 # define ANNOUNCE() ((void)0)
73 /****************************************************************************
75 ****************************************************************************/
76 static struct set *tarvals; /* container for tarval structs */
77 static struct set *values; /* container for values */
79 /****************************************************************************
81 ****************************************************************************/
83 static int hash_val(const void *value, unsigned int length);
84 static int hash_tv(tarval *tv);
85 static void _fail_verify(tarval *tv, const char* file, int line)
87 /* print a memory image of the tarval and throw an assertion */
89 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
91 printf("%s:%d: Invalid tarval (null)", file, line);
95 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
98 INLINE static void tarval_verify(tarval *tv)
104 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
105 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
107 if (!FIND_TARVAL(tv)) fail_verify(tv);
108 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
114 static int hash_tv(tarval *tv)
116 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
119 static int hash_val(const void *value, unsigned int length)
122 unsigned int hash = 0;
124 /* scramble the byte - array */
125 for (i = 0; i < length; i++)
127 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
128 hash += (hash << 11) ^ (hash >> 17);
134 /* finds tarval with value/mode or creates new tarval */
135 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
142 /* if there already is such a value, it is returned, else value
143 * is copied into the set */
144 tv.value = INSERT_VALUE(value, length);
148 /* if there is such a tarval, it is returned, else tv is copied
150 return (tarval *)INSERT_TARVAL(&tv);
153 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
155 switch (get_mode_sort(mode))
157 case irms_int_number:
158 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
159 switch (GET_OVERFLOW_MODE()) {
161 return get_mode_max(mode);
164 char *temp = alloca(sc_get_buffer_length());
165 char *diff = alloca(sc_get_buffer_length());
166 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
167 sc_val_from_ulong(1, temp);
168 sc_add(diff, temp, diff);
169 sc_sub(value, diff, temp);
170 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
171 sc_sub(temp, diff, temp);
172 return get_tarval(temp, length, mode);
177 return get_tarval(value, length, mode);
180 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
181 switch (GET_OVERFLOW_MODE()) {
183 return get_mode_min(mode);
186 char *temp = alloca(sc_get_buffer_length());
187 char *diff = alloca(sc_get_buffer_length());
188 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
189 sc_val_from_ulong(1, temp);
190 sc_add(diff, temp, diff);
191 sc_add(value, diff, temp);
192 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
193 sc_add(temp, diff, temp);
194 return get_tarval(temp, length, mode);
199 return get_tarval(value, length, mode);
204 case irms_float_number:
205 if (SWITCH_NOINFINITY && fc_is_inf(value))
207 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
210 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
212 return get_mode_null(mode);
218 return get_tarval(value, length, mode);
223 * public variables declared in tv.h
226 tarval *tarval_undefined;
227 tarval *tarval_b_false;
228 tarval *tarval_b_true;
229 tarval *tarval_P_void;
232 * public functions declared in tv.h
236 * Constructors =============================================================
238 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
245 switch (get_mode_sort(mode))
247 case irms_control_flow:
253 case irms_internal_boolean:
254 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
255 if (strcasecmp(str, "true")) return tarval_b_true;
256 else if (strcasecmp(str, "false")) return tarval_b_true;
258 /* XXX This is C semantics */
259 return atoi(str) ? tarval_b_true : tarval_b_false;
261 case irms_float_number:
262 switch(get_mode_size_bits(mode)) {
264 fc_val_from_str(str, len, 8, 23, NULL);
267 fc_val_from_str(str, len, 11, 52, NULL);
270 fc_val_from_str(str, len, 15, 64, NULL);
273 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
275 case irms_int_number:
277 sc_val_from_str(str, len, NULL);
278 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
281 return get_tarval(str, len, mode);
284 assert(0); /* can't be reached, can it? */
289 * helper function, create a tarval from long
291 tarval *new_tarval_from_long(long l, ir_mode *mode)
294 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
296 switch(get_mode_sort(mode))
298 case irms_internal_boolean:
299 /* XXX C semantics ! */
300 return l ? tarval_b_true : tarval_b_false ;
302 case irms_int_number:
304 sc_val_from_long(l, NULL);
305 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
307 case irms_float_number:
308 return new_tarval_from_double((long double)l, mode);
311 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
319 /* returns non-zero if can be converted to long */
320 int tarval_is_long(tarval *tv)
323 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
325 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
327 /* the value might be too big to fit in a long */
328 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
329 if (sc_comp(sc_get_buffer(), tv->value) == -1)
331 /* really doesn't fit */
338 /* this might overflow the machine's long, so use only with small values */
339 long tarval_to_long(tarval* tv)
342 assert(tarval_is_long(tv) && "tarval too big to fit in long");
344 return sc_val_to_long(tv->value);
347 tarval *new_tarval_from_double(long double d, ir_mode *mode)
350 assert(mode && (get_mode_sort(mode) == irms_float_number));
352 switch (get_mode_size_bits(mode)) {
354 fc_val_from_float(d, 8, 23, NULL);
357 fc_val_from_float(d, 11, 52, NULL);
360 fc_val_from_float(d, 15, 64, NULL);
363 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
366 /* returns non-zero if can be converted to double */
367 int tarval_is_double(tarval *tv)
372 return (get_mode_sort(tv->mode) == irms_float_number);
375 long double tarval_to_double(tarval *tv)
378 assert(tarval_is_double(tv));
380 return fc_val_to_float(tv->value);
383 /* The tarval represents the address of the entity. As the address must
384 be constant the entity must have as owner the global type. */
385 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
389 assert(mode && (get_mode_sort(mode) == irms_reference));
391 return get_tarval((void *)ent, 0, mode);
393 int tarval_is_entity(tarval *tv)
397 /* tv->value == NULL means dereferencing a null pointer */
398 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
399 && (tv != tarval_P_void));
402 entity *tarval_to_entity(tarval *tv)
407 if (tarval_is_entity(tv))
408 return (entity *)tv->value;
410 assert(0 && "tarval did not represent an entity");
415 void free_tarval_entity(entity *ent) {
416 /* There can be a tarval referencing this entity. Even if the
417 tarval is not used by the code any more, it can still reference
418 the entity as tarvals live indepently of the entity referenced.
419 Further the tarval is hashed into a set. If a hash function
420 evaluation happens to collide with this tarval, we will vrfy that
421 it contains a proper entity and we will crash if the entity is
424 Unluckily, tarvals can neither be changed nor deleted, and to find
425 one, all existing reference modes have to be tried -> a facility
426 to retrieve all modes of a kind is needed. */
431 * Access routines for tarval fields ========================================
433 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
441 * Special value query functions ============================================
443 * These functions calculate and return a tarval representing the requested
445 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
446 * functions, but these are stored on initialization of the irmode module and
447 * therefore the irmode functions should be prefered to the functions below.
450 tarval *get_tarval_bad(void)
455 tarval *get_tarval_undefined(void)
458 return tarval_undefined;
460 tarval *get_tarval_b_false(void)
463 return tarval_b_false;
465 tarval *get_tarval_b_true(void)
468 return tarval_b_true;
470 tarval *get_tarval_P_void(void)
473 return tarval_P_void;
476 tarval *get_tarval_max(ir_mode *mode)
481 switch(get_mode_sort(mode))
484 case irms_control_flow:
490 case irms_internal_boolean:
491 return tarval_b_true;
493 case irms_float_number:
494 switch(get_mode_size_bits(mode))
497 fc_get_max(8, 23, NULL);
500 fc_get_max(11, 52, NULL);
503 fc_get_max(15, 64, NULL);
506 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
508 case irms_int_number:
510 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
511 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
516 tarval *get_tarval_min(ir_mode *mode)
521 switch(get_mode_sort(mode))
524 case irms_control_flow:
530 case irms_internal_boolean:
531 return tarval_b_false;
533 case irms_float_number:
534 switch(get_mode_size_bits(mode))
537 fc_get_min(8, 23, NULL);
540 fc_get_min(11, 52, NULL);
543 fc_get_min(15, 64, NULL);
546 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
548 case irms_int_number:
550 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
551 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
556 tarval *get_tarval_null(ir_mode *mode)
561 switch(get_mode_sort(mode))
563 case irms_control_flow:
566 case irms_internal_boolean:
570 case irms_float_number:
571 return new_tarval_from_double(0.0, mode);
573 case irms_int_number:
575 return new_tarval_from_long(0l, mode);
578 return tarval_P_void;
583 tarval *get_tarval_one(ir_mode *mode)
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_nan(ir_mode *mode)
614 if (get_mode_sort(mode) == irms_float_number) {
615 switch(get_mode_size_bits(mode))
618 fc_get_qnan(8, 23, NULL);
621 fc_get_qnan(11, 52, NULL);
624 fc_get_qnan(15, 64, NULL);
627 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
630 assert(0 && "tarval is not floating point");
635 tarval *get_tarval_inf(ir_mode *mode)
640 if (get_mode_sort(mode) == irms_float_number) {
641 switch(get_mode_size_bits(mode))
644 fc_get_plusinf(8, 23, NULL);
647 fc_get_plusinf(11, 52, NULL);
650 fc_get_plusinf(15, 64, NULL);
653 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
656 assert(0 && "tarval is not floating point");
662 * Arithmethic operations on tarvals ========================================
666 * test if negative number, 1 means 'yes'
668 int tarval_is_negative(tarval *a)
673 switch (get_mode_sort(a->mode))
675 case irms_int_number:
676 if (!mode_is_signed(a->mode)) return 0;
678 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
680 case irms_float_number:
681 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
684 assert(0 && "not implemented");
690 * test if null, 1 means 'yes'
692 int tarval_is_null(tarval *a)
694 ir_mode *m = get_tarval_mode(a);
696 return a == get_tarval_null(m);
702 pnc_number tarval_cmp(tarval *a, tarval *b)
708 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
709 if (a == tarval_undefined || b == tarval_undefined) return False;
710 if (a == b) return Eq;
711 if (get_tarval_mode(a) != get_tarval_mode(b)) return False;
713 /* Here the two tarvals are unequal and of the same mode */
714 switch (get_mode_sort(a->mode))
716 case irms_control_flow:
722 case irms_float_number:
723 switch (fc_comp(a->value, b->value)) {
725 case 0: assert(0 && "different tarvals compare equal"); return Eq;
728 default: return False;
730 case irms_int_number:
732 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
734 case irms_internal_boolean:
735 return (a == tarval_b_true)?(Gt):(Lt);
741 * convert to other mode
743 tarval *tarval_convert_to(tarval *src, ir_mode *m)
751 if (src->mode == m) return src;
753 switch (get_mode_sort(src->mode))
755 case irms_control_flow:
760 /* cast float to something */
761 case irms_float_number:
762 switch (get_mode_sort(m)) {
763 case irms_float_number:
764 switch (get_mode_size_bits(m))
767 fc_cast(src->value, 8, 23, NULL);
770 fc_cast(src->value, 11, 52, NULL);
773 fc_cast(src->value, 15, 64, NULL);
778 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
781 case irms_int_number:
782 switch (GET_FLOAT_TO_INT_MODE())
785 fc_int(src->value, NULL);
788 fc_rnd(src->value, NULL);
793 /* XXX floating point unit can't produce a value in integer
795 * an intermediate representation is needed here first. */
796 /* return get_tarval(); */
801 /* the rest can't be converted */
806 /* cast int to something */
807 case irms_int_number:
808 switch (get_mode_sort(m)) {
809 case irms_int_number:
811 return get_tarval_overflow(src->value, src->length, m);
813 case irms_internal_boolean:
814 /* XXX C semantics */
815 if (src == get_mode_null(src->mode)) return tarval_b_false;
816 else return tarval_b_true;
818 case irms_float_number:
819 /* XXX floating point unit does not understand internal integer
820 * representation, convert to string first, then create float from
822 buffer = alloca(100);
823 /* decimal string representation because hexadecimal output is
824 * interpreted unsigned by fc_val_from_str, so this is a HACK */
825 snprintf(buffer, 100, "%s",
826 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
827 switch (get_mode_size_bits(m))
830 fc_val_from_str(buffer, 0, 8, 23, NULL);
833 fc_val_from_str(buffer, 0, 11, 52, NULL);
836 fc_val_from_str(buffer, 0, 15, 64, NULL);
839 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
846 case irms_internal_boolean:
847 switch (get_mode_sort(m))
849 case irms_int_number:
850 if (src == tarval_b_true) return get_mode_one(m);
851 else return get_mode_null(m);
870 tarval *tarval_neg(tarval *a)
876 assert(mode_is_num(a->mode)); /* negation only for numerical values */
877 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
879 switch (get_mode_sort(a->mode))
881 case irms_int_number:
882 buffer = alloca(sc_get_buffer_length());
883 sc_neg(a->value, buffer);
884 return get_tarval_overflow(buffer, a->length, a->mode);
886 case irms_float_number:
887 fc_neg(a->value, NULL);
888 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
898 tarval *tarval_add(tarval *a, tarval *b)
905 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
907 switch (get_mode_sort(a->mode))
910 case irms_int_number:
911 /* modes of a,b are equal, so result has mode of a as this might be the character */
912 buffer = alloca(sc_get_buffer_length());
913 sc_add(a->value, b->value, buffer);
914 return get_tarval_overflow(buffer, a->length, a->mode);
916 case irms_float_number:
917 fc_add(a->value, b->value, NULL);
918 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
928 tarval *tarval_sub(tarval *a, tarval *b)
935 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
937 switch (get_mode_sort(a->mode))
940 case irms_int_number:
941 /* modes of a,b are equal, so result has mode of a as this might be the character */
942 buffer = alloca(sc_get_buffer_length());
943 sc_sub(a->value, b->value, buffer);
944 return get_tarval_overflow(buffer, a->length, a->mode);
946 case irms_float_number:
947 fc_sub(a->value, b->value, NULL);
948 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
958 tarval *tarval_mul(tarval *a, tarval *b)
965 assert((a->mode == b->mode) && mode_is_num(a->mode));
967 switch (get_mode_sort(a->mode))
969 case irms_int_number:
970 /* modes of a,b are equal */
971 buffer = alloca(sc_get_buffer_length());
972 sc_mul(a->value, b->value, buffer);
973 return get_tarval_overflow(buffer, a->length, a->mode);
975 case irms_float_number:
976 fc_mul(a->value, b->value, NULL);
977 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
985 * floating point division
987 tarval *tarval_quo(tarval *a, tarval *b)
992 assert((a->mode == b->mode) && mode_is_float(a->mode));
994 fc_div(a->value, b->value, NULL);
995 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1000 * overflow is impossible, but look out for division by zero
1002 tarval *tarval_div(tarval *a, tarval *b)
1007 assert((a->mode == b->mode) && mode_is_int(a->mode));
1010 if (b == get_mode_null(b->mode)) return tarval_bad;
1011 /* modes of a,b are equal */
1012 sc_div(a->value, b->value, NULL);
1013 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1018 * overflow is impossible, but look out for division by zero
1020 tarval *tarval_mod(tarval *a, tarval *b)
1025 assert((a->mode == b->mode) && mode_is_int(a->mode));
1028 if (b == get_mode_null(b->mode)) return tarval_bad;
1029 /* modes of a,b are equal */
1030 sc_mod(a->value, b->value, NULL);
1031 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1037 tarval *tarval_abs(tarval *a)
1043 assert(mode_is_num(a->mode));
1045 switch (get_mode_sort(a->mode))
1047 case irms_int_number:
1048 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1050 buffer = alloca(sc_get_buffer_length());
1051 sc_neg(a->value, buffer);
1052 return get_tarval_overflow(buffer, a->length, a->mode);
1056 case irms_float_number:
1057 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1059 fc_neg(a->value, NULL);
1060 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1073 tarval *tarval_and(tarval *a, tarval *b)
1078 assert(a->mode == b->mode);
1080 switch(get_mode_sort(a->mode))
1082 case irms_internal_boolean:
1083 return (a == tarval_b_false) ? a : b;
1085 case irms_int_number:
1086 sc_and(a->value, b->value, NULL);
1087 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1090 assert(0 && "operation not defined on mode");
1098 tarval *tarval_or (tarval *a, tarval *b)
1103 assert(a->mode == b->mode);
1105 switch (get_mode_sort(a->mode))
1107 case irms_internal_boolean:
1108 return (a == tarval_b_true) ? a : b;
1110 case irms_int_number:
1111 sc_or(a->value, b->value, NULL);
1112 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1115 assert(0 && "operation not defined on mode");
1121 * bitwise exclusive or (xor)
1123 tarval *tarval_eor(tarval *a, tarval *b)
1128 assert((a->mode == b->mode));
1130 switch (get_mode_sort(a->mode))
1132 case irms_internal_boolean:
1133 return (a == b)? tarval_b_false : tarval_b_true;
1135 case irms_int_number:
1136 sc_or(a->value, b->value, NULL);
1137 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1140 assert(0 && "operation not defined on mode");
1146 * bitwise left shift
1148 tarval *tarval_shl(tarval *a, tarval *b)
1153 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1155 sc_shl(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1156 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1160 * bitwise unsigned right shift
1162 tarval *tarval_shr(tarval *a, tarval *b)
1167 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1169 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1170 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1174 * bitwise signed right shift
1176 tarval *tarval_shrs(tarval *a, tarval *b)
1181 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1183 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1184 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1190 tarval *tarval_rot(tarval *a, tarval *b)
1195 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1197 sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1198 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1205 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1207 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1211 const tarval_mode_info *mode_info;
1212 const char *prefix, *suffix;
1216 mode_info = tv->mode->tv_priv;
1218 mode_info = &default_info;
1219 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1220 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1222 switch (get_mode_sort(tv->mode))
1224 case irms_int_number:
1225 case irms_character:
1226 switch (mode_info->mode_output) {
1229 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1233 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1239 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1242 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1244 case irms_float_number:
1245 switch (mode_info->mode_output) {
1247 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1250 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1255 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1259 case irms_reference:
1260 if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1261 if (tv->value != NULL)
1262 if (tarval_is_entity(tv)) {
1263 if (get_entity_peculiarity((entity *)tv->value) == existent)
1264 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1266 if (mode_info->mode_output == TVO_NATIVE)
1267 return snprintf(buf, len, "NULL");
1269 return snprintf(buf, len, "0");
1273 if (size > tv->length) {
1274 memcpy(buf, tv->value, tv->length);
1275 buf[tv->length] = '\0';
1279 memcpy(buf, tv->value, size-1);
1285 return snprintf(buf, len, "void");
1287 case irms_internal_boolean:
1288 switch (mode_info->mode_output) {
1294 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1298 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1301 case irms_control_flow:
1303 case irms_auxiliary:
1304 return snprintf(buf, len, "<BAD>");
1312 * Output of tarvals to stdio.
1314 int tarval_printf(tarval *tv) {
1318 res = tarval_snprintf(buf, sizeof(buf), tv);
1319 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1325 char *tarval_bitpattern(tarval *tv)
1331 * access to the bitpattern
1333 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1335 switch (get_mode_sort(tv->mode)) {
1336 case irms_int_number:
1337 case irms_character:
1338 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1340 case irms_float_number:
1341 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1349 * Specify the output options of one mode.
1351 * This functions stores the modinfo, so DO NOT DESTROY it.
1353 * Returns zero on success.
1355 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1359 mode->tv_priv = modeinfo;
1364 * Returns the output options of one mode.
1366 * This functions returns the modinfo of a given mode.
1368 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1372 return mode->tv_priv;
1376 /* Identifying some tarvals ??? */
1377 /* Implemented in old tv.c as such:
1378 * return 0 for additive neutral,
1379 * 1 for multiplicative neutral,
1380 * -1 for bitwise-and neutral
1383 * Implemented for compatibility */
1384 long tarval_classify(tarval *tv)
1387 if (!tv || tv == tarval_bad) return 2;
1389 if (tv == get_mode_null(tv->mode)) return 0;
1390 else if (tv == get_mode_one(tv->mode)) return 1;
1391 else if ((get_mode_sort(tv->mode) == irms_int_number)
1392 && (tv == new_tarval_from_long(-1, tv->mode))) return -1;
1398 * default mode_info for output as HEX
1400 static const tarval_mode_info hex_output = {
1407 * default mode_info for output as reference
1409 static const tarval_mode_info reference_output = {
1417 * Initialization of the tarval module: called before init_mode()
1419 void init_tarval_1(void)
1422 /* initialize the sets holding the tarvals with a comparison function and
1423 * an initial size, which is the expected number of constants */
1424 tarvals = new_set(memcmp, TUNE_NCONSTANTS);
1425 values = new_set(memcmp, TUNE_NCONSTANTS);
1426 /* init strcalc with precision of 68 to support floating point values with 64
1427 * bit mantissa (needs extra bits for rounding and overflow) */
1431 tarval_bad = (tarval*)malloc(sizeof(tarval));
1432 tarval_undefined = (tarval*)malloc(sizeof(tarval));
1433 tarval_b_true = (tarval*)malloc(sizeof(tarval));
1434 tarval_b_false = (tarval*)malloc(sizeof(tarval));
1435 tarval_P_void = (tarval*)malloc(sizeof(tarval));
1439 * Initialization of the tarval module: called after init_mode()
1441 void init_tarval_2(void)
1445 tarval_bad->mode = mode_BAD;
1446 tarval_undefined->mode = mode_ANY;
1447 tarval_b_true->mode = mode_b;
1448 tarval_b_false->mode = mode_b;
1449 tarval_P_void->mode = mode_P;
1452 * assign output modes that are compatible with the
1453 * old implementation: Hex output
1455 tarval_set_mode_output_option(mode_U, &hex_output);
1456 tarval_set_mode_output_option(mode_C, &hex_output);
1457 tarval_set_mode_output_option(mode_Bs, &hex_output);
1458 tarval_set_mode_output_option(mode_Bu, &hex_output);
1459 tarval_set_mode_output_option(mode_Hs, &hex_output);
1460 tarval_set_mode_output_option(mode_Hu, &hex_output);
1461 tarval_set_mode_output_option(mode_Is, &hex_output);
1462 tarval_set_mode_output_option(mode_Iu, &hex_output);
1463 tarval_set_mode_output_option(mode_Ls, &hex_output);
1464 tarval_set_mode_output_option(mode_Lu, &hex_output);
1465 tarval_set_mode_output_option(mode_P, &reference_output);
1468 /****************************************************************************
1470 ****************************************************************************/