1 /* TV --- Target Values, aka Constant Table.
2 Copyright (C) 1995, 1996 Christian von Roques */
6 /****i* tv/implementation
13 * Values are stored in a format depending upon chosen arithmetic
14 * module. Default uses strcalc and fltcalc.
17 /* This implementation assumes:
18 * - target has IEEE-754 floating-point arithmetic. */
20 #include <assert.h> /* assertions */
21 #include <stdlib.h> /* atoi() */
22 #include <string.h> /* nice things for strings */
23 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
28 #include "set.h" /* to store tarvals in */
29 #include "tune.h" /* some constants */
30 #include "entity_t.h" /* needed to store pointers to entities */
31 #include "irmode.h" /* defines modes etc */
33 #include "irnode.h" /* defines boolean return values (pnc_number)*/
38 /* XXX hack until theres's a proper interface */
42 #define GET_OVERFLOW_MODE() BAD
44 /* unused, float to int doesn't work yet */
47 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
49 #define SWITCH_NOINFINITY 0
50 #define SWITCH_NODENORMALS 0
52 /****************************************************************************
53 * local definitions and macros
54 ****************************************************************************/
56 # define TARVAL_VERIFY(a) tarval_verify((a))
58 # define TARVAL_VERIFY(a) ((void)0)
61 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
62 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
64 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
65 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
67 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
69 static long long count = 0;
70 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
72 # define ANNOUNCE() ((void)0)
74 /****************************************************************************
76 ****************************************************************************/
77 static struct set *tarvals; /* container for tarval structs */
78 static struct set *values; /* container for values */
80 /****************************************************************************
82 ****************************************************************************/
84 static int hash_val(const void *value, unsigned int length);
85 static int hash_tv(tarval *tv);
86 static void _fail_verify(tarval *tv, const char* file, int line)
88 /* print a memory image of the tarval and throw an assertion */
90 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
92 printf("%s:%d: Invalid tarval (null)", file, line);
96 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
99 INLINE static void tarval_verify(tarval *tv)
105 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
106 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
108 if (!FIND_TARVAL(tv)) fail_verify(tv);
109 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
115 static int hash_tv(tarval *tv)
117 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
120 static int hash_val(const void *value, unsigned int length)
123 unsigned int hash = 0;
125 /* scramble the byte - array */
126 for (i = 0; i < length; i++)
128 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
129 hash += (hash << 11) ^ (hash >> 17);
135 /* finds tarval with value/mode or creates new tarval */
136 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
143 /* if there already is such a value, it is returned, else value
144 * is copied into the set */
145 tv.value = INSERT_VALUE(value, length);
149 /* if there is such a tarval, it is returned, else tv is copied
151 return (tarval *)INSERT_TARVAL(&tv);
154 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
156 switch (get_mode_sort(mode))
158 case irms_int_number:
159 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
160 switch (GET_OVERFLOW_MODE()) {
162 return get_mode_max(mode);
165 char *temp = alloca(sc_get_buffer_length());
166 char *diff = alloca(sc_get_buffer_length());
167 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
168 sc_val_from_ulong(1, temp);
169 sc_add(diff, temp, diff);
170 sc_sub(value, diff, temp);
171 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
172 sc_sub(temp, diff, temp);
173 return get_tarval(temp, length, mode);
178 return get_tarval(value, length, mode);
181 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
182 switch (GET_OVERFLOW_MODE()) {
184 return get_mode_min(mode);
187 char *temp = alloca(sc_get_buffer_length());
188 char *diff = alloca(sc_get_buffer_length());
189 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
190 sc_val_from_ulong(1, temp);
191 sc_add(diff, temp, diff);
192 sc_add(value, diff, temp);
193 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
194 sc_add(temp, diff, temp);
195 return get_tarval(temp, length, mode);
200 return get_tarval(value, length, mode);
205 case irms_float_number:
206 if (SWITCH_NOINFINITY && fc_is_inf(value))
208 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
211 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
213 return get_mode_null(mode);
219 return get_tarval(value, length, mode);
224 * public variables declared in tv.h
227 tarval *tarval_undefined;
228 tarval *tarval_b_false;
229 tarval *tarval_b_true;
230 tarval *tarval_P_void;
233 * public functions declared in tv.h
237 * Constructors =============================================================
239 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
246 switch (get_mode_sort(mode))
248 case irms_control_flow:
254 case irms_internal_boolean:
255 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
256 if (strcasecmp(str, "true")) return tarval_b_true;
257 else if (strcasecmp(str, "false")) return tarval_b_true;
259 /* XXX This is C semantics */
260 return atoi(str) ? tarval_b_true : tarval_b_false;
262 case irms_float_number:
263 switch(get_mode_size_bits(mode)) {
265 fc_val_from_str(str, len, 8, 23, NULL);
268 fc_val_from_str(str, len, 11, 52, NULL);
271 fc_val_from_str(str, len, 15, 64, NULL);
274 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
276 case irms_int_number:
278 sc_val_from_str(str, len, NULL);
279 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
282 return get_tarval(str, len, mode);
285 assert(0); /* can't be reached, can it? */
290 * helper function, create a tarval from long
292 tarval *new_tarval_from_long(long l, ir_mode *mode)
295 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
297 switch(get_mode_sort(mode))
299 case irms_internal_boolean:
300 /* XXX C semantics ! */
301 return l ? tarval_b_true : tarval_b_false ;
303 case irms_int_number:
305 sc_val_from_long(l, NULL);
306 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
308 case irms_float_number:
309 return new_tarval_from_double((long double)l, mode);
312 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
320 /* returns non-zero if can be converted to long */
321 int tarval_is_long(tarval *tv)
324 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
326 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
328 /* the value might be too big to fit in a long */
329 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
330 if (sc_comp(sc_get_buffer(), tv->value) == -1)
332 /* really doesn't fit */
339 /* this might overflow the machine's long, so use only with small values */
340 long tarval_to_long(tarval* tv)
343 assert(tarval_is_long(tv) && "tarval too big to fit in long");
345 return sc_val_to_long(tv->value);
348 tarval *new_tarval_from_double(long double d, ir_mode *mode)
351 assert(mode && (get_mode_sort(mode) == irms_float_number));
353 switch (get_mode_size_bits(mode)) {
355 fc_val_from_float(d, 8, 23, NULL);
358 fc_val_from_float(d, 11, 52, NULL);
361 fc_val_from_float(d, 15, 64, NULL);
364 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
367 /* returns non-zero if can be converted to double */
368 int tarval_is_double(tarval *tv)
373 return (get_mode_sort(tv->mode) == irms_float_number);
376 long double tarval_to_double(tarval *tv)
379 assert(tarval_is_double(tv));
381 return fc_val_to_float(tv->value);
384 /* The tarval represents the address of the entity. As the address must
385 be constant the entity must have as owner the global type. */
386 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
390 assert(mode && (get_mode_sort(mode) == irms_reference));
392 return get_tarval((void *)ent, 0, mode);
394 int tarval_is_entity(tarval *tv)
398 /* tv->value == NULL means dereferencing a null pointer */
399 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
400 && (tv != tarval_P_void));
403 entity *tarval_to_entity(tarval *tv)
408 if (tarval_is_entity(tv))
409 return (entity *)tv->value;
411 assert(0 && "tarval did not represent an entity");
416 void free_tarval_entity(entity *ent) {
417 /* There can be a tarval referencing this entity. Even if the
418 tarval is not used by the code any more, it can still reference
419 the entity as tarvals live indepently of the entity referenced.
420 Further the tarval is hashed into a set. If a hash function
421 evaluation happens to collide with this tarval, we will vrfy that
422 it contains a proper entity and we will crash if the entity is
425 Unluckily, tarvals can neither be changed nor deleted, and to find
426 one, all existing reference modes have to be tried -> a facility
427 to retrieve all modes of a kind is needed. */
432 * Access routines for tarval fields ========================================
434 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
442 * Special value query functions ============================================
444 * These functions calculate and return a tarval representing the requested
446 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
447 * functions, but these are stored on initialization of the irmode module and
448 * therefore the irmode functions should be prefered to the functions below.
451 tarval *get_tarval_bad(void)
456 tarval *get_tarval_undefined(void)
459 return tarval_undefined;
461 tarval *get_tarval_b_false(void)
464 return tarval_b_false;
466 tarval *get_tarval_b_true(void)
469 return tarval_b_true;
471 tarval *get_tarval_P_void(void)
474 return tarval_P_void;
477 tarval *get_tarval_max(ir_mode *mode)
482 switch(get_mode_sort(mode))
485 case irms_control_flow:
491 case irms_internal_boolean:
492 return tarval_b_true;
494 case irms_float_number:
495 switch(get_mode_size_bits(mode))
498 fc_get_max(8, 23, NULL);
501 fc_get_max(11, 52, NULL);
504 fc_get_max(15, 64, NULL);
507 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
509 case irms_int_number:
511 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
512 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
517 tarval *get_tarval_min(ir_mode *mode)
522 switch(get_mode_sort(mode))
525 case irms_control_flow:
531 case irms_internal_boolean:
532 return tarval_b_false;
534 case irms_float_number:
535 switch(get_mode_size_bits(mode))
538 fc_get_min(8, 23, NULL);
541 fc_get_min(11, 52, NULL);
544 fc_get_min(15, 64, NULL);
547 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
549 case irms_int_number:
551 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
552 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
557 tarval *get_tarval_null(ir_mode *mode)
562 switch(get_mode_sort(mode))
564 case irms_control_flow:
567 case irms_internal_boolean:
571 case irms_float_number:
572 return new_tarval_from_double(0.0, mode);
574 case irms_int_number:
576 return new_tarval_from_long(0l, mode);
579 return tarval_P_void;
584 tarval *get_tarval_one(ir_mode *mode)
589 switch(get_mode_sort(mode))
591 case irms_control_flow:
594 case irms_internal_boolean:
599 case irms_float_number:
600 return new_tarval_from_double(1.0, mode);
602 case irms_int_number:
604 return new_tarval_from_long(1l, mode);
610 tarval *get_tarval_nan(ir_mode *mode)
615 if (get_mode_sort(mode) == irms_float_number) {
616 switch(get_mode_size_bits(mode))
619 fc_get_qnan(8, 23, NULL);
622 fc_get_qnan(11, 52, NULL);
625 fc_get_qnan(15, 64, NULL);
628 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
631 assert(0 && "tarval is not floating point");
636 tarval *get_tarval_inf(ir_mode *mode)
641 if (get_mode_sort(mode) == irms_float_number) {
642 switch(get_mode_size_bits(mode))
645 fc_get_plusinf(8, 23, NULL);
648 fc_get_plusinf(11, 52, NULL);
651 fc_get_plusinf(15, 64, NULL);
654 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
657 assert(0 && "tarval is not floating point");
663 * Arithmethic operations on tarvals ========================================
667 * test if negative number, 1 means 'yes'
669 int tarval_is_negative(tarval *a)
674 switch (get_mode_sort(a->mode))
676 case irms_int_number:
677 if (!mode_is_signed(a->mode)) return 0;
679 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
681 case irms_float_number:
682 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
685 assert(0 && "not implemented");
691 * test if null, 1 means 'yes'
693 int tarval_is_null(tarval *a)
695 ir_mode *m = get_tarval_mode(a);
697 return a == get_tarval_null(m);
703 pnc_number tarval_cmp(tarval *a, tarval *b)
709 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
710 if (a == tarval_undefined || b == tarval_undefined) return False;
711 if (a == b) return Eq;
712 if (get_tarval_mode(a) != get_tarval_mode(b)) return False;
714 /* Here the two tarvals are unequal and of the same mode */
715 switch (get_mode_sort(a->mode))
717 case irms_control_flow:
723 case irms_float_number:
724 switch (fc_comp(a->value, b->value)) {
726 case 0: assert(0 && "different tarvals compare equal"); return Eq;
729 default: return False;
731 case irms_int_number:
733 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
735 case irms_internal_boolean:
736 return (a == tarval_b_true)?(Gt):(Lt);
742 * convert to other mode
744 tarval *tarval_convert_to(tarval *src, ir_mode *m)
752 if (src->mode == m) return src;
754 switch (get_mode_sort(src->mode))
756 case irms_control_flow:
761 /* cast float to something */
762 case irms_float_number:
763 switch (get_mode_sort(m)) {
764 case irms_float_number:
765 switch (get_mode_size_bits(m))
768 fc_cast(src->value, 8, 23, NULL);
771 fc_cast(src->value, 11, 52, NULL);
774 fc_cast(src->value, 15, 64, NULL);
779 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
782 case irms_int_number:
783 switch (GET_FLOAT_TO_INT_MODE())
786 fc_int(src->value, NULL);
789 fc_rnd(src->value, NULL);
794 /* XXX floating point unit can't produce a value in integer
796 * an intermediate representation is needed here first. */
797 /* return get_tarval(); */
802 /* the rest can't be converted */
807 /* cast int to something */
808 case irms_int_number:
809 switch (get_mode_sort(m)) {
810 case irms_int_number:
812 return get_tarval_overflow(src->value, src->length, m);
814 case irms_internal_boolean:
815 /* XXX C semantics */
816 if (src == get_mode_null(src->mode)) return tarval_b_false;
817 else return tarval_b_true;
819 case irms_float_number:
820 /* XXX floating point unit does not understand internal integer
821 * representation, convert to string first, then create float from
823 buffer = alloca(100);
824 /* decimal string representation because hexadecimal output is
825 * interpreted unsigned by fc_val_from_str, so this is a HACK */
826 snprintf(buffer, 100, "%s",
827 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
828 switch (get_mode_size_bits(m))
831 fc_val_from_str(buffer, 0, 8, 23, NULL);
834 fc_val_from_str(buffer, 0, 11, 52, NULL);
837 fc_val_from_str(buffer, 0, 15, 64, NULL);
840 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
847 case irms_internal_boolean:
848 switch (get_mode_sort(m))
850 case irms_int_number:
851 if (src == tarval_b_true) return get_mode_one(m);
852 else return get_mode_null(m);
871 tarval *tarval_neg(tarval *a)
877 assert(mode_is_num(a->mode)); /* negation only for numerical values */
878 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
880 switch (get_mode_sort(a->mode))
882 case irms_int_number:
883 buffer = alloca(sc_get_buffer_length());
884 sc_neg(a->value, buffer);
885 return get_tarval_overflow(buffer, a->length, a->mode);
887 case irms_float_number:
888 fc_neg(a->value, NULL);
889 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
899 tarval *tarval_add(tarval *a, tarval *b)
906 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
908 switch (get_mode_sort(a->mode))
911 case irms_int_number:
912 /* modes of a,b are equal, so result has mode of a as this might be the character */
913 buffer = alloca(sc_get_buffer_length());
914 sc_add(a->value, b->value, buffer);
915 return get_tarval_overflow(buffer, a->length, a->mode);
917 case irms_float_number:
918 fc_add(a->value, b->value, NULL);
919 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
929 tarval *tarval_sub(tarval *a, tarval *b)
936 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
938 switch (get_mode_sort(a->mode))
941 case irms_int_number:
942 /* modes of a,b are equal, so result has mode of a as this might be the character */
943 buffer = alloca(sc_get_buffer_length());
944 sc_sub(a->value, b->value, buffer);
945 return get_tarval_overflow(buffer, a->length, a->mode);
947 case irms_float_number:
948 fc_sub(a->value, b->value, NULL);
949 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
959 tarval *tarval_mul(tarval *a, tarval *b)
966 assert((a->mode == b->mode) && mode_is_num(a->mode));
968 switch (get_mode_sort(a->mode))
970 case irms_int_number:
971 /* modes of a,b are equal */
972 buffer = alloca(sc_get_buffer_length());
973 sc_mul(a->value, b->value, buffer);
974 return get_tarval_overflow(buffer, a->length, a->mode);
976 case irms_float_number:
977 fc_mul(a->value, b->value, NULL);
978 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
986 * floating point division
988 tarval *tarval_quo(tarval *a, tarval *b)
993 assert((a->mode == b->mode) && mode_is_float(a->mode));
995 fc_div(a->value, b->value, NULL);
996 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1001 * overflow is impossible, but look out for division by zero
1003 tarval *tarval_div(tarval *a, tarval *b)
1008 assert((a->mode == b->mode) && mode_is_int(a->mode));
1011 if (b == get_mode_null(b->mode)) return tarval_bad;
1012 /* modes of a,b are equal */
1013 sc_div(a->value, b->value, NULL);
1014 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1019 * overflow is impossible, but look out for division by zero
1021 tarval *tarval_mod(tarval *a, tarval *b)
1026 assert((a->mode == b->mode) && mode_is_int(a->mode));
1029 if (b == get_mode_null(b->mode)) return tarval_bad;
1030 /* modes of a,b are equal */
1031 sc_mod(a->value, b->value, NULL);
1032 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1038 tarval *tarval_abs(tarval *a)
1044 assert(mode_is_num(a->mode));
1046 switch (get_mode_sort(a->mode))
1048 case irms_int_number:
1049 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1051 buffer = alloca(sc_get_buffer_length());
1052 sc_neg(a->value, buffer);
1053 return get_tarval_overflow(buffer, a->length, a->mode);
1057 case irms_float_number:
1058 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1060 fc_neg(a->value, NULL);
1061 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1074 tarval *tarval_and(tarval *a, tarval *b)
1079 assert(a->mode == b->mode);
1081 switch(get_mode_sort(a->mode))
1083 case irms_internal_boolean:
1084 return (a == tarval_b_false) ? a : b;
1086 case irms_int_number:
1087 sc_and(a->value, b->value, NULL);
1088 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1091 assert(0 && "operation not defined on mode");
1099 tarval *tarval_or (tarval *a, tarval *b)
1104 assert(a->mode == b->mode);
1106 switch (get_mode_sort(a->mode))
1108 case irms_internal_boolean:
1109 return (a == tarval_b_true) ? a : b;
1111 case irms_int_number:
1112 sc_or(a->value, b->value, NULL);
1113 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1116 assert(0 && "operation not defined on mode");
1122 * bitwise exclusive or (xor)
1124 tarval *tarval_eor(tarval *a, tarval *b)
1129 assert((a->mode == b->mode));
1131 switch (get_mode_sort(a->mode))
1133 case irms_internal_boolean:
1134 return (a == b)? tarval_b_false : tarval_b_true;
1136 case irms_int_number:
1137 sc_or(a->value, b->value, NULL);
1138 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1141 assert(0 && "operation not defined on mode");
1147 * bitwise left shift
1149 tarval *tarval_shl(tarval *a, tarval *b)
1154 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1156 sc_shl(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1157 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1161 * bitwise unsigned right shift
1163 tarval *tarval_shr(tarval *a, tarval *b)
1168 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1170 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1171 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1175 * bitwise signed right shift
1177 tarval *tarval_shrs(tarval *a, tarval *b)
1182 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1184 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1185 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1191 tarval *tarval_rot(tarval *a, tarval *b)
1196 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1198 sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1199 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1206 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1208 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1212 const tarval_mode_info *mode_info;
1213 const char *prefix, *suffix;
1217 mode_info = tv->mode->tv_priv;
1219 mode_info = &default_info;
1220 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1221 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1223 switch (get_mode_sort(tv->mode))
1225 case irms_int_number:
1226 case irms_character:
1227 switch (mode_info->mode_output) {
1230 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1234 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1240 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1243 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1245 case irms_float_number:
1246 switch (mode_info->mode_output) {
1248 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1251 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1256 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1260 case irms_reference:
1261 if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1262 if (tv->value != NULL)
1263 if (tarval_is_entity(tv)) {
1264 if (get_entity_peculiarity((entity *)tv->value) == existent)
1265 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1267 if (mode_info->mode_output == TVO_NATIVE)
1268 return snprintf(buf, len, "NULL");
1270 return snprintf(buf, len, "0");
1274 if (size > tv->length) {
1275 memcpy(buf, tv->value, tv->length);
1276 buf[tv->length] = '\0';
1280 memcpy(buf, tv->value, size-1);
1286 return snprintf(buf, len, "void");
1288 case irms_internal_boolean:
1289 switch (mode_info->mode_output) {
1295 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1299 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1302 case irms_control_flow:
1304 case irms_auxiliary:
1305 return snprintf(buf, len, "<BAD>");
1313 * Output of tarvals to stdio.
1315 int tarval_printf(tarval *tv) {
1319 res = tarval_snprintf(buf, sizeof(buf), tv);
1320 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1326 char *tarval_bitpattern(tarval *tv)
1332 * access to the bitpattern
1334 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1336 switch (get_mode_sort(tv->mode)) {
1337 case irms_int_number:
1338 case irms_character:
1339 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1341 case irms_float_number:
1342 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1350 * Specify the output options of one mode.
1352 * This functions stores the modinfo, so DO NOT DESTROY it.
1354 * Returns zero on success.
1356 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1360 mode->tv_priv = modeinfo;
1365 * Returns the output options of one mode.
1367 * This functions returns the modinfo of a given mode.
1369 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1373 return mode->tv_priv;
1377 /* Identifying some tarvals ??? */
1378 /* Implemented in old tv.c as such:
1379 * return 0 for additive neutral,
1380 * 1 for multiplicative neutral,
1381 * -1 for bitwise-and neutral
1384 * Implemented for compatibility */
1385 long tarval_classify(tarval *tv)
1388 if (!tv || tv == tarval_bad) return 2;
1390 if (tv == get_mode_null(tv->mode)) return 0;
1391 else if (tv == get_mode_one(tv->mode)) return 1;
1392 else if ((get_mode_sort(tv->mode) == irms_int_number)
1393 && (tv == new_tarval_from_long(-1, tv->mode))) return -1;
1399 * default mode_info for output as HEX
1401 static const tarval_mode_info hex_output = {
1408 * default mode_info for output as reference
1410 static const tarval_mode_info reference_output = {
1418 * Initialization of the tarval module: called before init_mode()
1420 void init_tarval_1(void)
1423 /* initialize the sets holding the tarvals with a comparison function and
1424 * an initial size, which is the expected number of constants */
1425 tarvals = new_set(memcmp, TUNE_NCONSTANTS);
1426 values = new_set(memcmp, TUNE_NCONSTANTS);
1427 /* init strcalc with precision of 68 to support floating point values with 64
1428 * bit mantissa (needs extra bits for rounding and overflow) */
1432 tarval_bad = (tarval*)malloc(sizeof(tarval));
1433 tarval_undefined = (tarval*)malloc(sizeof(tarval));
1434 tarval_b_true = (tarval*)malloc(sizeof(tarval));
1435 tarval_b_false = (tarval*)malloc(sizeof(tarval));
1436 tarval_P_void = (tarval*)malloc(sizeof(tarval));
1440 * Initialization of the tarval module: called after init_mode()
1442 void init_tarval_2(void)
1446 tarval_bad->mode = mode_BAD;
1447 tarval_undefined->mode = mode_ANY;
1448 tarval_b_true->mode = mode_b;
1449 tarval_b_false->mode = mode_b;
1450 tarval_P_void->mode = mode_P;
1453 * assign output modes that are compatible with the
1454 * old implementation: Hex output
1456 tarval_set_mode_output_option(mode_U, &hex_output);
1457 tarval_set_mode_output_option(mode_C, &hex_output);
1458 tarval_set_mode_output_option(mode_Bs, &hex_output);
1459 tarval_set_mode_output_option(mode_Bu, &hex_output);
1460 tarval_set_mode_output_option(mode_Hs, &hex_output);
1461 tarval_set_mode_output_option(mode_Hu, &hex_output);
1462 tarval_set_mode_output_option(mode_Is, &hex_output);
1463 tarval_set_mode_output_option(mode_Iu, &hex_output);
1464 tarval_set_mode_output_option(mode_Ls, &hex_output);
1465 tarval_set_mode_output_option(mode_Lu, &hex_output);
1466 tarval_set_mode_output_option(mode_P, &reference_output);
1469 /****************************************************************************
1471 ****************************************************************************/