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_not(tarval *a)
886 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
888 switch (get_mode_sort(a->mode))
890 case irms_int_number:
891 buffer = alloca(sc_get_buffer_length());
892 sc_not(a->value, buffer);
893 return get_tarval(buffer, a->length, a->mode);
901 * arithmetic negation
903 tarval *tarval_neg(tarval *a)
909 assert(mode_is_num(a->mode)); /* negation only for numerical values */
910 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
912 switch (get_mode_sort(a->mode))
914 case irms_int_number:
915 buffer = alloca(sc_get_buffer_length());
916 sc_neg(a->value, buffer);
917 return get_tarval_overflow(buffer, a->length, a->mode);
919 case irms_float_number:
920 fc_neg(a->value, NULL);
921 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
931 tarval *tarval_add(tarval *a, tarval *b)
938 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
940 switch (get_mode_sort(a->mode))
943 case irms_int_number:
944 /* modes of a,b are equal, so result has mode of a as this might be the character */
945 buffer = alloca(sc_get_buffer_length());
946 sc_add(a->value, b->value, buffer);
947 return get_tarval_overflow(buffer, a->length, a->mode);
949 case irms_float_number:
950 fc_add(a->value, b->value, NULL);
951 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
961 tarval *tarval_sub(tarval *a, tarval *b)
968 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
970 switch (get_mode_sort(a->mode))
973 case irms_int_number:
974 /* modes of a,b are equal, so result has mode of a as this might be the character */
975 buffer = alloca(sc_get_buffer_length());
976 sc_sub(a->value, b->value, buffer);
977 return get_tarval_overflow(buffer, a->length, a->mode);
979 case irms_float_number:
980 fc_sub(a->value, b->value, NULL);
981 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
991 tarval *tarval_mul(tarval *a, tarval *b)
998 assert((a->mode == b->mode) && mode_is_num(a->mode));
1000 switch (get_mode_sort(a->mode))
1002 case irms_int_number:
1003 /* modes of a,b are equal */
1004 buffer = alloca(sc_get_buffer_length());
1005 sc_mul(a->value, b->value, buffer);
1006 return get_tarval_overflow(buffer, a->length, a->mode);
1008 case irms_float_number:
1009 fc_mul(a->value, b->value, NULL);
1010 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1018 * floating point division
1020 tarval *tarval_quo(tarval *a, tarval *b)
1025 assert((a->mode == b->mode) && mode_is_float(a->mode));
1027 fc_div(a->value, b->value, NULL);
1028 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1033 * overflow is impossible, but look out for division by zero
1035 tarval *tarval_div(tarval *a, tarval *b)
1040 assert((a->mode == b->mode) && mode_is_int(a->mode));
1043 if (b == get_mode_null(b->mode)) return tarval_bad;
1044 /* modes of a,b are equal */
1045 sc_div(a->value, b->value, NULL);
1046 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1051 * overflow is impossible, but look out for division by zero
1053 tarval *tarval_mod(tarval *a, tarval *b)
1058 assert((a->mode == b->mode) && mode_is_int(a->mode));
1061 if (b == get_mode_null(b->mode)) return tarval_bad;
1062 /* modes of a,b are equal */
1063 sc_mod(a->value, b->value, NULL);
1064 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1070 tarval *tarval_abs(tarval *a)
1076 assert(mode_is_num(a->mode));
1078 switch (get_mode_sort(a->mode))
1080 case irms_int_number:
1081 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1083 buffer = alloca(sc_get_buffer_length());
1084 sc_neg(a->value, buffer);
1085 return get_tarval_overflow(buffer, a->length, a->mode);
1089 case irms_float_number:
1090 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1092 fc_neg(a->value, NULL);
1093 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1106 tarval *tarval_and(tarval *a, tarval *b)
1111 assert(a->mode == b->mode);
1113 switch(get_mode_sort(a->mode))
1115 case irms_internal_boolean:
1116 return (a == tarval_b_false) ? a : b;
1118 case irms_int_number:
1119 sc_and(a->value, b->value, NULL);
1120 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1123 assert(0 && "operation not defined on mode");
1131 tarval *tarval_or (tarval *a, tarval *b)
1136 assert(a->mode == b->mode);
1138 switch (get_mode_sort(a->mode))
1140 case irms_internal_boolean:
1141 return (a == tarval_b_true) ? a : b;
1143 case irms_int_number:
1144 sc_or(a->value, b->value, NULL);
1145 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1148 assert(0 && "operation not defined on mode");
1154 * bitwise exclusive or (xor)
1156 tarval *tarval_eor(tarval *a, tarval *b)
1161 assert((a->mode == b->mode));
1163 switch (get_mode_sort(a->mode))
1165 case irms_internal_boolean:
1166 return (a == b)? tarval_b_false : tarval_b_true;
1168 case irms_int_number:
1169 sc_xor(a->value, b->value, NULL);
1170 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1173 assert(0 && "operation not defined on mode");
1179 * bitwise left shift
1181 tarval *tarval_shl(tarval *a, tarval *b)
1183 char *temp_val = NULL;
1187 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1189 if (get_mode_modulo_shift(a->mode) != 0)
1191 temp_val = alloca(sc_get_buffer_length());
1193 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1194 sc_mod(b->value, temp_val, temp_val);
1197 temp_val = (char*)b->value;
1199 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1200 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1204 * bitwise unsigned right shift
1206 tarval *tarval_shr(tarval *a, tarval *b)
1208 char *temp_val = NULL;
1212 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1214 if (get_mode_modulo_shift(a->mode) != 0)
1216 temp_val = alloca(sc_get_buffer_length());
1218 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1219 sc_mod(b->value, temp_val, temp_val);
1222 temp_val = (char*)b->value;
1224 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1225 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1229 * bitwise signed right shift
1231 tarval *tarval_shrs(tarval *a, tarval *b)
1233 char *temp_val = NULL;
1237 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1239 if (get_mode_modulo_shift(a->mode) != 0)
1241 temp_val = alloca(sc_get_buffer_length());
1243 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1244 sc_mod(b->value, temp_val, temp_val);
1247 temp_val = (char*)b->value;
1249 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1250 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1256 tarval *tarval_rot(tarval *a, tarval *b)
1258 char *temp_val = NULL;
1262 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1264 if (get_mode_modulo_shift(a->mode) != 0)
1266 temp_val = alloca(sc_get_buffer_length());
1268 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1269 sc_mod(b->value, temp_val, temp_val);
1272 temp_val = (char*)b->value;
1274 sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1275 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1282 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1284 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1288 const tarval_mode_info *mode_info;
1289 const char *prefix, *suffix;
1293 mode_info = tv->mode->tv_priv;
1295 mode_info = &default_info;
1296 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1297 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1299 switch (get_mode_sort(tv->mode))
1301 case irms_int_number:
1302 case irms_character:
1303 switch (mode_info->mode_output) {
1306 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1310 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1316 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1319 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1321 case irms_float_number:
1322 switch (mode_info->mode_output) {
1324 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1327 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1332 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1336 case irms_reference:
1337 if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1338 if (tv->value != NULL)
1339 if (tarval_is_entity(tv)) {
1340 if (get_entity_peculiarity((entity *)tv->value) == peculiarity_existent)
1341 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1343 if (mode_info->mode_output == TVO_NATIVE)
1344 return snprintf(buf, len, "NULL");
1346 return snprintf(buf, len, "0");
1350 if (size > tv->length) {
1351 memcpy(buf, tv->value, tv->length);
1352 buf[tv->length] = '\0';
1356 memcpy(buf, tv->value, size-1);
1362 return snprintf(buf, len, "void");
1364 case irms_internal_boolean:
1365 switch (mode_info->mode_output) {
1371 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1375 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1378 case irms_control_flow:
1380 case irms_auxiliary:
1381 return snprintf(buf, len, "<BAD>");
1389 * Output of tarvals to stdio.
1391 int tarval_printf(tarval *tv) {
1395 res = tarval_snprintf(buf, sizeof(buf), tv);
1396 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1402 char *tarval_bitpattern(tarval *tv)
1408 * access to the bitpattern
1410 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1412 switch (get_mode_sort(tv->mode)) {
1413 case irms_int_number:
1414 case irms_character:
1415 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1417 case irms_float_number:
1418 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1426 * Specify the output options of one mode.
1428 * This functions stores the modinfo, so DO NOT DESTROY it.
1430 * Returns zero on success.
1432 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1436 mode->tv_priv = modeinfo;
1441 * Returns the output options of one mode.
1443 * This functions returns the modinfo of a given mode.
1445 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1449 return mode->tv_priv;
1453 /* Identifying some tarvals ??? */
1454 /* Implemented in old tv.c as such:
1455 * return 0 for additive neutral,
1456 * 1 for multiplicative neutral,
1457 * -1 for bitwise-and neutral
1460 * Implemented for compatibility */
1461 long tarval_classify(tarval *tv)
1464 if (!tv || tv == tarval_bad) return 2;
1466 if (tv == get_mode_null(tv->mode)) return 0;
1467 else if (tv == get_mode_one(tv->mode)) return 1;
1468 else if ((get_mode_sort(tv->mode) == irms_int_number)
1469 && (tv == new_tarval_from_long(-1, tv->mode))) return -1;
1475 * default mode_info for output as HEX
1477 static const tarval_mode_info hex_output = {
1484 * default mode_info for output as reference
1486 static const tarval_mode_info reference_output = {
1494 * Initialization of the tarval module: called before init_mode()
1496 void init_tarval_1(void)
1499 /* initialize the sets holding the tarvals with a comparison function and
1500 * an initial size, which is the expected number of constants */
1501 tarvals = new_set(memcmp, N_CONSTANTS);
1502 values = new_set(memcmp, N_CONSTANTS);
1503 /* init strcalc with precision of 68 to support floating point values with 64
1504 * bit mantissa (needs extra bits for rounding and overflow) */
1510 * Initialization of the tarval module: called after init_mode()
1512 void init_tarval_2(void)
1516 tarval_bad->mode = mode_BAD;
1517 tarval_undefined->mode = mode_ANY;
1518 tarval_b_true->mode = mode_b;
1519 tarval_b_false->mode = mode_b;
1520 tarval_P_void->mode = mode_P;
1523 * assign output modes that are compatible with the
1524 * old implementation: Hex output
1526 tarval_set_mode_output_option(mode_U, &hex_output);
1527 tarval_set_mode_output_option(mode_C, &hex_output);
1528 tarval_set_mode_output_option(mode_Bs, &hex_output);
1529 tarval_set_mode_output_option(mode_Bu, &hex_output);
1530 tarval_set_mode_output_option(mode_Hs, &hex_output);
1531 tarval_set_mode_output_option(mode_Hu, &hex_output);
1532 tarval_set_mode_output_option(mode_Is, &hex_output);
1533 tarval_set_mode_output_option(mode_Iu, &hex_output);
1534 tarval_set_mode_output_option(mode_Ls, &hex_output);
1535 tarval_set_mode_output_option(mode_Lu, &hex_output);
1536 tarval_set_mode_output_option(mode_P, &reference_output);
1539 /****************************************************************************
1541 ****************************************************************************/