3 * File name: ir/tv/tv.c
4 * Purpose: Representation of and static computations on target machine
10 * Copyright: (c) 2003 Universität Karlsruhe
11 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * Values are stored in a format depending upon chosen arithmetic
16 * module. Default uses strcalc and fltcalc.
20 /* This implementation assumes:
21 * - target has IEEE-754 floating-point arithmetic. */
29 #include <assert.h> /* assertions */
30 #include <stdlib.h> /* atoi() */
31 #include <string.h> /* nice things for strings */
33 #include <strings.h> /* strings.h also includes bsd only function strcasecmp */
44 #include "set.h" /* to store tarvals in */
45 //#include "tune.h" /* some constants */
46 #include "entity_t.h" /* needed to store pointers to entities */
47 #include "irmode.h" /* defines modes etc */
49 #include "irnode.h" /* defines boolean return values (pnc_number)*/
54 /** Size of hash tables. Should correspond to average number of distinct constant
56 #define N_CONSTANTS 2048
58 /* XXX hack until theres's a proper interface */
62 #define GET_OVERFLOW_MODE() BAD
64 /* unused, float to int doesn't work yet */
67 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
69 #define SWITCH_NOINFINITY 0
70 #define SWITCH_NODENORMALS 0
72 /****************************************************************************
73 * local definitions and macros
74 ****************************************************************************/
76 # define TARVAL_VERIFY(a) tarval_verify((a))
78 # define TARVAL_VERIFY(a) ((void)0)
81 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
82 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
84 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
85 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
87 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
89 static long long count = 0;
90 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
92 # define ANNOUNCE() ((void)0)
94 /****************************************************************************
96 ****************************************************************************/
97 static struct set *tarvals; /* container for tarval structs */
98 static struct set *values; /* container for values */
100 /****************************************************************************
102 ****************************************************************************/
104 static int hash_val(const void *value, unsigned int length);
105 static int hash_tv(tarval *tv);
106 static void _fail_verify(tarval *tv, const char* file, int line)
108 /* print a memory image of the tarval and throw an assertion */
110 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
112 printf("%s:%d: Invalid tarval (null)", file, line);
116 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
119 INLINE static void tarval_verify(tarval *tv)
125 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
126 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
128 if (!FIND_TARVAL(tv)) fail_verify(tv);
129 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
135 static int hash_tv(tarval *tv)
137 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
140 static int hash_val(const void *value, unsigned int length)
143 unsigned int hash = 0;
145 /* scramble the byte - array */
146 for (i = 0; i < length; i++)
148 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
149 hash += (hash << 11) ^ (hash >> 17);
155 /* finds tarval with value/mode or creates new tarval */
156 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
163 /* if there already is such a value, it is returned, else value
164 * is copied into the set */
165 tv.value = INSERT_VALUE(value, length);
169 /* if there is such a tarval, it is returned, else tv is copied
171 return (tarval *)INSERT_TARVAL(&tv);
174 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
176 switch (get_mode_sort(mode))
178 case irms_int_number:
179 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
180 switch (GET_OVERFLOW_MODE()) {
182 return get_mode_max(mode);
185 char *temp = alloca(sc_get_buffer_length());
186 char *diff = alloca(sc_get_buffer_length());
187 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
188 sc_val_from_ulong(1, temp);
189 sc_add(diff, temp, diff);
190 sc_sub(value, diff, temp);
191 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
192 sc_sub(temp, diff, temp);
193 return get_tarval(temp, length, mode);
198 return get_tarval(value, length, mode);
201 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
202 switch (GET_OVERFLOW_MODE()) {
204 return get_mode_min(mode);
207 char *temp = alloca(sc_get_buffer_length());
208 char *diff = alloca(sc_get_buffer_length());
209 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
210 sc_val_from_ulong(1, temp);
211 sc_add(diff, temp, diff);
212 sc_add(value, diff, temp);
213 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
214 sc_add(temp, diff, temp);
215 return get_tarval(temp, length, mode);
220 return get_tarval(value, length, mode);
225 case irms_float_number:
226 if (SWITCH_NOINFINITY && fc_is_inf(value))
228 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
231 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
233 return get_mode_null(mode);
239 return get_tarval(value, length, mode);
244 * public variables declared in tv.h
246 static tarval reserved_tv[5];
248 tarval *tarval_bad = &reserved_tv[0];
249 tarval *tarval_undefined = &reserved_tv[1];
250 tarval *tarval_b_false = &reserved_tv[2];
251 tarval *tarval_b_true = &reserved_tv[3];
252 tarval *tarval_P_void = &reserved_tv[4];
255 * public functions declared in tv.h
259 * Constructors =============================================================
261 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
268 switch (get_mode_sort(mode))
270 case irms_control_flow:
276 case irms_internal_boolean:
277 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
278 if (strcasecmp(str, "true")) return tarval_b_true;
279 else if (strcasecmp(str, "false")) return tarval_b_true;
281 /* XXX This is C semantics */
282 return atoi(str) ? tarval_b_true : tarval_b_false;
284 case irms_float_number:
285 switch(get_mode_size_bits(mode)) {
287 fc_val_from_str(str, len, 8, 23, NULL);
290 fc_val_from_str(str, len, 11, 52, NULL);
293 fc_val_from_str(str, len, 15, 64, NULL);
296 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
298 case irms_int_number:
300 sc_val_from_str(str, len, NULL);
301 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
304 return get_tarval(str, len, mode);
307 assert(0); /* can't be reached, can it? */
312 * helper function, create a tarval from long
314 tarval *new_tarval_from_long(long l, ir_mode *mode)
317 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
319 switch(get_mode_sort(mode))
321 case irms_internal_boolean:
322 /* XXX C semantics ! */
323 return l ? tarval_b_true : tarval_b_false ;
325 case irms_int_number:
327 sc_val_from_long(l, NULL);
328 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
330 case irms_float_number:
331 return new_tarval_from_double((long double)l, mode);
334 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
342 /* returns non-zero if can be converted to long */
343 int tarval_is_long(tarval *tv)
346 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
348 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
350 /* the value might be too big to fit in a long */
351 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
352 if (sc_comp(sc_get_buffer(), tv->value) == -1)
354 /* really doesn't fit */
361 /* this might overflow the machine's long, so use only with small values */
362 long tarval_to_long(tarval* tv)
365 assert(tarval_is_long(tv) && "tarval too big to fit in long");
367 return sc_val_to_long(tv->value);
370 tarval *new_tarval_from_double(long double d, ir_mode *mode)
373 assert(mode && (get_mode_sort(mode) == irms_float_number));
375 switch (get_mode_size_bits(mode)) {
377 fc_val_from_float(d, 8, 23, NULL);
380 fc_val_from_float(d, 11, 52, NULL);
383 fc_val_from_float(d, 15, 64, NULL);
386 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
389 /* returns non-zero if can be converted to double */
390 int tarval_is_double(tarval *tv)
395 return (get_mode_sort(tv->mode) == irms_float_number);
398 long double tarval_to_double(tarval *tv)
401 assert(tarval_is_double(tv));
403 return fc_val_to_float(tv->value);
406 /* The tarval represents the address of the entity. As the address must
407 be constant the entity must have as owner the global type. */
408 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
412 assert(mode && (get_mode_sort(mode) == irms_reference));
414 return get_tarval((void *)ent, 0, mode);
416 int tarval_is_entity(tarval *tv)
420 /* tv->value == NULL means dereferencing a null pointer */
421 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
422 && (tv != tarval_P_void));
425 entity *tarval_to_entity(tarval *tv)
430 if (tarval_is_entity(tv))
431 return (entity *)tv->value;
433 assert(0 && "tarval did not represent an entity");
438 void free_tarval_entity(entity *ent) {
439 /* There can be a tarval referencing this entity. Even if the
440 tarval is not used by the code any more, it can still reference
441 the entity as tarvals live indepently of the entity referenced.
442 Further the tarval is hashed into a set. If a hash function
443 evaluation happens to collide with this tarval, we will vrfy that
444 it contains a proper entity and we will crash if the entity is
447 Unluckily, tarvals can neither be changed nor deleted, and to find
448 one, all existing reference modes have to be tried -> a facility
449 to retrieve all modes of a kind is needed. */
454 * Access routines for tarval fields ========================================
456 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
464 * Special value query functions ============================================
466 * These functions calculate and return a tarval representing the requested
468 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
469 * functions, but these are stored on initialization of the irmode module and
470 * therefore the irmode functions should be prefered to the functions below.
473 tarval *get_tarval_bad(void)
478 tarval *get_tarval_undefined(void)
481 return tarval_undefined;
483 tarval *get_tarval_b_false(void)
486 return tarval_b_false;
488 tarval *get_tarval_b_true(void)
491 return tarval_b_true;
493 tarval *get_tarval_P_void(void)
496 return tarval_P_void;
499 tarval *get_tarval_max(ir_mode *mode)
504 switch(get_mode_sort(mode))
507 case irms_control_flow:
513 case irms_internal_boolean:
514 return tarval_b_true;
516 case irms_float_number:
517 switch(get_mode_size_bits(mode))
520 fc_get_max(8, 23, NULL);
523 fc_get_max(11, 52, NULL);
526 fc_get_max(15, 64, NULL);
529 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
531 case irms_int_number:
533 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
534 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
539 tarval *get_tarval_min(ir_mode *mode)
544 switch(get_mode_sort(mode))
547 case irms_control_flow:
553 case irms_internal_boolean:
554 return tarval_b_false;
556 case irms_float_number:
557 switch(get_mode_size_bits(mode))
560 fc_get_min(8, 23, NULL);
563 fc_get_min(11, 52, NULL);
566 fc_get_min(15, 64, NULL);
569 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
571 case irms_int_number:
573 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
574 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
579 tarval *get_tarval_null(ir_mode *mode)
584 switch(get_mode_sort(mode))
586 case irms_control_flow:
589 case irms_internal_boolean:
593 case irms_float_number:
594 return new_tarval_from_double(0.0, mode);
596 case irms_int_number:
598 return new_tarval_from_long(0l, mode);
601 return tarval_P_void;
606 tarval *get_tarval_one(ir_mode *mode)
611 switch(get_mode_sort(mode))
613 case irms_control_flow:
616 case irms_internal_boolean:
621 case irms_float_number:
622 return new_tarval_from_double(1.0, mode);
624 case irms_int_number:
626 return new_tarval_from_long(1l, mode);
632 tarval *get_tarval_nan(ir_mode *mode)
637 if (get_mode_sort(mode) == irms_float_number) {
638 switch(get_mode_size_bits(mode))
641 fc_get_qnan(8, 23, NULL);
644 fc_get_qnan(11, 52, NULL);
647 fc_get_qnan(15, 64, NULL);
650 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
653 assert(0 && "tarval is not floating point");
658 tarval *get_tarval_inf(ir_mode *mode)
663 if (get_mode_sort(mode) == irms_float_number) {
664 switch(get_mode_size_bits(mode))
667 fc_get_plusinf(8, 23, NULL);
670 fc_get_plusinf(11, 52, NULL);
673 fc_get_plusinf(15, 64, NULL);
676 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
679 assert(0 && "tarval is not floating point");
685 * Arithmethic operations on tarvals ========================================
689 * test if negative number, 1 means 'yes'
691 int tarval_is_negative(tarval *a)
696 switch (get_mode_sort(a->mode))
698 case irms_int_number:
699 if (!mode_is_signed(a->mode)) return 0;
701 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
703 case irms_float_number:
704 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
707 assert(0 && "not implemented");
713 * test if null, 1 means 'yes'
715 int tarval_is_null(tarval *a)
717 ir_mode *m = get_tarval_mode(a);
719 return a == get_tarval_null(m);
723 * test if one, 1 means 'yes'
725 int tarval_is_one(tarval *a)
727 ir_mode *m = get_tarval_mode(a);
729 return a == get_tarval_one(m);
735 pnc_number tarval_cmp(tarval *a, tarval *b)
741 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
742 if (a == tarval_undefined || b == tarval_undefined) return False;
743 if (a == b) return Eq;
744 if (get_tarval_mode(a) != get_tarval_mode(b)) return False;
746 /* Here the two tarvals are unequal and of the same mode */
747 switch (get_mode_sort(a->mode))
749 case irms_control_flow:
755 case irms_float_number:
756 switch (fc_comp(a->value, b->value)) {
758 case 0: assert(0 && "different tarvals compare equal"); return Eq;
761 default: return False;
763 case irms_int_number:
765 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
767 case irms_internal_boolean:
768 return (a == tarval_b_true)?(Gt):(Lt);
774 * convert to other mode
776 tarval *tarval_convert_to(tarval *src, ir_mode *m)
784 if (src->mode == m) return src;
786 switch (get_mode_sort(src->mode))
788 case irms_control_flow:
793 /* cast float to something */
794 case irms_float_number:
795 switch (get_mode_sort(m)) {
796 case irms_float_number:
797 switch (get_mode_size_bits(m))
800 fc_cast(src->value, 8, 23, NULL);
803 fc_cast(src->value, 11, 52, NULL);
806 fc_cast(src->value, 15, 64, NULL);
811 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
814 case irms_int_number:
815 switch (GET_FLOAT_TO_INT_MODE())
818 fc_int(src->value, NULL);
821 fc_rnd(src->value, NULL);
826 /* XXX floating point unit can't produce a value in integer
828 * an intermediate representation is needed here first. */
829 /* return get_tarval(); */
834 /* the rest can't be converted */
839 /* cast int to something */
840 case irms_int_number:
841 switch (get_mode_sort(m)) {
842 case irms_int_number:
844 return get_tarval_overflow(src->value, src->length, m);
846 case irms_internal_boolean:
847 /* XXX C semantics */
848 if (src == get_mode_null(src->mode)) return tarval_b_false;
849 else return tarval_b_true;
851 case irms_float_number:
852 /* XXX floating point unit does not understand internal integer
853 * representation, convert to string first, then create float from
855 buffer = alloca(100);
856 /* decimal string representation because hexadecimal output is
857 * interpreted unsigned by fc_val_from_str, so this is a HACK */
858 snprintf(buffer, 100, "%s",
859 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
860 switch (get_mode_size_bits(m))
863 fc_val_from_str(buffer, 0, 8, 23, NULL);
866 fc_val_from_str(buffer, 0, 11, 52, NULL);
869 fc_val_from_str(buffer, 0, 15, 64, NULL);
872 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
879 case irms_internal_boolean:
880 switch (get_mode_sort(m))
882 case irms_int_number:
883 if (src == tarval_b_true) return get_mode_one(m);
884 else return get_mode_null(m);
903 tarval *tarval_not(tarval *a)
909 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
911 switch (get_mode_sort(a->mode))
913 case irms_int_number:
914 buffer = alloca(sc_get_buffer_length());
915 sc_not(a->value, buffer);
916 return get_tarval(buffer, a->length, a->mode);
924 * arithmetic negation
926 tarval *tarval_neg(tarval *a)
932 assert(mode_is_num(a->mode)); /* negation only for numerical values */
933 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
935 switch (get_mode_sort(a->mode))
937 case irms_int_number:
938 buffer = alloca(sc_get_buffer_length());
939 sc_neg(a->value, buffer);
940 return get_tarval_overflow(buffer, a->length, a->mode);
942 case irms_float_number:
943 fc_neg(a->value, NULL);
944 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
954 tarval *tarval_add(tarval *a, tarval *b)
961 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
963 switch (get_mode_sort(a->mode))
966 case irms_int_number:
967 /* modes of a,b are equal, so result has mode of a as this might be the character */
968 buffer = alloca(sc_get_buffer_length());
969 sc_add(a->value, b->value, buffer);
970 return get_tarval_overflow(buffer, a->length, a->mode);
972 case irms_float_number:
973 fc_add(a->value, b->value, NULL);
974 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
984 tarval *tarval_sub(tarval *a, tarval *b)
991 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
993 switch (get_mode_sort(a->mode))
996 case irms_int_number:
997 /* modes of a,b are equal, so result has mode of a as this might be the character */
998 buffer = alloca(sc_get_buffer_length());
999 sc_sub(a->value, b->value, buffer);
1000 return get_tarval_overflow(buffer, a->length, a->mode);
1002 case irms_float_number:
1003 fc_sub(a->value, b->value, NULL);
1004 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1014 tarval *tarval_mul(tarval *a, tarval *b)
1021 assert((a->mode == b->mode) && mode_is_num(a->mode));
1023 switch (get_mode_sort(a->mode))
1025 case irms_int_number:
1026 /* modes of a,b are equal */
1027 buffer = alloca(sc_get_buffer_length());
1028 sc_mul(a->value, b->value, buffer);
1029 return get_tarval_overflow(buffer, a->length, a->mode);
1031 case irms_float_number:
1032 fc_mul(a->value, b->value, NULL);
1033 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1041 * floating point division
1043 tarval *tarval_quo(tarval *a, tarval *b)
1048 assert((a->mode == b->mode) && mode_is_float(a->mode));
1050 fc_div(a->value, b->value, NULL);
1051 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1056 * overflow is impossible, but look out for division by zero
1058 tarval *tarval_div(tarval *a, tarval *b)
1063 assert((a->mode == b->mode) && mode_is_int(a->mode));
1066 if (b == get_mode_null(b->mode)) return tarval_bad;
1067 /* modes of a,b are equal */
1068 sc_div(a->value, b->value, NULL);
1069 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1074 * overflow is impossible, but look out for division by zero
1076 tarval *tarval_mod(tarval *a, tarval *b)
1081 assert((a->mode == b->mode) && mode_is_int(a->mode));
1084 if (b == get_mode_null(b->mode)) return tarval_bad;
1085 /* modes of a,b are equal */
1086 sc_mod(a->value, b->value, NULL);
1087 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1093 tarval *tarval_abs(tarval *a)
1099 assert(mode_is_num(a->mode));
1101 switch (get_mode_sort(a->mode))
1103 case irms_int_number:
1104 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1106 buffer = alloca(sc_get_buffer_length());
1107 sc_neg(a->value, buffer);
1108 return get_tarval_overflow(buffer, a->length, a->mode);
1112 case irms_float_number:
1113 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1115 fc_neg(a->value, NULL);
1116 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1129 tarval *tarval_and(tarval *a, tarval *b)
1134 assert(a->mode == b->mode);
1136 switch(get_mode_sort(a->mode))
1138 case irms_internal_boolean:
1139 return (a == tarval_b_false) ? a : b;
1141 case irms_int_number:
1142 sc_and(a->value, b->value, NULL);
1143 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1146 assert(0 && "operation not defined on mode");
1154 tarval *tarval_or (tarval *a, tarval *b)
1159 assert(a->mode == b->mode);
1161 switch (get_mode_sort(a->mode))
1163 case irms_internal_boolean:
1164 return (a == tarval_b_true) ? a : b;
1166 case irms_int_number:
1167 sc_or(a->value, b->value, NULL);
1168 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1171 assert(0 && "operation not defined on mode");
1177 * bitwise exclusive or (xor)
1179 tarval *tarval_eor(tarval *a, tarval *b)
1184 assert((a->mode == b->mode));
1186 switch (get_mode_sort(a->mode))
1188 case irms_internal_boolean:
1189 return (a == b)? tarval_b_false : tarval_b_true;
1191 case irms_int_number:
1192 sc_xor(a->value, b->value, NULL);
1193 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1196 assert(0 && "operation not defined on mode");
1202 * bitwise left shift
1204 tarval *tarval_shl(tarval *a, tarval *b)
1206 char *temp_val = NULL;
1210 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1212 if (get_mode_modulo_shift(a->mode) != 0)
1214 temp_val = alloca(sc_get_buffer_length());
1216 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1217 sc_mod(b->value, temp_val, temp_val);
1220 temp_val = (char*)b->value;
1222 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1223 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1227 * bitwise unsigned right shift
1229 tarval *tarval_shr(tarval *a, tarval *b)
1231 char *temp_val = NULL;
1235 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1237 if (get_mode_modulo_shift(a->mode) != 0)
1239 temp_val = alloca(sc_get_buffer_length());
1241 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1242 sc_mod(b->value, temp_val, temp_val);
1245 temp_val = (char*)b->value;
1247 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1248 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1252 * bitwise signed right shift
1254 tarval *tarval_shrs(tarval *a, tarval *b)
1256 char *temp_val = NULL;
1260 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1262 if (get_mode_modulo_shift(a->mode) != 0)
1264 temp_val = alloca(sc_get_buffer_length());
1266 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1267 sc_mod(b->value, temp_val, temp_val);
1270 temp_val = (char*)b->value;
1272 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1273 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1279 tarval *tarval_rot(tarval *a, tarval *b)
1281 char *temp_val = NULL;
1285 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1287 if (get_mode_modulo_shift(a->mode) != 0)
1289 temp_val = alloca(sc_get_buffer_length());
1291 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1292 sc_mod(b->value, temp_val, temp_val);
1295 temp_val = (char*)b->value;
1297 sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1298 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1305 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1307 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1311 const tarval_mode_info *mode_info;
1312 const char *prefix, *suffix;
1316 mode_info = tv->mode->tv_priv;
1318 mode_info = &default_info;
1319 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1320 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1322 switch (get_mode_sort(tv->mode))
1324 case irms_int_number:
1325 case irms_character:
1326 switch (mode_info->mode_output) {
1329 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1333 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1339 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1342 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1344 case irms_float_number:
1345 switch (mode_info->mode_output) {
1347 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1350 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1355 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1359 case irms_reference:
1360 if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1361 if (tv->value != NULL)
1362 if (tarval_is_entity(tv)) {
1363 if (get_entity_peculiarity((entity *)tv->value) == peculiarity_existent)
1364 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1366 if (mode_info->mode_output == TVO_NATIVE)
1367 return snprintf(buf, len, "NULL");
1369 return snprintf(buf, len, "0");
1373 if (size > tv->length) {
1374 memcpy(buf, tv->value, tv->length);
1375 buf[tv->length] = '\0';
1379 memcpy(buf, tv->value, size-1);
1385 return snprintf(buf, len, "void");
1387 case irms_internal_boolean:
1388 switch (mode_info->mode_output) {
1394 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1398 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1401 case irms_control_flow:
1403 case irms_auxiliary:
1404 return snprintf(buf, len, "<BAD>");
1412 * Output of tarvals to stdio.
1414 int tarval_printf(tarval *tv) {
1418 res = tarval_snprintf(buf, sizeof(buf), tv);
1419 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1425 char *tarval_bitpattern(tarval *tv)
1431 * access to the bitpattern
1433 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1435 switch (get_mode_sort(tv->mode)) {
1436 case irms_int_number:
1437 case irms_character:
1438 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1440 case irms_float_number:
1441 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1449 * Specify the output options of one mode.
1451 * This functions stores the modinfo, so DO NOT DESTROY it.
1453 * Returns zero on success.
1455 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1459 mode->tv_priv = modeinfo;
1464 * Returns the output options of one mode.
1466 * This functions returns the modinfo of a given mode.
1468 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1472 return mode->tv_priv;
1476 * Identifying tarvals values for algebraic simplifications.
1479 * - TV_CLASSIFY_NULL for additive neutral,
1480 * - TV_CLASSIFY_ONE for multiplicative neutral,
1481 * - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1482 * - TV_CLASSIFY_OTHER else
1484 tarval_classification_t tarval_classify(tarval *tv)
1487 if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1489 if (tv == get_mode_null(tv->mode))
1490 return TV_CLASSIFY_NULL;
1491 else if (tv == get_mode_one(tv->mode))
1492 return TV_CLASSIFY_ONE;
1493 else if ((get_mode_sort(tv->mode) == irms_int_number)
1494 && (tv == new_tarval_from_long(-1, tv->mode)))
1495 return TV_CLASSIFY_ALL_ONE;
1497 return TV_CLASSIFY_OTHER;
1501 * default mode_info for output as HEX
1503 static const tarval_mode_info hex_output = {
1510 * default mode_info for output as reference
1512 static const tarval_mode_info reference_output = {
1520 * Initialization of the tarval module: called before init_mode()
1522 void init_tarval_1(void)
1525 /* initialize the sets holding the tarvals with a comparison function and
1526 * an initial size, which is the expected number of constants */
1527 tarvals = new_set(memcmp, N_CONSTANTS);
1528 values = new_set(memcmp, N_CONSTANTS);
1529 /* init strcalc with precision of 68 to support floating point values with 64
1530 * bit mantissa (needs extra bits for rounding and overflow) */
1536 * Initialization of the tarval module: called after init_mode()
1538 void init_tarval_2(void)
1542 tarval_bad->mode = mode_BAD;
1543 tarval_undefined->mode = mode_ANY;
1544 tarval_b_true->mode = mode_b;
1545 tarval_b_false->mode = mode_b;
1546 tarval_P_void->mode = mode_P;
1549 * assign output modes that are compatible with the
1550 * old implementation: Hex output
1552 tarval_set_mode_output_option(mode_U, &hex_output);
1553 tarval_set_mode_output_option(mode_C, &hex_output);
1554 tarval_set_mode_output_option(mode_Bs, &hex_output);
1555 tarval_set_mode_output_option(mode_Bu, &hex_output);
1556 tarval_set_mode_output_option(mode_Hs, &hex_output);
1557 tarval_set_mode_output_option(mode_Hu, &hex_output);
1558 tarval_set_mode_output_option(mode_Is, &hex_output);
1559 tarval_set_mode_output_option(mode_Iu, &hex_output);
1560 tarval_set_mode_output_option(mode_Ls, &hex_output);
1561 tarval_set_mode_output_option(mode_Lu, &hex_output);
1562 tarval_set_mode_output_option(mode_P, &reference_output);
1565 /****************************************************************************
1567 ****************************************************************************/