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 /* XXX hack until theres's a proper interface */
45 #define GET_OVERFLOW_MODE() BAD
47 /* unused, float to int doesn't work yet */
50 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
52 #define SWITCH_NOINFINITY 0
53 #define SWITCH_NODENORMALS 0
55 /****************************************************************************
56 * local definitions and macros
57 ****************************************************************************/
59 # define TARVAL_VERIFY(a) tarval_verify((a))
61 # define TARVAL_VERIFY(a) ((void)0)
64 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
65 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
67 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
68 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
70 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
72 static long long count = 0;
73 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
75 # define ANNOUNCE() ((void)0)
77 /****************************************************************************
79 ****************************************************************************/
80 static struct set *tarvals; /* container for tarval structs */
81 static struct set *values; /* container for values */
83 /****************************************************************************
85 ****************************************************************************/
87 static int hash_val(const void *value, unsigned int length);
88 static int hash_tv(tarval *tv);
89 static void _fail_verify(tarval *tv, const char* file, int line)
91 /* print a memory image of the tarval and throw an assertion */
93 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
95 printf("%s:%d: Invalid tarval (null)", file, line);
99 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
102 INLINE static void tarval_verify(tarval *tv)
108 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
109 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
111 if (!FIND_TARVAL(tv)) fail_verify(tv);
112 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
118 static int hash_tv(tarval *tv)
120 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
123 static int hash_val(const void *value, unsigned int length)
126 unsigned int hash = 0;
128 /* scramble the byte - array */
129 for (i = 0; i < length; i++)
131 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
132 hash += (hash << 11) ^ (hash >> 17);
138 /* finds tarval with value/mode or creates new tarval */
139 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
146 /* if there already is such a value, it is returned, else value
147 * is copied into the set */
148 tv.value = INSERT_VALUE(value, length);
152 /* if there is such a tarval, it is returned, else tv is copied
154 return (tarval *)INSERT_TARVAL(&tv);
157 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
159 switch (get_mode_sort(mode))
161 case irms_int_number:
162 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
163 switch (GET_OVERFLOW_MODE()) {
165 return get_mode_max(mode);
168 char *temp = alloca(sc_get_buffer_length());
169 char *diff = alloca(sc_get_buffer_length());
170 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
171 sc_val_from_ulong(1, temp);
172 sc_add(diff, temp, diff);
173 sc_sub(value, diff, temp);
174 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
175 sc_sub(temp, diff, temp);
176 return get_tarval(temp, length, mode);
181 return get_tarval(value, length, mode);
184 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
185 switch (GET_OVERFLOW_MODE()) {
187 return get_mode_min(mode);
190 char *temp = alloca(sc_get_buffer_length());
191 char *diff = alloca(sc_get_buffer_length());
192 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
193 sc_val_from_ulong(1, temp);
194 sc_add(diff, temp, diff);
195 sc_add(value, diff, temp);
196 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
197 sc_add(temp, diff, temp);
198 return get_tarval(temp, length, mode);
203 return get_tarval(value, length, mode);
208 case irms_float_number:
209 if (SWITCH_NOINFINITY && fc_is_inf(value))
211 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
214 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
216 return get_mode_null(mode);
222 return get_tarval(value, length, mode);
227 * public variables declared in tv.h
230 tarval *tarval_undefined;
231 tarval *tarval_b_false;
232 tarval *tarval_b_true;
233 tarval *tarval_P_void;
236 * public functions declared in tv.h
240 * Constructors =============================================================
242 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
249 switch (get_mode_sort(mode))
251 case irms_control_flow:
257 case irms_internal_boolean:
258 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
259 if (strcasecmp(str, "true")) return tarval_b_true;
260 else if (strcasecmp(str, "false")) return tarval_b_true;
262 /* XXX This is C semantics */
263 return atoi(str) ? tarval_b_true : tarval_b_false;
265 case irms_float_number:
266 switch(get_mode_size_bits(mode)) {
268 fc_val_from_str(str, len, 8, 23, NULL);
271 fc_val_from_str(str, len, 11, 52, NULL);
274 fc_val_from_str(str, len, 15, 64, NULL);
277 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
279 case irms_int_number:
281 sc_val_from_str(str, len, NULL);
282 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
285 return get_tarval(str, len, mode);
288 assert(0); /* can't be reached, can it? */
293 * helper function, create a tarval from long
295 tarval *new_tarval_from_long(long l, ir_mode *mode)
298 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
300 switch(get_mode_sort(mode))
302 case irms_internal_boolean:
303 /* XXX C semantics ! */
304 return l ? tarval_b_true : tarval_b_false ;
306 case irms_int_number:
308 sc_val_from_long(l, NULL);
309 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
311 case irms_float_number:
312 return new_tarval_from_double((long double)l, mode);
315 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
323 /* returns non-zero if can be converted to long */
324 int tarval_is_long(tarval *tv)
327 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
329 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
331 /* the value might be too big to fit in a long */
332 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
333 if (sc_comp(sc_get_buffer(), tv->value) == -1)
335 /* really doesn't fit */
342 /* this might overflow the machine's long, so use only with small values */
343 long tarval_to_long(tarval* tv)
346 assert(tarval_is_long(tv) && "tarval too big to fit in long");
348 return sc_val_to_long(tv->value);
351 tarval *new_tarval_from_double(long double d, ir_mode *mode)
354 assert(mode && (get_mode_sort(mode) == irms_float_number));
356 switch (get_mode_size_bits(mode)) {
358 fc_val_from_float(d, 8, 23, NULL);
361 fc_val_from_float(d, 11, 52, NULL);
364 fc_val_from_float(d, 15, 64, NULL);
367 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
370 /* returns non-zero if can be converted to double */
371 int tarval_is_double(tarval *tv)
376 return (get_mode_sort(tv->mode) == irms_float_number);
379 long double tarval_to_double(tarval *tv)
382 assert(tarval_is_double(tv));
384 return fc_val_to_float(tv->value);
387 /* The tarval represents the address of the entity. As the address must
388 be constant the entity must have as owner the global type. */
389 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
393 assert(mode && (get_mode_sort(mode) == irms_reference));
395 return get_tarval((void *)ent, 0, mode);
397 int tarval_is_entity(tarval *tv)
401 /* tv->value == NULL means dereferencing a null pointer */
402 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
403 && (tv != tarval_P_void));
406 entity *tarval_to_entity(tarval *tv)
411 if (tarval_is_entity(tv))
412 return (entity *)tv->value;
414 assert(0 && "tarval did not represent an entity");
419 void free_tarval_entity(entity *ent) {
420 /* There can be a tarval referencing this entity. Even if the
421 tarval is not used by the code any more, it can still reference
422 the entity as tarvals live indepently of the entity referenced.
423 Further the tarval is hashed into a set. If a hash function
424 evaluation happens to collide with this tarval, we will vrfy that
425 it contains a proper entity and we will crash if the entity is
428 Unluckily, tarvals can neither be changed nor deleted, and to find
429 one, all existing reference modes have to be tried -> a facility
430 to retrieve all modes of a kind is needed. */
435 * Access routines for tarval fields ========================================
437 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
445 * Special value query functions ============================================
447 * These functions calculate and return a tarval representing the requested
449 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
450 * functions, but these are stored on initialization of the irmode module and
451 * therefore the irmode functions should be prefered to the functions below.
454 tarval *get_tarval_bad(void)
459 tarval *get_tarval_undefined(void)
462 return tarval_undefined;
464 tarval *get_tarval_b_false(void)
467 return tarval_b_false;
469 tarval *get_tarval_b_true(void)
472 return tarval_b_true;
474 tarval *get_tarval_P_void(void)
477 return tarval_P_void;
480 tarval *get_tarval_max(ir_mode *mode)
485 switch(get_mode_sort(mode))
488 case irms_control_flow:
494 case irms_internal_boolean:
495 return tarval_b_true;
497 case irms_float_number:
498 switch(get_mode_size_bits(mode))
501 fc_get_max(8, 23, NULL);
504 fc_get_max(11, 52, NULL);
507 fc_get_max(15, 64, NULL);
510 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
512 case irms_int_number:
514 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
515 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
520 tarval *get_tarval_min(ir_mode *mode)
525 switch(get_mode_sort(mode))
528 case irms_control_flow:
534 case irms_internal_boolean:
535 return tarval_b_false;
537 case irms_float_number:
538 switch(get_mode_size_bits(mode))
541 fc_get_min(8, 23, NULL);
544 fc_get_min(11, 52, NULL);
547 fc_get_min(15, 64, NULL);
550 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
552 case irms_int_number:
554 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
555 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
560 tarval *get_tarval_null(ir_mode *mode)
565 switch(get_mode_sort(mode))
567 case irms_control_flow:
570 case irms_internal_boolean:
574 case irms_float_number:
575 return new_tarval_from_double(0.0, mode);
577 case irms_int_number:
579 return new_tarval_from_long(0l, mode);
582 return tarval_P_void;
587 tarval *get_tarval_one(ir_mode *mode)
592 switch(get_mode_sort(mode))
594 case irms_control_flow:
597 case irms_internal_boolean:
602 case irms_float_number:
603 return new_tarval_from_double(1.0, mode);
605 case irms_int_number:
607 return new_tarval_from_long(1l, mode);
613 tarval *get_tarval_nan(ir_mode *mode)
618 if (get_mode_sort(mode) == irms_float_number) {
619 switch(get_mode_size_bits(mode))
622 fc_get_qnan(8, 23, NULL);
625 fc_get_qnan(11, 52, NULL);
628 fc_get_qnan(15, 64, NULL);
631 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
634 assert(0 && "tarval is not floating point");
639 tarval *get_tarval_inf(ir_mode *mode)
644 if (get_mode_sort(mode) == irms_float_number) {
645 switch(get_mode_size_bits(mode))
648 fc_get_plusinf(8, 23, NULL);
651 fc_get_plusinf(11, 52, NULL);
654 fc_get_plusinf(15, 64, NULL);
657 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
660 assert(0 && "tarval is not floating point");
666 * Arithmethic operations on tarvals ========================================
670 * test if negative number, 1 means 'yes'
672 int tarval_is_negative(tarval *a)
677 switch (get_mode_sort(a->mode))
679 case irms_int_number:
680 if (!mode_is_signed(a->mode)) return 0;
682 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
684 case irms_float_number:
685 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
688 assert(0 && "not implemented");
694 * test if null, 1 means 'yes'
696 int tarval_is_null(tarval *a)
698 ir_mode *m = get_tarval_mode(a);
700 return a == get_tarval_null(m);
706 pnc_number tarval_cmp(tarval *a, tarval *b)
712 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
713 if (a == tarval_undefined || b == tarval_undefined) return False;
714 if (a == b) return Eq;
715 if (get_tarval_mode(a) != get_tarval_mode(b)) return False;
717 /* Here the two tarvals are unequal and of the same mode */
718 switch (get_mode_sort(a->mode))
720 case irms_control_flow:
726 case irms_float_number:
727 switch (fc_comp(a->value, b->value)) {
729 case 0: assert(0 && "different tarvals compare equal"); return Eq;
732 default: return False;
734 case irms_int_number:
736 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
738 case irms_internal_boolean:
739 return (a == tarval_b_true)?(Gt):(Lt);
745 * convert to other mode
747 tarval *tarval_convert_to(tarval *src, ir_mode *m)
755 if (src->mode == m) return src;
757 switch (get_mode_sort(src->mode))
759 case irms_control_flow:
764 /* cast float to something */
765 case irms_float_number:
766 switch (get_mode_sort(m)) {
767 case irms_float_number:
768 switch (get_mode_size_bits(m))
771 fc_cast(src->value, 8, 23, NULL);
774 fc_cast(src->value, 11, 52, NULL);
777 fc_cast(src->value, 15, 64, NULL);
782 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
785 case irms_int_number:
786 switch (GET_FLOAT_TO_INT_MODE())
789 fc_int(src->value, NULL);
792 fc_rnd(src->value, NULL);
797 /* XXX floating point unit can't produce a value in integer
799 * an intermediate representation is needed here first. */
800 /* return get_tarval(); */
805 /* the rest can't be converted */
810 /* cast int to something */
811 case irms_int_number:
812 switch (get_mode_sort(m)) {
813 case irms_int_number:
815 return get_tarval_overflow(src->value, src->length, m);
817 case irms_internal_boolean:
818 /* XXX C semantics */
819 if (src == get_mode_null(src->mode)) return tarval_b_false;
820 else return tarval_b_true;
822 case irms_float_number:
823 /* XXX floating point unit does not understand internal integer
824 * representation, convert to string first, then create float from
826 buffer = alloca(100);
827 /* decimal string representation because hexadecimal output is
828 * interpreted unsigned by fc_val_from_str, so this is a HACK */
829 snprintf(buffer, 100, "%s",
830 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
831 switch (get_mode_size_bits(m))
834 fc_val_from_str(buffer, 0, 8, 23, NULL);
837 fc_val_from_str(buffer, 0, 11, 52, NULL);
840 fc_val_from_str(buffer, 0, 15, 64, NULL);
843 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
850 case irms_internal_boolean:
851 switch (get_mode_sort(m))
853 case irms_int_number:
854 if (src == tarval_b_true) return get_mode_one(m);
855 else return get_mode_null(m);
874 tarval *tarval_neg(tarval *a)
880 assert(mode_is_num(a->mode)); /* negation only for numerical values */
881 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
883 switch (get_mode_sort(a->mode))
885 case irms_int_number:
886 buffer = alloca(sc_get_buffer_length());
887 sc_neg(a->value, buffer);
888 return get_tarval_overflow(buffer, a->length, a->mode);
890 case irms_float_number:
891 fc_neg(a->value, NULL);
892 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
902 tarval *tarval_add(tarval *a, tarval *b)
909 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
911 switch (get_mode_sort(a->mode))
914 case irms_int_number:
915 /* modes of a,b are equal, so result has mode of a as this might be the character */
916 buffer = alloca(sc_get_buffer_length());
917 sc_add(a->value, b->value, buffer);
918 return get_tarval_overflow(buffer, a->length, a->mode);
920 case irms_float_number:
921 fc_add(a->value, b->value, NULL);
922 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
932 tarval *tarval_sub(tarval *a, tarval *b)
939 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
941 switch (get_mode_sort(a->mode))
944 case irms_int_number:
945 /* modes of a,b are equal, so result has mode of a as this might be the character */
946 buffer = alloca(sc_get_buffer_length());
947 sc_sub(a->value, b->value, buffer);
948 return get_tarval_overflow(buffer, a->length, a->mode);
950 case irms_float_number:
951 fc_sub(a->value, b->value, NULL);
952 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
962 tarval *tarval_mul(tarval *a, tarval *b)
969 assert((a->mode == b->mode) && mode_is_num(a->mode));
971 switch (get_mode_sort(a->mode))
973 case irms_int_number:
974 /* modes of a,b are equal */
975 buffer = alloca(sc_get_buffer_length());
976 sc_mul(a->value, b->value, buffer);
977 return get_tarval_overflow(buffer, a->length, a->mode);
979 case irms_float_number:
980 fc_mul(a->value, b->value, NULL);
981 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
989 * floating point division
991 tarval *tarval_quo(tarval *a, tarval *b)
996 assert((a->mode == b->mode) && mode_is_float(a->mode));
998 fc_div(a->value, b->value, NULL);
999 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1004 * overflow is impossible, but look out for division by zero
1006 tarval *tarval_div(tarval *a, tarval *b)
1011 assert((a->mode == b->mode) && mode_is_int(a->mode));
1014 if (b == get_mode_null(b->mode)) return tarval_bad;
1015 /* modes of a,b are equal */
1016 sc_div(a->value, b->value, NULL);
1017 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1022 * overflow is impossible, but look out for division by zero
1024 tarval *tarval_mod(tarval *a, tarval *b)
1029 assert((a->mode == b->mode) && mode_is_int(a->mode));
1032 if (b == get_mode_null(b->mode)) return tarval_bad;
1033 /* modes of a,b are equal */
1034 sc_mod(a->value, b->value, NULL);
1035 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1041 tarval *tarval_abs(tarval *a)
1047 assert(mode_is_num(a->mode));
1049 switch (get_mode_sort(a->mode))
1051 case irms_int_number:
1052 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1054 buffer = alloca(sc_get_buffer_length());
1055 sc_neg(a->value, buffer);
1056 return get_tarval_overflow(buffer, a->length, a->mode);
1060 case irms_float_number:
1061 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1063 fc_neg(a->value, NULL);
1064 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1077 tarval *tarval_and(tarval *a, tarval *b)
1082 assert(a->mode == b->mode);
1084 switch(get_mode_sort(a->mode))
1086 case irms_internal_boolean:
1087 return (a == tarval_b_false) ? a : b;
1089 case irms_int_number:
1090 sc_and(a->value, b->value, NULL);
1091 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1094 assert(0 && "operation not defined on mode");
1102 tarval *tarval_or (tarval *a, tarval *b)
1107 assert(a->mode == b->mode);
1109 switch (get_mode_sort(a->mode))
1111 case irms_internal_boolean:
1112 return (a == tarval_b_true) ? a : b;
1114 case irms_int_number:
1115 sc_or(a->value, b->value, NULL);
1116 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1119 assert(0 && "operation not defined on mode");
1125 * bitwise exclusive or (xor)
1127 tarval *tarval_eor(tarval *a, tarval *b)
1132 assert((a->mode == b->mode));
1134 switch (get_mode_sort(a->mode))
1136 case irms_internal_boolean:
1137 return (a == b)? tarval_b_false : tarval_b_true;
1139 case irms_int_number:
1140 sc_or(a->value, b->value, NULL);
1141 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1144 assert(0 && "operation not defined on mode");
1150 * bitwise left shift
1152 tarval *tarval_shl(tarval *a, tarval *b)
1157 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1159 sc_shl(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1160 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1164 * bitwise unsigned right shift
1166 tarval *tarval_shr(tarval *a, tarval *b)
1171 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1173 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1174 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1178 * bitwise signed right shift
1180 tarval *tarval_shrs(tarval *a, tarval *b)
1185 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1187 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1188 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1194 tarval *tarval_rot(tarval *a, tarval *b)
1199 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1201 sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1202 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1209 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1211 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1215 const tarval_mode_info *mode_info;
1216 const char *prefix, *suffix;
1220 mode_info = tv->mode->tv_priv;
1222 mode_info = &default_info;
1223 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1224 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1226 switch (get_mode_sort(tv->mode))
1228 case irms_int_number:
1229 case irms_character:
1230 switch (mode_info->mode_output) {
1233 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1237 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1243 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1246 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1248 case irms_float_number:
1249 switch (mode_info->mode_output) {
1251 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1254 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1259 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1263 case irms_reference:
1264 if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1265 if (tv->value != NULL)
1266 if (tarval_is_entity(tv)) {
1267 if (get_entity_peculiarity((entity *)tv->value) == existent)
1268 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1270 if (mode_info->mode_output == TVO_NATIVE)
1271 return snprintf(buf, len, "NULL");
1273 return snprintf(buf, len, "0");
1277 if (size > tv->length) {
1278 memcpy(buf, tv->value, tv->length);
1279 buf[tv->length] = '\0';
1283 memcpy(buf, tv->value, size-1);
1289 return snprintf(buf, len, "void");
1291 case irms_internal_boolean:
1292 switch (mode_info->mode_output) {
1298 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1302 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1305 case irms_control_flow:
1307 case irms_auxiliary:
1308 return snprintf(buf, len, "<BAD>");
1316 * Output of tarvals to stdio.
1318 int tarval_printf(tarval *tv) {
1322 res = tarval_snprintf(buf, sizeof(buf), tv);
1323 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1329 char *tarval_bitpattern(tarval *tv)
1335 * access to the bitpattern
1337 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1339 switch (get_mode_sort(tv->mode)) {
1340 case irms_int_number:
1341 case irms_character:
1342 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1344 case irms_float_number:
1345 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1353 * Specify the output options of one mode.
1355 * This functions stores the modinfo, so DO NOT DESTROY it.
1357 * Returns zero on success.
1359 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1363 mode->tv_priv = modeinfo;
1368 * Returns the output options of one mode.
1370 * This functions returns the modinfo of a given mode.
1372 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1376 return mode->tv_priv;
1380 /* Identifying some tarvals ??? */
1381 /* Implemented in old tv.c as such:
1382 * return 0 for additive neutral,
1383 * 1 for multiplicative neutral,
1384 * -1 for bitwise-and neutral
1387 * Implemented for compatibility */
1388 long tarval_classify(tarval *tv)
1391 if (!tv || tv == tarval_bad) return 2;
1393 if (tv == get_mode_null(tv->mode)) return 0;
1394 else if (tv == get_mode_one(tv->mode)) return 1;
1395 else if ((get_mode_sort(tv->mode) == irms_int_number)
1396 && (tv == new_tarval_from_long(-1, tv->mode))) return -1;
1402 * default mode_info for output as HEX
1404 static const tarval_mode_info hex_output = {
1411 * default mode_info for output as reference
1413 static const tarval_mode_info reference_output = {
1421 * Initialization of the tarval module: called before init_mode()
1423 void init_tarval_1(void)
1426 /* initialize the sets holding the tarvals with a comparison function and
1427 * an initial size, which is the expected number of constants */
1428 tarvals = new_set(memcmp, TUNE_NCONSTANTS);
1429 values = new_set(memcmp, TUNE_NCONSTANTS);
1430 /* init strcalc with precision of 68 to support floating point values with 64
1431 * bit mantissa (needs extra bits for rounding and overflow) */
1435 tarval_bad = (tarval*)malloc(sizeof(tarval));
1436 tarval_undefined = (tarval*)malloc(sizeof(tarval));
1437 tarval_b_true = (tarval*)malloc(sizeof(tarval));
1438 tarval_b_false = (tarval*)malloc(sizeof(tarval));
1439 tarval_P_void = (tarval*)malloc(sizeof(tarval));
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 ****************************************************************************/