3 * File name: ir/tv/tv.c
4 * Purpose: Representation of and static computations on target machine
10 * Copyright: (c) 2003 Universität Karlsruhe
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * Values are stored in a format depending upon chosen arithmetic
16 * module. Default uses strcalc and fltcalc.
20 /* This implementation assumes:
21 * - target has IEEE-754 floating-point arithmetic. */
29 #include <assert.h> /* assertions */
30 #include <stdlib.h> /* atoi() */
31 #include <string.h> /* nice things for strings */
33 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
44 #include "set.h" /* to store tarvals in */
45 //#include "tune.h" /* some constants */
46 #include "entity_t.h" /* needed to store pointers to entities */
47 #include "irmode.h" /* defines modes etc */
49 #include "irnode.h" /* defines boolean return values (pnc_number)*/
54 /** Size of hash tables. Should correspond to average number of distinct constant
56 #define N_CONSTANTS 2048
58 /* XXX hack until theres's a proper interface */
62 #define GET_OVERFLOW_MODE() BAD
64 /* unused, float to int doesn't work yet */
67 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
69 #define SWITCH_NOINFINITY 0
70 #define SWITCH_NODENORMALS 0
72 /****************************************************************************
73 * local definitions and macros
74 ****************************************************************************/
76 # define TARVAL_VERIFY(a) tarval_verify((a))
78 # define TARVAL_VERIFY(a) ((void)0)
81 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
82 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
84 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
85 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
87 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
89 static long long count = 0;
90 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
92 # define ANNOUNCE() ((void)0)
94 /****************************************************************************
96 ****************************************************************************/
97 static struct set *tarvals; /* container for tarval structs */
98 static struct set *values; /* container for values */
100 /****************************************************************************
102 ****************************************************************************/
104 static int hash_val(const void *value, unsigned int length);
105 static int hash_tv(tarval *tv);
106 static void _fail_verify(tarval *tv, const char* file, int line)
108 /* print a memory image of the tarval and throw an assertion */
110 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
112 printf("%s:%d: Invalid tarval (null)", file, line);
116 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
119 INLINE static void tarval_verify(tarval *tv)
125 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
126 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
128 if (!FIND_TARVAL(tv)) fail_verify(tv);
129 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
135 static int hash_tv(tarval *tv)
137 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
140 static int hash_val(const void *value, unsigned int length)
143 unsigned int hash = 0;
145 /* scramble the byte - array */
146 for (i = 0; i < length; i++)
148 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
149 hash += (hash << 11) ^ (hash >> 17);
155 /* finds tarval with value/mode or creates new tarval */
156 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
163 /* if there already is such a value, it is returned, else value
164 * is copied into the set */
165 tv.value = INSERT_VALUE(value, length);
169 /* if there is such a tarval, it is returned, else tv is copied
171 return (tarval *)INSERT_TARVAL(&tv);
174 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
176 switch (get_mode_sort(mode))
178 case irms_int_number:
179 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
180 switch (GET_OVERFLOW_MODE()) {
182 return get_mode_max(mode);
185 char *temp = alloca(sc_get_buffer_length());
186 char *diff = alloca(sc_get_buffer_length());
187 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
188 sc_val_from_ulong(1, temp);
189 sc_add(diff, temp, diff);
190 sc_sub(value, diff, temp);
191 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
192 sc_sub(temp, diff, temp);
193 return get_tarval(temp, length, mode);
198 return get_tarval(value, length, mode);
201 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
202 switch (GET_OVERFLOW_MODE()) {
204 return get_mode_min(mode);
207 char *temp = alloca(sc_get_buffer_length());
208 char *diff = alloca(sc_get_buffer_length());
209 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
210 sc_val_from_ulong(1, temp);
211 sc_add(diff, temp, diff);
212 sc_add(value, diff, temp);
213 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
214 sc_add(temp, diff, temp);
215 return get_tarval(temp, length, mode);
220 return get_tarval(value, length, mode);
225 case irms_float_number:
226 if (SWITCH_NOINFINITY && fc_is_inf(value))
228 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
231 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
233 return get_mode_null(mode);
239 return get_tarval(value, length, mode);
244 * public variables declared in tv.h
246 static tarval reserved_tv[5];
248 tarval *tarval_bad = &reserved_tv[0];
249 tarval *tarval_undefined = &reserved_tv[1];
250 tarval *tarval_b_false = &reserved_tv[2];
251 tarval *tarval_b_true = &reserved_tv[3];
252 tarval *tarval_P_void = &reserved_tv[4];
255 * public functions declared in tv.h
259 * Constructors =============================================================
261 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
268 switch (get_mode_sort(mode))
270 case irms_control_flow:
276 case irms_internal_boolean:
277 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
278 if (strcasecmp(str, "true")) return tarval_b_true;
279 else if (strcasecmp(str, "false")) return tarval_b_true;
281 /* XXX This is C semantics */
282 return atoi(str) ? tarval_b_true : tarval_b_false;
284 case irms_float_number:
285 switch(get_mode_size_bits(mode)) {
287 fc_val_from_str(str, len, 8, 23, NULL);
290 fc_val_from_str(str, len, 11, 52, NULL);
293 fc_val_from_str(str, len, 15, 64, NULL);
296 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
298 case irms_int_number:
300 sc_val_from_str(str, len, NULL);
301 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
304 return get_tarval(str, len, mode);
307 assert(0); /* can't be reached, can it? */
312 * helper function, create a tarval from long
314 tarval *new_tarval_from_long(long l, ir_mode *mode)
317 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
319 switch(get_mode_sort(mode))
321 case irms_internal_boolean:
322 /* XXX C semantics ! */
323 return l ? tarval_b_true : tarval_b_false ;
325 case irms_int_number:
327 sc_val_from_long(l, NULL);
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 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
342 /* returns non-zero if can be converted to long */
343 int tarval_is_long(tarval *tv)
346 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
348 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
350 /* the value might be too big to fit in a long */
351 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
352 if (sc_comp(sc_get_buffer(), tv->value) == -1)
354 /* really doesn't fit */
361 /* this might overflow the machine's long, so use only with small values */
362 long tarval_to_long(tarval* tv)
365 assert(tarval_is_long(tv) && "tarval too big to fit in long");
367 return sc_val_to_long(tv->value);
370 tarval *new_tarval_from_double(long double d, ir_mode *mode)
373 assert(mode && (get_mode_sort(mode) == irms_float_number));
375 switch (get_mode_size_bits(mode)) {
377 fc_val_from_float(d, 8, 23, NULL);
380 fc_val_from_float(d, 11, 52, NULL);
383 fc_val_from_float(d, 15, 64, NULL);
386 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
389 /* returns non-zero if can be converted to double */
390 int tarval_is_double(tarval *tv)
395 return (get_mode_sort(tv->mode) == irms_float_number);
398 long double tarval_to_double(tarval *tv)
401 assert(tarval_is_double(tv));
403 return fc_val_to_float(tv->value);
406 /* The tarval represents the address of the entity. As the address must
407 be constant the entity must have as owner the global type. */
408 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
412 assert(mode && (get_mode_sort(mode) == irms_reference));
414 return get_tarval((void *)ent, 0, mode);
416 int tarval_is_entity(tarval *tv)
420 /* tv->value == NULL means dereferencing a null pointer */
421 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
422 && (tv != tarval_P_void));
425 entity *tarval_to_entity(tarval *tv)
430 if (tarval_is_entity(tv))
431 return (entity *)tv->value;
433 assert(0 && "tarval did not represent an entity");
438 void free_tarval_entity(entity *ent) {
439 /* There can be a tarval referencing this entity. Even if the
440 tarval is not used by the code any more, it can still reference
441 the entity as tarvals live indepently of the entity referenced.
442 Further the tarval is hashed into a set. If a hash function
443 evaluation happens to collide with this tarval, we will vrfy that
444 it contains a proper entity and we will crash if the entity is
447 Unluckily, tarvals can neither be changed nor deleted, and to find
448 one, all existing reference modes have to be tried -> a facility
449 to retrieve all modes of a kind is needed. */
454 * Access routines for tarval fields ========================================
456 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
464 * Special value query functions ============================================
466 * These functions calculate and return a tarval representing the requested
468 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
469 * functions, but these are stored on initialization of the irmode module and
470 * therefore the irmode functions should be prefered to the functions below.
473 tarval *get_tarval_bad(void)
478 tarval *get_tarval_undefined(void)
481 return tarval_undefined;
483 tarval *get_tarval_b_false(void)
486 return tarval_b_false;
488 tarval *get_tarval_b_true(void)
491 return tarval_b_true;
493 tarval *get_tarval_P_void(void)
496 return tarval_P_void;
499 tarval *get_tarval_max(ir_mode *mode)
504 switch(get_mode_sort(mode))
507 case irms_control_flow:
513 case irms_internal_boolean:
514 return tarval_b_true;
516 case irms_float_number:
517 switch(get_mode_size_bits(mode))
520 fc_get_max(8, 23, NULL);
523 fc_get_max(11, 52, NULL);
526 fc_get_max(15, 64, NULL);
529 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
531 case irms_int_number:
533 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
534 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
539 tarval *get_tarval_min(ir_mode *mode)
544 switch(get_mode_sort(mode))
547 case irms_control_flow:
553 case irms_internal_boolean:
554 return tarval_b_false;
556 case irms_float_number:
557 switch(get_mode_size_bits(mode))
560 fc_get_min(8, 23, NULL);
563 fc_get_min(11, 52, NULL);
566 fc_get_min(15, 64, NULL);
569 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
571 case irms_int_number:
573 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
574 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
579 tarval *get_tarval_null(ir_mode *mode)
584 switch(get_mode_sort(mode))
586 case irms_control_flow:
589 case irms_internal_boolean:
593 case irms_float_number:
594 return new_tarval_from_double(0.0, mode);
596 case irms_int_number:
598 return new_tarval_from_long(0l, mode);
601 return tarval_P_void;
606 tarval *get_tarval_one(ir_mode *mode)
611 switch(get_mode_sort(mode))
613 case irms_control_flow:
616 case irms_internal_boolean:
621 case irms_float_number:
622 return new_tarval_from_double(1.0, mode);
624 case irms_int_number:
626 return new_tarval_from_long(1l, mode);
632 tarval *get_tarval_nan(ir_mode *mode)
637 if (get_mode_sort(mode) == irms_float_number) {
638 switch(get_mode_size_bits(mode))
641 fc_get_qnan(8, 23, NULL);
644 fc_get_qnan(11, 52, NULL);
647 fc_get_qnan(15, 64, NULL);
650 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
653 assert(0 && "tarval is not floating point");
658 tarval *get_tarval_inf(ir_mode *mode)
663 if (get_mode_sort(mode) == irms_float_number) {
664 switch(get_mode_size_bits(mode))
667 fc_get_plusinf(8, 23, NULL);
670 fc_get_plusinf(11, 52, NULL);
673 fc_get_plusinf(15, 64, NULL);
676 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
679 assert(0 && "tarval is not floating point");
685 * Arithmethic operations on tarvals ========================================
689 * test if negative number, 1 means 'yes'
691 int tarval_is_negative(tarval *a)
696 switch (get_mode_sort(a->mode))
698 case irms_int_number:
699 if (!mode_is_signed(a->mode)) return 0;
701 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
703 case irms_float_number:
704 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
707 assert(0 && "not implemented");
713 * test if null, 1 means 'yes'
715 int tarval_is_null(tarval *a)
717 ir_mode *m = get_tarval_mode(a);
719 return a == get_tarval_null(m);
725 pnc_number tarval_cmp(tarval *a, tarval *b)
731 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
732 if (a == tarval_undefined || b == tarval_undefined) return False;
733 if (a == b) return Eq;
734 if (get_tarval_mode(a) != get_tarval_mode(b)) return False;
736 /* Here the two tarvals are unequal and of the same mode */
737 switch (get_mode_sort(a->mode))
739 case irms_control_flow:
745 case irms_float_number:
746 switch (fc_comp(a->value, b->value)) {
748 case 0: assert(0 && "different tarvals compare equal"); return Eq;
751 default: return False;
753 case irms_int_number:
755 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
757 case irms_internal_boolean:
758 return (a == tarval_b_true)?(Gt):(Lt);
764 * convert to other mode
766 tarval *tarval_convert_to(tarval *src, ir_mode *m)
774 if (src->mode == m) return src;
776 switch (get_mode_sort(src->mode))
778 case irms_control_flow:
783 /* cast float to something */
784 case irms_float_number:
785 switch (get_mode_sort(m)) {
786 case irms_float_number:
787 switch (get_mode_size_bits(m))
790 fc_cast(src->value, 8, 23, NULL);
793 fc_cast(src->value, 11, 52, NULL);
796 fc_cast(src->value, 15, 64, NULL);
801 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
804 case irms_int_number:
805 switch (GET_FLOAT_TO_INT_MODE())
808 fc_int(src->value, NULL);
811 fc_rnd(src->value, NULL);
816 /* XXX floating point unit can't produce a value in integer
818 * an intermediate representation is needed here first. */
819 /* return get_tarval(); */
824 /* the rest can't be converted */
829 /* cast int to something */
830 case irms_int_number:
831 switch (get_mode_sort(m)) {
832 case irms_int_number:
834 return get_tarval_overflow(src->value, src->length, m);
836 case irms_internal_boolean:
837 /* XXX C semantics */
838 if (src == get_mode_null(src->mode)) return tarval_b_false;
839 else return tarval_b_true;
841 case irms_float_number:
842 /* XXX floating point unit does not understand internal integer
843 * representation, convert to string first, then create float from
845 buffer = alloca(100);
846 /* decimal string representation because hexadecimal output is
847 * interpreted unsigned by fc_val_from_str, so this is a HACK */
848 snprintf(buffer, 100, "%s",
849 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
850 switch (get_mode_size_bits(m))
853 fc_val_from_str(buffer, 0, 8, 23, NULL);
856 fc_val_from_str(buffer, 0, 11, 52, NULL);
859 fc_val_from_str(buffer, 0, 15, 64, NULL);
862 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
869 case irms_internal_boolean:
870 switch (get_mode_sort(m))
872 case irms_int_number:
873 if (src == tarval_b_true) return get_mode_one(m);
874 else return get_mode_null(m);
893 tarval *tarval_not(tarval *a)
899 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
901 switch (get_mode_sort(a->mode))
903 case irms_int_number:
904 buffer = alloca(sc_get_buffer_length());
905 sc_not(a->value, buffer);
906 return get_tarval(buffer, a->length, a->mode);
914 * arithmetic negation
916 tarval *tarval_neg(tarval *a)
922 assert(mode_is_num(a->mode)); /* negation only for numerical values */
923 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
925 switch (get_mode_sort(a->mode))
927 case irms_int_number:
928 buffer = alloca(sc_get_buffer_length());
929 sc_neg(a->value, buffer);
930 return get_tarval_overflow(buffer, a->length, a->mode);
932 case irms_float_number:
933 fc_neg(a->value, NULL);
934 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
944 tarval *tarval_add(tarval *a, tarval *b)
951 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
953 switch (get_mode_sort(a->mode))
956 case irms_int_number:
957 /* modes of a,b are equal, so result has mode of a as this might be the character */
958 buffer = alloca(sc_get_buffer_length());
959 sc_add(a->value, b->value, buffer);
960 return get_tarval_overflow(buffer, a->length, a->mode);
962 case irms_float_number:
963 fc_add(a->value, b->value, NULL);
964 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
974 tarval *tarval_sub(tarval *a, tarval *b)
981 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
983 switch (get_mode_sort(a->mode))
986 case irms_int_number:
987 /* modes of a,b are equal, so result has mode of a as this might be the character */
988 buffer = alloca(sc_get_buffer_length());
989 sc_sub(a->value, b->value, buffer);
990 return get_tarval_overflow(buffer, a->length, a->mode);
992 case irms_float_number:
993 fc_sub(a->value, b->value, NULL);
994 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1004 tarval *tarval_mul(tarval *a, tarval *b)
1011 assert((a->mode == b->mode) && mode_is_num(a->mode));
1013 switch (get_mode_sort(a->mode))
1015 case irms_int_number:
1016 /* modes of a,b are equal */
1017 buffer = alloca(sc_get_buffer_length());
1018 sc_mul(a->value, b->value, buffer);
1019 return get_tarval_overflow(buffer, a->length, a->mode);
1021 case irms_float_number:
1022 fc_mul(a->value, b->value, NULL);
1023 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1031 * floating point division
1033 tarval *tarval_quo(tarval *a, tarval *b)
1038 assert((a->mode == b->mode) && mode_is_float(a->mode));
1040 fc_div(a->value, b->value, NULL);
1041 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1046 * overflow is impossible, but look out for division by zero
1048 tarval *tarval_div(tarval *a, tarval *b)
1053 assert((a->mode == b->mode) && mode_is_int(a->mode));
1056 if (b == get_mode_null(b->mode)) return tarval_bad;
1057 /* modes of a,b are equal */
1058 sc_div(a->value, b->value, NULL);
1059 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1064 * overflow is impossible, but look out for division by zero
1066 tarval *tarval_mod(tarval *a, tarval *b)
1071 assert((a->mode == b->mode) && mode_is_int(a->mode));
1074 if (b == get_mode_null(b->mode)) return tarval_bad;
1075 /* modes of a,b are equal */
1076 sc_mod(a->value, b->value, NULL);
1077 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1083 tarval *tarval_abs(tarval *a)
1089 assert(mode_is_num(a->mode));
1091 switch (get_mode_sort(a->mode))
1093 case irms_int_number:
1094 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1096 buffer = alloca(sc_get_buffer_length());
1097 sc_neg(a->value, buffer);
1098 return get_tarval_overflow(buffer, a->length, a->mode);
1102 case irms_float_number:
1103 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1105 fc_neg(a->value, NULL);
1106 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1119 tarval *tarval_and(tarval *a, tarval *b)
1124 assert(a->mode == b->mode);
1126 switch(get_mode_sort(a->mode))
1128 case irms_internal_boolean:
1129 return (a == tarval_b_false) ? a : b;
1131 case irms_int_number:
1132 sc_and(a->value, b->value, NULL);
1133 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1136 assert(0 && "operation not defined on mode");
1144 tarval *tarval_or (tarval *a, tarval *b)
1149 assert(a->mode == b->mode);
1151 switch (get_mode_sort(a->mode))
1153 case irms_internal_boolean:
1154 return (a == tarval_b_true) ? a : b;
1156 case irms_int_number:
1157 sc_or(a->value, b->value, NULL);
1158 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1161 assert(0 && "operation not defined on mode");
1167 * bitwise exclusive or (xor)
1169 tarval *tarval_eor(tarval *a, tarval *b)
1174 assert((a->mode == b->mode));
1176 switch (get_mode_sort(a->mode))
1178 case irms_internal_boolean:
1179 return (a == b)? tarval_b_false : tarval_b_true;
1181 case irms_int_number:
1182 sc_xor(a->value, b->value, NULL);
1183 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1186 assert(0 && "operation not defined on mode");
1192 * bitwise left shift
1194 tarval *tarval_shl(tarval *a, tarval *b)
1196 char *temp_val = NULL;
1200 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1202 if (get_mode_modulo_shift(a->mode) != 0)
1204 temp_val = alloca(sc_get_buffer_length());
1206 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1207 sc_mod(b->value, temp_val, temp_val);
1210 temp_val = (char*)b->value;
1212 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1213 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1217 * bitwise unsigned right shift
1219 tarval *tarval_shr(tarval *a, tarval *b)
1221 char *temp_val = NULL;
1225 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1227 if (get_mode_modulo_shift(a->mode) != 0)
1229 temp_val = alloca(sc_get_buffer_length());
1231 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1232 sc_mod(b->value, temp_val, temp_val);
1235 temp_val = (char*)b->value;
1237 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1238 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1242 * bitwise signed right shift
1244 tarval *tarval_shrs(tarval *a, tarval *b)
1246 char *temp_val = NULL;
1250 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1252 if (get_mode_modulo_shift(a->mode) != 0)
1254 temp_val = alloca(sc_get_buffer_length());
1256 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1257 sc_mod(b->value, temp_val, temp_val);
1260 temp_val = (char*)b->value;
1262 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1263 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1269 tarval *tarval_rot(tarval *a, tarval *b)
1271 char *temp_val = NULL;
1275 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1277 if (get_mode_modulo_shift(a->mode) != 0)
1279 temp_val = alloca(sc_get_buffer_length());
1281 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1282 sc_mod(b->value, temp_val, temp_val);
1285 temp_val = (char*)b->value;
1287 sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1288 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1295 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1297 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1301 const tarval_mode_info *mode_info;
1302 const char *prefix, *suffix;
1306 mode_info = tv->mode->tv_priv;
1308 mode_info = &default_info;
1309 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1310 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1312 switch (get_mode_sort(tv->mode))
1314 case irms_int_number:
1315 case irms_character:
1316 switch (mode_info->mode_output) {
1319 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1323 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1329 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1332 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1334 case irms_float_number:
1335 switch (mode_info->mode_output) {
1337 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1340 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1345 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1349 case irms_reference:
1350 if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1351 if (tv->value != NULL)
1352 if (tarval_is_entity(tv)) {
1353 if (get_entity_peculiarity((entity *)tv->value) == peculiarity_existent)
1354 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1356 if (mode_info->mode_output == TVO_NATIVE)
1357 return snprintf(buf, len, "NULL");
1359 return snprintf(buf, len, "0");
1363 if (size > tv->length) {
1364 memcpy(buf, tv->value, tv->length);
1365 buf[tv->length] = '\0';
1369 memcpy(buf, tv->value, size-1);
1375 return snprintf(buf, len, "void");
1377 case irms_internal_boolean:
1378 switch (mode_info->mode_output) {
1384 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1388 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1391 case irms_control_flow:
1393 case irms_auxiliary:
1394 return snprintf(buf, len, "<BAD>");
1402 * Output of tarvals to stdio.
1404 int tarval_printf(tarval *tv) {
1408 res = tarval_snprintf(buf, sizeof(buf), tv);
1409 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1415 char *tarval_bitpattern(tarval *tv)
1421 * access to the bitpattern
1423 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1425 switch (get_mode_sort(tv->mode)) {
1426 case irms_int_number:
1427 case irms_character:
1428 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1430 case irms_float_number:
1431 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1439 * Specify the output options of one mode.
1441 * This functions stores the modinfo, so DO NOT DESTROY it.
1443 * Returns zero on success.
1445 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1449 mode->tv_priv = modeinfo;
1454 * Returns the output options of one mode.
1456 * This functions returns the modinfo of a given mode.
1458 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1462 return mode->tv_priv;
1466 /* Identifying some tarvals ??? */
1467 /* Implemented in old tv.c as such:
1468 * return 0 for additive neutral,
1469 * 1 for multiplicative neutral,
1470 * -1 for bitwise-and neutral
1473 * Implemented for compatibility */
1474 long tarval_classify(tarval *tv)
1477 if (!tv || tv == tarval_bad) return 2;
1479 if (tv == get_mode_null(tv->mode)) return 0;
1480 else if (tv == get_mode_one(tv->mode)) return 1;
1481 else if ((get_mode_sort(tv->mode) == irms_int_number)
1482 && (tv == new_tarval_from_long(-1, tv->mode))) return -1;
1488 * default mode_info for output as HEX
1490 static const tarval_mode_info hex_output = {
1497 * default mode_info for output as reference
1499 static const tarval_mode_info reference_output = {
1507 * Initialization of the tarval module: called before init_mode()
1509 void init_tarval_1(void)
1512 /* initialize the sets holding the tarvals with a comparison function and
1513 * an initial size, which is the expected number of constants */
1514 tarvals = new_set(memcmp, N_CONSTANTS);
1515 values = new_set(memcmp, N_CONSTANTS);
1516 /* init strcalc with precision of 68 to support floating point values with 64
1517 * bit mantissa (needs extra bits for rounding and overflow) */
1523 * Initialization of the tarval module: called after init_mode()
1525 void init_tarval_2(void)
1529 tarval_bad->mode = mode_BAD;
1530 tarval_undefined->mode = mode_ANY;
1531 tarval_b_true->mode = mode_b;
1532 tarval_b_false->mode = mode_b;
1533 tarval_P_void->mode = mode_P;
1536 * assign output modes that are compatible with the
1537 * old implementation: Hex output
1539 tarval_set_mode_output_option(mode_U, &hex_output);
1540 tarval_set_mode_output_option(mode_C, &hex_output);
1541 tarval_set_mode_output_option(mode_Bs, &hex_output);
1542 tarval_set_mode_output_option(mode_Bu, &hex_output);
1543 tarval_set_mode_output_option(mode_Hs, &hex_output);
1544 tarval_set_mode_output_option(mode_Hu, &hex_output);
1545 tarval_set_mode_output_option(mode_Is, &hex_output);
1546 tarval_set_mode_output_option(mode_Iu, &hex_output);
1547 tarval_set_mode_output_option(mode_Ls, &hex_output);
1548 tarval_set_mode_output_option(mode_Lu, &hex_output);
1549 tarval_set_mode_output_option(mode_P, &reference_output);
1552 /****************************************************************************
1554 ****************************************************************************/