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 */
32 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
39 #include "set.h" /* to store tarvals in */
40 //#include "tune.h" /* some constants */
41 #include "entity_t.h" /* needed to store pointers to entities */
42 #include "irmode.h" /* defines modes etc */
44 #include "irnode.h" /* defines boolean return values (pnc_number)*/
49 /** Size of hash tables. Should correspond to average number of distinct constant
51 #define N_CONSTANTS 2048
53 /* XXX hack until theres's a proper interface */
57 #define GET_OVERFLOW_MODE() BAD
59 /* unused, float to int doesn't work yet */
62 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
64 #define SWITCH_NOINFINITY 0
65 #define SWITCH_NODENORMALS 0
67 /****************************************************************************
68 * local definitions and macros
69 ****************************************************************************/
71 # define TARVAL_VERIFY(a) tarval_verify((a))
73 # define TARVAL_VERIFY(a) ((void)0)
76 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
77 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
79 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
80 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
82 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
84 static long long count = 0;
85 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
87 # define ANNOUNCE() ((void)0)
89 /****************************************************************************
91 ****************************************************************************/
92 static struct set *tarvals; /* container for tarval structs */
93 static struct set *values; /* container for values */
95 /****************************************************************************
97 ****************************************************************************/
99 static int hash_val(const void *value, unsigned int length);
100 static int hash_tv(tarval *tv);
101 static void _fail_verify(tarval *tv, const char* file, int line)
103 /* print a memory image of the tarval and throw an assertion */
105 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
107 printf("%s:%d: Invalid tarval (null)", file, line);
111 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
114 INLINE static void tarval_verify(tarval *tv)
120 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
121 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
123 if (!FIND_TARVAL(tv)) fail_verify(tv);
124 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
130 static int hash_tv(tarval *tv)
132 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
135 static int hash_val(const void *value, unsigned int length)
138 unsigned int hash = 0;
140 /* scramble the byte - array */
141 for (i = 0; i < length; i++)
143 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
144 hash += (hash << 11) ^ (hash >> 17);
150 /* finds tarval with value/mode or creates new tarval */
151 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
158 /* if there already is such a value, it is returned, else value
159 * is copied into the set */
160 tv.value = INSERT_VALUE(value, length);
164 /* if there is such a tarval, it is returned, else tv is copied
166 return (tarval *)INSERT_TARVAL(&tv);
169 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
171 switch (get_mode_sort(mode))
173 case irms_int_number:
174 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
175 switch (GET_OVERFLOW_MODE()) {
177 return get_mode_max(mode);
180 char *temp = alloca(sc_get_buffer_length());
181 char *diff = alloca(sc_get_buffer_length());
182 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
183 sc_val_from_ulong(1, temp);
184 sc_add(diff, temp, diff);
185 sc_sub(value, diff, temp);
186 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
187 sc_sub(temp, diff, temp);
188 return get_tarval(temp, length, mode);
193 return get_tarval(value, length, mode);
196 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
197 switch (GET_OVERFLOW_MODE()) {
199 return get_mode_min(mode);
202 char *temp = alloca(sc_get_buffer_length());
203 char *diff = alloca(sc_get_buffer_length());
204 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
205 sc_val_from_ulong(1, temp);
206 sc_add(diff, temp, diff);
207 sc_add(value, diff, temp);
208 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
209 sc_add(temp, diff, temp);
210 return get_tarval(temp, length, mode);
215 return get_tarval(value, length, mode);
220 case irms_float_number:
221 if (SWITCH_NOINFINITY && fc_is_inf(value))
223 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
226 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
228 return get_mode_null(mode);
234 return get_tarval(value, length, mode);
239 * public variables declared in tv.h
241 static tarval reserved_tv[5];
243 tarval *tarval_bad = &reserved_tv[0];
244 tarval *tarval_undefined = &reserved_tv[1];
245 tarval *tarval_b_false = &reserved_tv[2];
246 tarval *tarval_b_true = &reserved_tv[3];
247 tarval *tarval_P_void = &reserved_tv[4];
250 * public functions declared in tv.h
254 * Constructors =============================================================
256 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
263 switch (get_mode_sort(mode))
265 case irms_control_flow:
271 case irms_internal_boolean:
272 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
273 if (strcasecmp(str, "true")) return tarval_b_true;
274 else if (strcasecmp(str, "false")) return tarval_b_true;
276 /* XXX This is C semantics */
277 return atoi(str) ? tarval_b_true : tarval_b_false;
279 case irms_float_number:
280 switch(get_mode_size_bits(mode)) {
282 fc_val_from_str(str, len, 8, 23, NULL);
285 fc_val_from_str(str, len, 11, 52, NULL);
288 fc_val_from_str(str, len, 15, 64, NULL);
291 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
293 case irms_int_number:
295 sc_val_from_str(str, len, NULL);
296 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
299 return get_tarval(str, len, mode);
302 assert(0); /* can't be reached, can it? */
307 * helper function, create a tarval from long
309 tarval *new_tarval_from_long(long l, ir_mode *mode)
312 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
314 switch(get_mode_sort(mode))
316 case irms_internal_boolean:
317 /* XXX C semantics ! */
318 return l ? tarval_b_true : tarval_b_false ;
320 case irms_int_number:
322 sc_val_from_long(l, NULL);
323 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
325 case irms_float_number:
326 return new_tarval_from_double((long double)l, mode);
329 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
337 /* returns non-zero if can be converted to long */
338 int tarval_is_long(tarval *tv)
341 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
343 if (get_mode_size_bits(tv->mode) > 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)
349 /* really doesn't fit */
356 /* this might overflow the machine's long, so use only with small values */
357 long tarval_to_long(tarval* tv)
360 assert(tarval_is_long(tv) && "tarval too big to fit in long");
362 return sc_val_to_long(tv->value);
365 tarval *new_tarval_from_double(long double d, ir_mode *mode)
368 assert(mode && (get_mode_sort(mode) == irms_float_number));
370 switch (get_mode_size_bits(mode)) {
372 fc_val_from_float(d, 8, 23, NULL);
375 fc_val_from_float(d, 11, 52, NULL);
378 fc_val_from_float(d, 15, 64, NULL);
381 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
384 /* returns non-zero if can be converted to double */
385 int tarval_is_double(tarval *tv)
390 return (get_mode_sort(tv->mode) == irms_float_number);
393 long double tarval_to_double(tarval *tv)
396 assert(tarval_is_double(tv));
398 return fc_val_to_float(tv->value);
401 /* The tarval represents the address of the entity. As the address must
402 be constant the entity must have as owner the global type. */
403 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
407 assert(mode && (get_mode_sort(mode) == irms_reference));
409 return get_tarval((void *)ent, 0, mode);
411 int tarval_is_entity(tarval *tv)
415 /* tv->value == NULL means dereferencing a null pointer */
416 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
417 && (tv != tarval_P_void));
420 entity *tarval_to_entity(tarval *tv)
425 if (tarval_is_entity(tv))
426 return (entity *)tv->value;
428 assert(0 && "tarval did not represent an entity");
433 void free_tarval_entity(entity *ent) {
434 /* There can be a tarval referencing this entity. Even if the
435 tarval is not used by the code any more, it can still reference
436 the entity as tarvals live indepently of the entity referenced.
437 Further the tarval is hashed into a set. If a hash function
438 evaluation happens to collide with this tarval, we will vrfy that
439 it contains a proper entity and we will crash if the entity is
442 Unluckily, tarvals can neither be changed nor deleted, and to find
443 one, all existing reference modes have to be tried -> a facility
444 to retrieve all modes of a kind is needed. */
449 * Access routines for tarval fields ========================================
451 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
459 * Special value query functions ============================================
461 * These functions calculate and return a tarval representing the requested
463 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
464 * functions, but these are stored on initialization of the irmode module and
465 * therefore the irmode functions should be prefered to the functions below.
468 tarval *get_tarval_bad(void)
473 tarval *get_tarval_undefined(void)
476 return tarval_undefined;
478 tarval *get_tarval_b_false(void)
481 return tarval_b_false;
483 tarval *get_tarval_b_true(void)
486 return tarval_b_true;
488 tarval *get_tarval_P_void(void)
491 return tarval_P_void;
494 tarval *get_tarval_max(ir_mode *mode)
499 switch(get_mode_sort(mode))
502 case irms_control_flow:
508 case irms_internal_boolean:
509 return tarval_b_true;
511 case irms_float_number:
512 switch(get_mode_size_bits(mode))
515 fc_get_max(8, 23, NULL);
518 fc_get_max(11, 52, NULL);
521 fc_get_max(15, 64, NULL);
524 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
526 case irms_int_number:
528 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
529 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
534 tarval *get_tarval_min(ir_mode *mode)
539 switch(get_mode_sort(mode))
542 case irms_control_flow:
548 case irms_internal_boolean:
549 return tarval_b_false;
551 case irms_float_number:
552 switch(get_mode_size_bits(mode))
555 fc_get_min(8, 23, NULL);
558 fc_get_min(11, 52, NULL);
561 fc_get_min(15, 64, NULL);
564 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
566 case irms_int_number:
568 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
569 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
574 tarval *get_tarval_null(ir_mode *mode)
579 switch(get_mode_sort(mode))
581 case irms_control_flow:
584 case irms_internal_boolean:
588 case irms_float_number:
589 return new_tarval_from_double(0.0, mode);
591 case irms_int_number:
593 return new_tarval_from_long(0l, mode);
596 return tarval_P_void;
601 tarval *get_tarval_one(ir_mode *mode)
606 switch(get_mode_sort(mode))
608 case irms_control_flow:
611 case irms_internal_boolean:
616 case irms_float_number:
617 return new_tarval_from_double(1.0, mode);
619 case irms_int_number:
621 return new_tarval_from_long(1l, mode);
627 tarval *get_tarval_nan(ir_mode *mode)
632 if (get_mode_sort(mode) == irms_float_number) {
633 switch(get_mode_size_bits(mode))
636 fc_get_qnan(8, 23, NULL);
639 fc_get_qnan(11, 52, NULL);
642 fc_get_qnan(15, 64, NULL);
645 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
648 assert(0 && "tarval is not floating point");
653 tarval *get_tarval_inf(ir_mode *mode)
658 if (get_mode_sort(mode) == irms_float_number) {
659 switch(get_mode_size_bits(mode))
662 fc_get_plusinf(8, 23, NULL);
665 fc_get_plusinf(11, 52, NULL);
668 fc_get_plusinf(15, 64, NULL);
671 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
674 assert(0 && "tarval is not floating point");
680 * Arithmethic operations on tarvals ========================================
684 * test if negative number, 1 means 'yes'
686 int tarval_is_negative(tarval *a)
691 switch (get_mode_sort(a->mode))
693 case irms_int_number:
694 if (!mode_is_signed(a->mode)) return 0;
696 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
698 case irms_float_number:
699 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
702 assert(0 && "not implemented");
708 * test if null, 1 means 'yes'
710 int tarval_is_null(tarval *a)
712 ir_mode *m = get_tarval_mode(a);
714 return a == get_tarval_null(m);
720 pnc_number tarval_cmp(tarval *a, tarval *b)
726 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
727 if (a == tarval_undefined || b == tarval_undefined) return False;
728 if (a == b) return Eq;
729 if (get_tarval_mode(a) != get_tarval_mode(b)) return False;
731 /* Here the two tarvals are unequal and of the same mode */
732 switch (get_mode_sort(a->mode))
734 case irms_control_flow:
740 case irms_float_number:
741 switch (fc_comp(a->value, b->value)) {
743 case 0: assert(0 && "different tarvals compare equal"); return Eq;
746 default: return False;
748 case irms_int_number:
750 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
752 case irms_internal_boolean:
753 return (a == tarval_b_true)?(Gt):(Lt);
759 * convert to other mode
761 tarval *tarval_convert_to(tarval *src, ir_mode *m)
769 if (src->mode == m) return src;
771 switch (get_mode_sort(src->mode))
773 case irms_control_flow:
778 /* cast float to something */
779 case irms_float_number:
780 switch (get_mode_sort(m)) {
781 case irms_float_number:
782 switch (get_mode_size_bits(m))
785 fc_cast(src->value, 8, 23, NULL);
788 fc_cast(src->value, 11, 52, NULL);
791 fc_cast(src->value, 15, 64, NULL);
796 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
799 case irms_int_number:
800 switch (GET_FLOAT_TO_INT_MODE())
803 fc_int(src->value, NULL);
806 fc_rnd(src->value, NULL);
811 /* XXX floating point unit can't produce a value in integer
813 * an intermediate representation is needed here first. */
814 /* return get_tarval(); */
819 /* the rest can't be converted */
824 /* cast int to something */
825 case irms_int_number:
826 switch (get_mode_sort(m)) {
827 case irms_int_number:
829 return get_tarval_overflow(src->value, src->length, m);
831 case irms_internal_boolean:
832 /* XXX C semantics */
833 if (src == get_mode_null(src->mode)) return tarval_b_false;
834 else return tarval_b_true;
836 case irms_float_number:
837 /* XXX floating point unit does not understand internal integer
838 * representation, convert to string first, then create float from
840 buffer = alloca(100);
841 /* decimal string representation because hexadecimal output is
842 * interpreted unsigned by fc_val_from_str, so this is a HACK */
843 snprintf(buffer, 100, "%s",
844 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
845 switch (get_mode_size_bits(m))
848 fc_val_from_str(buffer, 0, 8, 23, NULL);
851 fc_val_from_str(buffer, 0, 11, 52, NULL);
854 fc_val_from_str(buffer, 0, 15, 64, NULL);
857 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
864 case irms_internal_boolean:
865 switch (get_mode_sort(m))
867 case irms_int_number:
868 if (src == tarval_b_true) return get_mode_one(m);
869 else return get_mode_null(m);
888 tarval *tarval_not(tarval *a)
894 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
896 switch (get_mode_sort(a->mode))
898 case irms_int_number:
899 buffer = alloca(sc_get_buffer_length());
900 sc_not(a->value, buffer);
901 return get_tarval(buffer, a->length, a->mode);
909 * arithmetic negation
911 tarval *tarval_neg(tarval *a)
917 assert(mode_is_num(a->mode)); /* negation only for numerical values */
918 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
920 switch (get_mode_sort(a->mode))
922 case irms_int_number:
923 buffer = alloca(sc_get_buffer_length());
924 sc_neg(a->value, buffer);
925 return get_tarval_overflow(buffer, a->length, a->mode);
927 case irms_float_number:
928 fc_neg(a->value, NULL);
929 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
939 tarval *tarval_add(tarval *a, tarval *b)
946 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
948 switch (get_mode_sort(a->mode))
951 case irms_int_number:
952 /* modes of a,b are equal, so result has mode of a as this might be the character */
953 buffer = alloca(sc_get_buffer_length());
954 sc_add(a->value, b->value, buffer);
955 return get_tarval_overflow(buffer, a->length, a->mode);
957 case irms_float_number:
958 fc_add(a->value, b->value, NULL);
959 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
969 tarval *tarval_sub(tarval *a, tarval *b)
976 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
978 switch (get_mode_sort(a->mode))
981 case irms_int_number:
982 /* modes of a,b are equal, so result has mode of a as this might be the character */
983 buffer = alloca(sc_get_buffer_length());
984 sc_sub(a->value, b->value, buffer);
985 return get_tarval_overflow(buffer, a->length, a->mode);
987 case irms_float_number:
988 fc_sub(a->value, b->value, NULL);
989 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
999 tarval *tarval_mul(tarval *a, tarval *b)
1006 assert((a->mode == b->mode) && mode_is_num(a->mode));
1008 switch (get_mode_sort(a->mode))
1010 case irms_int_number:
1011 /* modes of a,b are equal */
1012 buffer = alloca(sc_get_buffer_length());
1013 sc_mul(a->value, b->value, buffer);
1014 return get_tarval_overflow(buffer, a->length, a->mode);
1016 case irms_float_number:
1017 fc_mul(a->value, b->value, NULL);
1018 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1026 * floating point division
1028 tarval *tarval_quo(tarval *a, tarval *b)
1033 assert((a->mode == b->mode) && mode_is_float(a->mode));
1035 fc_div(a->value, b->value, NULL);
1036 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1041 * overflow is impossible, but look out for division by zero
1043 tarval *tarval_div(tarval *a, tarval *b)
1048 assert((a->mode == b->mode) && mode_is_int(a->mode));
1051 if (b == get_mode_null(b->mode)) return tarval_bad;
1052 /* modes of a,b are equal */
1053 sc_div(a->value, b->value, NULL);
1054 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1059 * overflow is impossible, but look out for division by zero
1061 tarval *tarval_mod(tarval *a, tarval *b)
1066 assert((a->mode == b->mode) && mode_is_int(a->mode));
1069 if (b == get_mode_null(b->mode)) return tarval_bad;
1070 /* modes of a,b are equal */
1071 sc_mod(a->value, b->value, NULL);
1072 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1078 tarval *tarval_abs(tarval *a)
1084 assert(mode_is_num(a->mode));
1086 switch (get_mode_sort(a->mode))
1088 case irms_int_number:
1089 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1091 buffer = alloca(sc_get_buffer_length());
1092 sc_neg(a->value, buffer);
1093 return get_tarval_overflow(buffer, a->length, a->mode);
1097 case irms_float_number:
1098 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1100 fc_neg(a->value, NULL);
1101 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1114 tarval *tarval_and(tarval *a, tarval *b)
1119 assert(a->mode == b->mode);
1121 switch(get_mode_sort(a->mode))
1123 case irms_internal_boolean:
1124 return (a == tarval_b_false) ? a : b;
1126 case irms_int_number:
1127 sc_and(a->value, b->value, NULL);
1128 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1131 assert(0 && "operation not defined on mode");
1139 tarval *tarval_or (tarval *a, tarval *b)
1144 assert(a->mode == b->mode);
1146 switch (get_mode_sort(a->mode))
1148 case irms_internal_boolean:
1149 return (a == tarval_b_true) ? a : b;
1151 case irms_int_number:
1152 sc_or(a->value, b->value, NULL);
1153 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1156 assert(0 && "operation not defined on mode");
1162 * bitwise exclusive or (xor)
1164 tarval *tarval_eor(tarval *a, tarval *b)
1169 assert((a->mode == b->mode));
1171 switch (get_mode_sort(a->mode))
1173 case irms_internal_boolean:
1174 return (a == b)? tarval_b_false : tarval_b_true;
1176 case irms_int_number:
1177 sc_xor(a->value, b->value, NULL);
1178 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1181 assert(0 && "operation not defined on mode");
1187 * bitwise left shift
1189 tarval *tarval_shl(tarval *a, tarval *b)
1191 char *temp_val = NULL;
1195 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1197 if (get_mode_modulo_shift(a->mode) != 0)
1199 temp_val = alloca(sc_get_buffer_length());
1201 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1202 sc_mod(b->value, temp_val, temp_val);
1205 temp_val = (char*)b->value;
1207 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1208 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1212 * bitwise unsigned right shift
1214 tarval *tarval_shr(tarval *a, tarval *b)
1216 char *temp_val = NULL;
1220 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1222 if (get_mode_modulo_shift(a->mode) != 0)
1224 temp_val = alloca(sc_get_buffer_length());
1226 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1227 sc_mod(b->value, temp_val, temp_val);
1230 temp_val = (char*)b->value;
1232 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1233 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1237 * bitwise signed right shift
1239 tarval *tarval_shrs(tarval *a, tarval *b)
1241 char *temp_val = NULL;
1245 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1247 if (get_mode_modulo_shift(a->mode) != 0)
1249 temp_val = alloca(sc_get_buffer_length());
1251 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1252 sc_mod(b->value, temp_val, temp_val);
1255 temp_val = (char*)b->value;
1257 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1258 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1264 tarval *tarval_rot(tarval *a, tarval *b)
1266 char *temp_val = NULL;
1270 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1272 if (get_mode_modulo_shift(a->mode) != 0)
1274 temp_val = alloca(sc_get_buffer_length());
1276 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1277 sc_mod(b->value, temp_val, temp_val);
1280 temp_val = (char*)b->value;
1282 sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1283 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1290 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1292 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1296 const tarval_mode_info *mode_info;
1297 const char *prefix, *suffix;
1301 mode_info = tv->mode->tv_priv;
1303 mode_info = &default_info;
1304 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1305 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1307 switch (get_mode_sort(tv->mode))
1309 case irms_int_number:
1310 case irms_character:
1311 switch (mode_info->mode_output) {
1314 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1318 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1324 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1327 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1329 case irms_float_number:
1330 switch (mode_info->mode_output) {
1332 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1335 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1340 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1344 case irms_reference:
1345 if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1346 if (tv->value != NULL)
1347 if (tarval_is_entity(tv)) {
1348 if (get_entity_peculiarity((entity *)tv->value) == peculiarity_existent)
1349 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1351 if (mode_info->mode_output == TVO_NATIVE)
1352 return snprintf(buf, len, "NULL");
1354 return snprintf(buf, len, "0");
1358 if (size > tv->length) {
1359 memcpy(buf, tv->value, tv->length);
1360 buf[tv->length] = '\0';
1364 memcpy(buf, tv->value, size-1);
1370 return snprintf(buf, len, "void");
1372 case irms_internal_boolean:
1373 switch (mode_info->mode_output) {
1379 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1383 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1386 case irms_control_flow:
1388 case irms_auxiliary:
1389 return snprintf(buf, len, "<BAD>");
1397 * Output of tarvals to stdio.
1399 int tarval_printf(tarval *tv) {
1403 res = tarval_snprintf(buf, sizeof(buf), tv);
1404 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1410 char *tarval_bitpattern(tarval *tv)
1416 * access to the bitpattern
1418 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1420 switch (get_mode_sort(tv->mode)) {
1421 case irms_int_number:
1422 case irms_character:
1423 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1425 case irms_float_number:
1426 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1434 * Specify the output options of one mode.
1436 * This functions stores the modinfo, so DO NOT DESTROY it.
1438 * Returns zero on success.
1440 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1444 mode->tv_priv = modeinfo;
1449 * Returns the output options of one mode.
1451 * This functions returns the modinfo of a given mode.
1453 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1457 return mode->tv_priv;
1461 /* Identifying some tarvals ??? */
1462 /* Implemented in old tv.c as such:
1463 * return 0 for additive neutral,
1464 * 1 for multiplicative neutral,
1465 * -1 for bitwise-and neutral
1468 * Implemented for compatibility */
1469 long tarval_classify(tarval *tv)
1472 if (!tv || tv == tarval_bad) return 2;
1474 if (tv == get_mode_null(tv->mode)) return 0;
1475 else if (tv == get_mode_one(tv->mode)) return 1;
1476 else if ((get_mode_sort(tv->mode) == irms_int_number)
1477 && (tv == new_tarval_from_long(-1, tv->mode))) return -1;
1483 * default mode_info for output as HEX
1485 static const tarval_mode_info hex_output = {
1492 * default mode_info for output as reference
1494 static const tarval_mode_info reference_output = {
1502 * Initialization of the tarval module: called before init_mode()
1504 void init_tarval_1(void)
1507 /* initialize the sets holding the tarvals with a comparison function and
1508 * an initial size, which is the expected number of constants */
1509 tarvals = new_set(memcmp, N_CONSTANTS);
1510 values = new_set(memcmp, N_CONSTANTS);
1511 /* init strcalc with precision of 68 to support floating point values with 64
1512 * bit mantissa (needs extra bits for rounding and overflow) */
1518 * Initialization of the tarval module: called after init_mode()
1520 void init_tarval_2(void)
1524 tarval_bad->mode = mode_BAD;
1525 tarval_undefined->mode = mode_ANY;
1526 tarval_b_true->mode = mode_b;
1527 tarval_b_false->mode = mode_b;
1528 tarval_P_void->mode = mode_P;
1531 * assign output modes that are compatible with the
1532 * old implementation: Hex output
1534 tarval_set_mode_output_option(mode_U, &hex_output);
1535 tarval_set_mode_output_option(mode_C, &hex_output);
1536 tarval_set_mode_output_option(mode_Bs, &hex_output);
1537 tarval_set_mode_output_option(mode_Bu, &hex_output);
1538 tarval_set_mode_output_option(mode_Hs, &hex_output);
1539 tarval_set_mode_output_option(mode_Hu, &hex_output);
1540 tarval_set_mode_output_option(mode_Is, &hex_output);
1541 tarval_set_mode_output_option(mode_Iu, &hex_output);
1542 tarval_set_mode_output_option(mode_Ls, &hex_output);
1543 tarval_set_mode_output_option(mode_Lu, &hex_output);
1544 tarval_set_mode_output_option(mode_P, &reference_output);
1547 /****************************************************************************
1549 ****************************************************************************/