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. */
23 #include <assert.h> /* assertions */
24 #include <stdlib.h> /* atoi() */
25 #include <string.h> /* nice things for strings */
26 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
31 #include "set.h" /* to store tarvals in */
32 //#include "tune.h" /* some constants */
33 #include "entity_t.h" /* needed to store pointers to entities */
34 #include "irmode.h" /* defines modes etc */
36 #include "irnode.h" /* defines boolean return values (pnc_number)*/
41 /** Size of hash tables. Should correspond to average number of distinct constant
43 #define N_CONSTANTS 2048
45 /* XXX hack until theres's a proper interface */
49 #define GET_OVERFLOW_MODE() BAD
51 /* unused, float to int doesn't work yet */
54 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
56 #define SWITCH_NOINFINITY 0
57 #define SWITCH_NODENORMALS 0
59 /****************************************************************************
60 * local definitions and macros
61 ****************************************************************************/
63 # define TARVAL_VERIFY(a) tarval_verify((a))
65 # define TARVAL_VERIFY(a) ((void)0)
68 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
69 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
71 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
72 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
74 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
76 static long long count = 0;
77 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
79 # define ANNOUNCE() ((void)0)
81 /****************************************************************************
83 ****************************************************************************/
84 static struct set *tarvals; /* container for tarval structs */
85 static struct set *values; /* container for values */
87 /****************************************************************************
89 ****************************************************************************/
91 static int hash_val(const void *value, unsigned int length);
92 static int hash_tv(tarval *tv);
93 static void _fail_verify(tarval *tv, const char* file, int line)
95 /* print a memory image of the tarval and throw an assertion */
97 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
99 printf("%s:%d: Invalid tarval (null)", file, line);
103 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
106 INLINE static void tarval_verify(tarval *tv)
112 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
113 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
115 if (!FIND_TARVAL(tv)) fail_verify(tv);
116 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
122 static int hash_tv(tarval *tv)
124 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
127 static int hash_val(const void *value, unsigned int length)
130 unsigned int hash = 0;
132 /* scramble the byte - array */
133 for (i = 0; i < length; i++)
135 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
136 hash += (hash << 11) ^ (hash >> 17);
142 /* finds tarval with value/mode or creates new tarval */
143 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
150 /* if there already is such a value, it is returned, else value
151 * is copied into the set */
152 tv.value = INSERT_VALUE(value, length);
156 /* if there is such a tarval, it is returned, else tv is copied
158 return (tarval *)INSERT_TARVAL(&tv);
161 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
163 switch (get_mode_sort(mode))
165 case irms_int_number:
166 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
167 switch (GET_OVERFLOW_MODE()) {
169 return get_mode_max(mode);
172 char *temp = alloca(sc_get_buffer_length());
173 char *diff = alloca(sc_get_buffer_length());
174 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
175 sc_val_from_ulong(1, temp);
176 sc_add(diff, temp, diff);
177 sc_sub(value, diff, temp);
178 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
179 sc_sub(temp, diff, temp);
180 return get_tarval(temp, length, mode);
185 return get_tarval(value, length, mode);
188 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
189 switch (GET_OVERFLOW_MODE()) {
191 return get_mode_min(mode);
194 char *temp = alloca(sc_get_buffer_length());
195 char *diff = alloca(sc_get_buffer_length());
196 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
197 sc_val_from_ulong(1, temp);
198 sc_add(diff, temp, diff);
199 sc_add(value, diff, temp);
200 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
201 sc_add(temp, diff, temp);
202 return get_tarval(temp, length, mode);
207 return get_tarval(value, length, mode);
212 case irms_float_number:
213 if (SWITCH_NOINFINITY && fc_is_inf(value))
215 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
218 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
220 return get_mode_null(mode);
226 return get_tarval(value, length, mode);
231 * public variables declared in tv.h
233 static tarval reserved_tv[5];
235 tarval *tarval_bad = &reserved_tv[0];
236 tarval *tarval_undefined = &reserved_tv[1];
237 tarval *tarval_b_false = &reserved_tv[2];
238 tarval *tarval_b_true = &reserved_tv[3];
239 tarval *tarval_P_void = &reserved_tv[4];
242 * public functions declared in tv.h
246 * Constructors =============================================================
248 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
255 switch (get_mode_sort(mode))
257 case irms_control_flow:
263 case irms_internal_boolean:
264 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
265 if (strcasecmp(str, "true")) return tarval_b_true;
266 else if (strcasecmp(str, "false")) return tarval_b_true;
268 /* XXX This is C semantics */
269 return atoi(str) ? tarval_b_true : tarval_b_false;
271 case irms_float_number:
272 switch(get_mode_size_bits(mode)) {
274 fc_val_from_str(str, len, 8, 23, NULL);
277 fc_val_from_str(str, len, 11, 52, NULL);
280 fc_val_from_str(str, len, 15, 64, NULL);
283 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
285 case irms_int_number:
287 sc_val_from_str(str, len, NULL);
288 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
291 return get_tarval(str, len, mode);
294 assert(0); /* can't be reached, can it? */
299 * helper function, create a tarval from long
301 tarval *new_tarval_from_long(long l, ir_mode *mode)
304 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
306 switch(get_mode_sort(mode))
308 case irms_internal_boolean:
309 /* XXX C semantics ! */
310 return l ? tarval_b_true : tarval_b_false ;
312 case irms_int_number:
314 sc_val_from_long(l, NULL);
315 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
317 case irms_float_number:
318 return new_tarval_from_double((long double)l, mode);
321 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
329 /* returns non-zero if can be converted to long */
330 int tarval_is_long(tarval *tv)
333 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
335 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
337 /* the value might be too big to fit in a long */
338 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
339 if (sc_comp(sc_get_buffer(), tv->value) == -1)
341 /* really doesn't fit */
348 /* this might overflow the machine's long, so use only with small values */
349 long tarval_to_long(tarval* tv)
352 assert(tarval_is_long(tv) && "tarval too big to fit in long");
354 return sc_val_to_long(tv->value);
357 tarval *new_tarval_from_double(long double d, ir_mode *mode)
360 assert(mode && (get_mode_sort(mode) == irms_float_number));
362 switch (get_mode_size_bits(mode)) {
364 fc_val_from_float(d, 8, 23, NULL);
367 fc_val_from_float(d, 11, 52, NULL);
370 fc_val_from_float(d, 15, 64, NULL);
373 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
376 /* returns non-zero if can be converted to double */
377 int tarval_is_double(tarval *tv)
382 return (get_mode_sort(tv->mode) == irms_float_number);
385 long double tarval_to_double(tarval *tv)
388 assert(tarval_is_double(tv));
390 return fc_val_to_float(tv->value);
393 /* The tarval represents the address of the entity. As the address must
394 be constant the entity must have as owner the global type. */
395 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
399 assert(mode && (get_mode_sort(mode) == irms_reference));
401 return get_tarval((void *)ent, 0, mode);
403 int tarval_is_entity(tarval *tv)
407 /* tv->value == NULL means dereferencing a null pointer */
408 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
409 && (tv != tarval_P_void));
412 entity *tarval_to_entity(tarval *tv)
417 if (tarval_is_entity(tv))
418 return (entity *)tv->value;
420 assert(0 && "tarval did not represent an entity");
425 void free_tarval_entity(entity *ent) {
426 /* There can be a tarval referencing this entity. Even if the
427 tarval is not used by the code any more, it can still reference
428 the entity as tarvals live indepently of the entity referenced.
429 Further the tarval is hashed into a set. If a hash function
430 evaluation happens to collide with this tarval, we will vrfy that
431 it contains a proper entity and we will crash if the entity is
434 Unluckily, tarvals can neither be changed nor deleted, and to find
435 one, all existing reference modes have to be tried -> a facility
436 to retrieve all modes of a kind is needed. */
441 * Access routines for tarval fields ========================================
443 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
451 * Special value query functions ============================================
453 * These functions calculate and return a tarval representing the requested
455 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
456 * functions, but these are stored on initialization of the irmode module and
457 * therefore the irmode functions should be prefered to the functions below.
460 tarval *get_tarval_bad(void)
465 tarval *get_tarval_undefined(void)
468 return tarval_undefined;
470 tarval *get_tarval_b_false(void)
473 return tarval_b_false;
475 tarval *get_tarval_b_true(void)
478 return tarval_b_true;
480 tarval *get_tarval_P_void(void)
483 return tarval_P_void;
486 tarval *get_tarval_max(ir_mode *mode)
491 switch(get_mode_sort(mode))
494 case irms_control_flow:
500 case irms_internal_boolean:
501 return tarval_b_true;
503 case irms_float_number:
504 switch(get_mode_size_bits(mode))
507 fc_get_max(8, 23, NULL);
510 fc_get_max(11, 52, NULL);
513 fc_get_max(15, 64, NULL);
516 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
518 case irms_int_number:
520 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
521 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
526 tarval *get_tarval_min(ir_mode *mode)
531 switch(get_mode_sort(mode))
534 case irms_control_flow:
540 case irms_internal_boolean:
541 return tarval_b_false;
543 case irms_float_number:
544 switch(get_mode_size_bits(mode))
547 fc_get_min(8, 23, NULL);
550 fc_get_min(11, 52, NULL);
553 fc_get_min(15, 64, NULL);
556 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
558 case irms_int_number:
560 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
561 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
566 tarval *get_tarval_null(ir_mode *mode)
571 switch(get_mode_sort(mode))
573 case irms_control_flow:
576 case irms_internal_boolean:
580 case irms_float_number:
581 return new_tarval_from_double(0.0, mode);
583 case irms_int_number:
585 return new_tarval_from_long(0l, mode);
588 return tarval_P_void;
593 tarval *get_tarval_one(ir_mode *mode)
598 switch(get_mode_sort(mode))
600 case irms_control_flow:
603 case irms_internal_boolean:
608 case irms_float_number:
609 return new_tarval_from_double(1.0, mode);
611 case irms_int_number:
613 return new_tarval_from_long(1l, mode);
619 tarval *get_tarval_nan(ir_mode *mode)
624 if (get_mode_sort(mode) == irms_float_number) {
625 switch(get_mode_size_bits(mode))
628 fc_get_qnan(8, 23, NULL);
631 fc_get_qnan(11, 52, NULL);
634 fc_get_qnan(15, 64, NULL);
637 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
640 assert(0 && "tarval is not floating point");
645 tarval *get_tarval_inf(ir_mode *mode)
650 if (get_mode_sort(mode) == irms_float_number) {
651 switch(get_mode_size_bits(mode))
654 fc_get_plusinf(8, 23, NULL);
657 fc_get_plusinf(11, 52, NULL);
660 fc_get_plusinf(15, 64, NULL);
663 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
666 assert(0 && "tarval is not floating point");
672 * Arithmethic operations on tarvals ========================================
676 * test if negative number, 1 means 'yes'
678 int tarval_is_negative(tarval *a)
683 switch (get_mode_sort(a->mode))
685 case irms_int_number:
686 if (!mode_is_signed(a->mode)) return 0;
688 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
690 case irms_float_number:
691 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
694 assert(0 && "not implemented");
700 * test if null, 1 means 'yes'
702 int tarval_is_null(tarval *a)
704 ir_mode *m = get_tarval_mode(a);
706 return a == get_tarval_null(m);
712 pnc_number tarval_cmp(tarval *a, tarval *b)
718 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
719 if (a == tarval_undefined || b == tarval_undefined) return False;
720 if (a == b) return Eq;
721 if (get_tarval_mode(a) != get_tarval_mode(b)) return False;
723 /* Here the two tarvals are unequal and of the same mode */
724 switch (get_mode_sort(a->mode))
726 case irms_control_flow:
732 case irms_float_number:
733 switch (fc_comp(a->value, b->value)) {
735 case 0: assert(0 && "different tarvals compare equal"); return Eq;
738 default: return False;
740 case irms_int_number:
742 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
744 case irms_internal_boolean:
745 return (a == tarval_b_true)?(Gt):(Lt);
751 * convert to other mode
753 tarval *tarval_convert_to(tarval *src, ir_mode *m)
761 if (src->mode == m) return src;
763 switch (get_mode_sort(src->mode))
765 case irms_control_flow:
770 /* cast float to something */
771 case irms_float_number:
772 switch (get_mode_sort(m)) {
773 case irms_float_number:
774 switch (get_mode_size_bits(m))
777 fc_cast(src->value, 8, 23, NULL);
780 fc_cast(src->value, 11, 52, NULL);
783 fc_cast(src->value, 15, 64, NULL);
788 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
791 case irms_int_number:
792 switch (GET_FLOAT_TO_INT_MODE())
795 fc_int(src->value, NULL);
798 fc_rnd(src->value, NULL);
803 /* XXX floating point unit can't produce a value in integer
805 * an intermediate representation is needed here first. */
806 /* return get_tarval(); */
811 /* the rest can't be converted */
816 /* cast int to something */
817 case irms_int_number:
818 switch (get_mode_sort(m)) {
819 case irms_int_number:
821 return get_tarval_overflow(src->value, src->length, m);
823 case irms_internal_boolean:
824 /* XXX C semantics */
825 if (src == get_mode_null(src->mode)) return tarval_b_false;
826 else return tarval_b_true;
828 case irms_float_number:
829 /* XXX floating point unit does not understand internal integer
830 * representation, convert to string first, then create float from
832 buffer = alloca(100);
833 /* decimal string representation because hexadecimal output is
834 * interpreted unsigned by fc_val_from_str, so this is a HACK */
835 snprintf(buffer, 100, "%s",
836 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
837 switch (get_mode_size_bits(m))
840 fc_val_from_str(buffer, 0, 8, 23, NULL);
843 fc_val_from_str(buffer, 0, 11, 52, NULL);
846 fc_val_from_str(buffer, 0, 15, 64, NULL);
849 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
856 case irms_internal_boolean:
857 switch (get_mode_sort(m))
859 case irms_int_number:
860 if (src == tarval_b_true) return get_mode_one(m);
861 else return get_mode_null(m);
880 tarval *tarval_neg(tarval *a)
886 assert(mode_is_num(a->mode)); /* negation only for numerical values */
887 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
889 switch (get_mode_sort(a->mode))
891 case irms_int_number:
892 buffer = alloca(sc_get_buffer_length());
893 sc_neg(a->value, buffer);
894 return get_tarval_overflow(buffer, a->length, a->mode);
896 case irms_float_number:
897 fc_neg(a->value, NULL);
898 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
908 tarval *tarval_add(tarval *a, tarval *b)
915 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
917 switch (get_mode_sort(a->mode))
920 case irms_int_number:
921 /* modes of a,b are equal, so result has mode of a as this might be the character */
922 buffer = alloca(sc_get_buffer_length());
923 sc_add(a->value, b->value, buffer);
924 return get_tarval_overflow(buffer, a->length, a->mode);
926 case irms_float_number:
927 fc_add(a->value, b->value, NULL);
928 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
938 tarval *tarval_sub(tarval *a, tarval *b)
945 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
947 switch (get_mode_sort(a->mode))
950 case irms_int_number:
951 /* modes of a,b are equal, so result has mode of a as this might be the character */
952 buffer = alloca(sc_get_buffer_length());
953 sc_sub(a->value, b->value, buffer);
954 return get_tarval_overflow(buffer, a->length, a->mode);
956 case irms_float_number:
957 fc_sub(a->value, b->value, NULL);
958 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
968 tarval *tarval_mul(tarval *a, tarval *b)
975 assert((a->mode == b->mode) && mode_is_num(a->mode));
977 switch (get_mode_sort(a->mode))
979 case irms_int_number:
980 /* modes of a,b are equal */
981 buffer = alloca(sc_get_buffer_length());
982 sc_mul(a->value, b->value, buffer);
983 return get_tarval_overflow(buffer, a->length, a->mode);
985 case irms_float_number:
986 fc_mul(a->value, b->value, NULL);
987 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
995 * floating point division
997 tarval *tarval_quo(tarval *a, tarval *b)
1002 assert((a->mode == b->mode) && mode_is_float(a->mode));
1004 fc_div(a->value, b->value, NULL);
1005 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1010 * overflow is impossible, but look out for division by zero
1012 tarval *tarval_div(tarval *a, tarval *b)
1017 assert((a->mode == b->mode) && mode_is_int(a->mode));
1020 if (b == get_mode_null(b->mode)) return tarval_bad;
1021 /* modes of a,b are equal */
1022 sc_div(a->value, b->value, NULL);
1023 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1028 * overflow is impossible, but look out for division by zero
1030 tarval *tarval_mod(tarval *a, tarval *b)
1035 assert((a->mode == b->mode) && mode_is_int(a->mode));
1038 if (b == get_mode_null(b->mode)) return tarval_bad;
1039 /* modes of a,b are equal */
1040 sc_mod(a->value, b->value, NULL);
1041 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1047 tarval *tarval_abs(tarval *a)
1053 assert(mode_is_num(a->mode));
1055 switch (get_mode_sort(a->mode))
1057 case irms_int_number:
1058 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1060 buffer = alloca(sc_get_buffer_length());
1061 sc_neg(a->value, buffer);
1062 return get_tarval_overflow(buffer, a->length, a->mode);
1066 case irms_float_number:
1067 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1069 fc_neg(a->value, NULL);
1070 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1083 tarval *tarval_and(tarval *a, tarval *b)
1088 assert(a->mode == b->mode);
1090 switch(get_mode_sort(a->mode))
1092 case irms_internal_boolean:
1093 return (a == tarval_b_false) ? a : b;
1095 case irms_int_number:
1096 sc_and(a->value, b->value, NULL);
1097 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1100 assert(0 && "operation not defined on mode");
1108 tarval *tarval_or (tarval *a, tarval *b)
1113 assert(a->mode == b->mode);
1115 switch (get_mode_sort(a->mode))
1117 case irms_internal_boolean:
1118 return (a == tarval_b_true) ? a : b;
1120 case irms_int_number:
1121 sc_or(a->value, b->value, NULL);
1122 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1125 assert(0 && "operation not defined on mode");
1131 * bitwise exclusive or (xor)
1133 tarval *tarval_eor(tarval *a, tarval *b)
1138 assert((a->mode == b->mode));
1140 switch (get_mode_sort(a->mode))
1142 case irms_internal_boolean:
1143 return (a == b)? tarval_b_false : tarval_b_true;
1145 case irms_int_number:
1146 sc_or(a->value, b->value, NULL);
1147 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1150 assert(0 && "operation not defined on mode");
1156 * bitwise left shift
1158 tarval *tarval_shl(tarval *a, tarval *b)
1163 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1165 sc_shl(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1166 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1170 * bitwise unsigned right shift
1172 tarval *tarval_shr(tarval *a, tarval *b)
1177 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1179 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1180 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1184 * bitwise signed right shift
1186 tarval *tarval_shrs(tarval *a, tarval *b)
1191 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1193 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1194 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1200 tarval *tarval_rot(tarval *a, tarval *b)
1205 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1207 sc_rot(a->value, b->value, 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);
1215 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1217 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1221 const tarval_mode_info *mode_info;
1222 const char *prefix, *suffix;
1226 mode_info = tv->mode->tv_priv;
1228 mode_info = &default_info;
1229 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1230 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1232 switch (get_mode_sort(tv->mode))
1234 case irms_int_number:
1235 case irms_character:
1236 switch (mode_info->mode_output) {
1239 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1243 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1249 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1252 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1254 case irms_float_number:
1255 switch (mode_info->mode_output) {
1257 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1260 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1265 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1269 case irms_reference:
1270 if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1271 if (tv->value != NULL)
1272 if (tarval_is_entity(tv)) {
1273 if (get_entity_peculiarity((entity *)tv->value) == peculiarity_existent)
1274 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1276 if (mode_info->mode_output == TVO_NATIVE)
1277 return snprintf(buf, len, "NULL");
1279 return snprintf(buf, len, "0");
1283 if (size > tv->length) {
1284 memcpy(buf, tv->value, tv->length);
1285 buf[tv->length] = '\0';
1289 memcpy(buf, tv->value, size-1);
1295 return snprintf(buf, len, "void");
1297 case irms_internal_boolean:
1298 switch (mode_info->mode_output) {
1304 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1308 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1311 case irms_control_flow:
1313 case irms_auxiliary:
1314 return snprintf(buf, len, "<BAD>");
1322 * Output of tarvals to stdio.
1324 int tarval_printf(tarval *tv) {
1328 res = tarval_snprintf(buf, sizeof(buf), tv);
1329 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1335 char *tarval_bitpattern(tarval *tv)
1341 * access to the bitpattern
1343 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1345 switch (get_mode_sort(tv->mode)) {
1346 case irms_int_number:
1347 case irms_character:
1348 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1350 case irms_float_number:
1351 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1359 * Specify the output options of one mode.
1361 * This functions stores the modinfo, so DO NOT DESTROY it.
1363 * Returns zero on success.
1365 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1369 mode->tv_priv = modeinfo;
1374 * Returns the output options of one mode.
1376 * This functions returns the modinfo of a given mode.
1378 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1382 return mode->tv_priv;
1386 /* Identifying some tarvals ??? */
1387 /* Implemented in old tv.c as such:
1388 * return 0 for additive neutral,
1389 * 1 for multiplicative neutral,
1390 * -1 for bitwise-and neutral
1393 * Implemented for compatibility */
1394 long tarval_classify(tarval *tv)
1397 if (!tv || tv == tarval_bad) return 2;
1399 if (tv == get_mode_null(tv->mode)) return 0;
1400 else if (tv == get_mode_one(tv->mode)) return 1;
1401 else if ((get_mode_sort(tv->mode) == irms_int_number)
1402 && (tv == new_tarval_from_long(-1, tv->mode))) return -1;
1408 * default mode_info for output as HEX
1410 static const tarval_mode_info hex_output = {
1417 * default mode_info for output as reference
1419 static const tarval_mode_info reference_output = {
1427 * Initialization of the tarval module: called before init_mode()
1429 void init_tarval_1(void)
1432 /* initialize the sets holding the tarvals with a comparison function and
1433 * an initial size, which is the expected number of constants */
1434 tarvals = new_set(memcmp, N_CONSTANTS);
1435 values = new_set(memcmp, N_CONSTANTS);
1436 /* init strcalc with precision of 68 to support floating point values with 64
1437 * bit mantissa (needs extra bits for rounding and overflow) */
1443 * Initialization of the tarval module: called after init_mode()
1445 void init_tarval_2(void)
1449 tarval_bad->mode = mode_BAD;
1450 tarval_undefined->mode = mode_ANY;
1451 tarval_b_true->mode = mode_b;
1452 tarval_b_false->mode = mode_b;
1453 tarval_P_void->mode = mode_P;
1456 * assign output modes that are compatible with the
1457 * old implementation: Hex output
1459 tarval_set_mode_output_option(mode_U, &hex_output);
1460 tarval_set_mode_output_option(mode_C, &hex_output);
1461 tarval_set_mode_output_option(mode_Bs, &hex_output);
1462 tarval_set_mode_output_option(mode_Bu, &hex_output);
1463 tarval_set_mode_output_option(mode_Hs, &hex_output);
1464 tarval_set_mode_output_option(mode_Hu, &hex_output);
1465 tarval_set_mode_output_option(mode_Is, &hex_output);
1466 tarval_set_mode_output_option(mode_Iu, &hex_output);
1467 tarval_set_mode_output_option(mode_Ls, &hex_output);
1468 tarval_set_mode_output_option(mode_Lu, &hex_output);
1469 tarval_set_mode_output_option(mode_P, &reference_output);
1472 /****************************************************************************
1474 ****************************************************************************/