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 */
37 #include "set.h" /* to store tarvals in */
38 //#include "tune.h" /* some constants */
39 #include "entity_t.h" /* needed to store pointers to entities */
40 #include "irmode.h" /* defines modes etc */
42 #include "irnode.h" /* defines boolean return values (pnc_number)*/
47 /** Size of hash tables. Should correspond to average number of distinct constant
49 #define N_CONSTANTS 2048
51 /* XXX hack until theres's a proper interface */
55 #define GET_OVERFLOW_MODE() BAD
57 /* unused, float to int doesn't work yet */
60 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
62 #define SWITCH_NOINFINITY 0
63 #define SWITCH_NODENORMALS 0
65 /****************************************************************************
66 * local definitions and macros
67 ****************************************************************************/
69 # define TARVAL_VERIFY(a) tarval_verify((a))
71 # define TARVAL_VERIFY(a) ((void)0)
74 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
75 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
77 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
78 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
80 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
82 static long long count = 0;
83 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
85 # define ANNOUNCE() ((void)0)
87 /****************************************************************************
89 ****************************************************************************/
90 static struct set *tarvals; /* container for tarval structs */
91 static struct set *values; /* container for values */
93 /****************************************************************************
95 ****************************************************************************/
97 static int hash_val(const void *value, unsigned int length);
98 static int hash_tv(tarval *tv);
99 static void _fail_verify(tarval *tv, const char* file, int line)
101 /* print a memory image of the tarval and throw an assertion */
103 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
105 printf("%s:%d: Invalid tarval (null)", file, line);
109 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
112 INLINE static void tarval_verify(tarval *tv)
118 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
119 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
121 if (!FIND_TARVAL(tv)) fail_verify(tv);
122 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
128 static int hash_tv(tarval *tv)
130 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
133 static int hash_val(const void *value, unsigned int length)
136 unsigned int hash = 0;
138 /* scramble the byte - array */
139 for (i = 0; i < length; i++)
141 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
142 hash += (hash << 11) ^ (hash >> 17);
148 /* finds tarval with value/mode or creates new tarval */
149 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
156 /* if there already is such a value, it is returned, else value
157 * is copied into the set */
158 tv.value = INSERT_VALUE(value, length);
162 /* if there is such a tarval, it is returned, else tv is copied
164 return (tarval *)INSERT_TARVAL(&tv);
167 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
169 switch (get_mode_sort(mode))
171 case irms_int_number:
172 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
173 switch (GET_OVERFLOW_MODE()) {
175 return get_mode_max(mode);
178 char *temp = alloca(sc_get_buffer_length());
179 char *diff = alloca(sc_get_buffer_length());
180 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
181 sc_val_from_ulong(1, temp);
182 sc_add(diff, temp, diff);
183 sc_sub(value, diff, temp);
184 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
185 sc_sub(temp, diff, temp);
186 return get_tarval(temp, length, mode);
191 return get_tarval(value, length, mode);
194 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
195 switch (GET_OVERFLOW_MODE()) {
197 return get_mode_min(mode);
200 char *temp = alloca(sc_get_buffer_length());
201 char *diff = alloca(sc_get_buffer_length());
202 sc_sub(get_mode_max(mode)->value, get_mode_min(mode)->value, diff);
203 sc_val_from_ulong(1, temp);
204 sc_add(diff, temp, diff);
205 sc_add(value, diff, temp);
206 while (sc_comp(temp, get_mode_max(mode)->value) == 1)
207 sc_add(temp, diff, temp);
208 return get_tarval(temp, length, mode);
213 return get_tarval(value, length, mode);
218 case irms_float_number:
219 if (SWITCH_NOINFINITY && fc_is_inf(value))
221 return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
224 if (SWITCH_NODENORMALS && fc_is_subnormal(value))
226 return get_mode_null(mode);
232 return get_tarval(value, length, mode);
237 * public variables declared in tv.h
239 static tarval reserved_tv[5];
241 tarval *tarval_bad = &reserved_tv[0];
242 tarval *tarval_undefined = &reserved_tv[1];
243 tarval *tarval_b_false = &reserved_tv[2];
244 tarval *tarval_b_true = &reserved_tv[3];
245 tarval *tarval_P_void = &reserved_tv[4];
248 * public functions declared in tv.h
252 * Constructors =============================================================
254 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
261 switch (get_mode_sort(mode))
263 case irms_control_flow:
269 case irms_internal_boolean:
270 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
271 if (strcasecmp(str, "true")) return tarval_b_true;
272 else if (strcasecmp(str, "false")) return tarval_b_true;
274 /* XXX This is C semantics */
275 return atoi(str) ? tarval_b_true : tarval_b_false;
277 case irms_float_number:
278 switch(get_mode_size_bits(mode)) {
280 fc_val_from_str(str, len, 8, 23, NULL);
283 fc_val_from_str(str, len, 11, 52, NULL);
286 fc_val_from_str(str, len, 15, 64, NULL);
289 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
291 case irms_int_number:
293 sc_val_from_str(str, len, NULL);
294 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
297 return get_tarval(str, len, mode);
300 assert(0); /* can't be reached, can it? */
305 * helper function, create a tarval from long
307 tarval *new_tarval_from_long(long l, ir_mode *mode)
310 assert(mode && !((get_mode_sort(mode) == irms_memory)||(get_mode_sort(mode)==irms_control_flow)||(get_mode_sort(mode)==irms_auxiliary)));
312 switch(get_mode_sort(mode))
314 case irms_internal_boolean:
315 /* XXX C semantics ! */
316 return l ? tarval_b_true : tarval_b_false ;
318 case irms_int_number:
320 sc_val_from_long(l, NULL);
321 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
323 case irms_float_number:
324 return new_tarval_from_double((long double)l, mode);
327 return l ? tarval_bad : get_tarval(NULL, 0, mode); /* null pointer or tarval_bad */
335 /* returns non-zero if can be converted to long */
336 int tarval_is_long(tarval *tv)
339 if (get_mode_sort(tv->mode) != irms_int_number) return 0;
341 if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
343 /* the value might be too big to fit in a long */
344 sc_max_from_bits(sizeof(long)<<3, 0, NULL);
345 if (sc_comp(sc_get_buffer(), tv->value) == -1)
347 /* really doesn't fit */
354 /* this might overflow the machine's long, so use only with small values */
355 long tarval_to_long(tarval* tv)
358 assert(tarval_is_long(tv) && "tarval too big to fit in long");
360 return sc_val_to_long(tv->value);
363 tarval *new_tarval_from_double(long double d, ir_mode *mode)
366 assert(mode && (get_mode_sort(mode) == irms_float_number));
368 switch (get_mode_size_bits(mode)) {
370 fc_val_from_float(d, 8, 23, NULL);
373 fc_val_from_float(d, 11, 52, NULL);
376 fc_val_from_float(d, 15, 64, NULL);
379 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
382 /* returns non-zero if can be converted to double */
383 int tarval_is_double(tarval *tv)
388 return (get_mode_sort(tv->mode) == irms_float_number);
391 long double tarval_to_double(tarval *tv)
394 assert(tarval_is_double(tv));
396 return fc_val_to_float(tv->value);
399 /* The tarval represents the address of the entity. As the address must
400 be constant the entity must have as owner the global type. */
401 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
405 assert(mode && (get_mode_sort(mode) == irms_reference));
407 return get_tarval((void *)ent, 0, mode);
409 int tarval_is_entity(tarval *tv)
413 /* tv->value == NULL means dereferencing a null pointer */
414 return ((get_mode_sort(tv->mode) == irms_reference) && (tv->value != NULL) && (tv->length == 0)
415 && (tv != tarval_P_void));
418 entity *tarval_to_entity(tarval *tv)
423 if (tarval_is_entity(tv))
424 return (entity *)tv->value;
426 assert(0 && "tarval did not represent an entity");
431 void free_tarval_entity(entity *ent) {
432 /* There can be a tarval referencing this entity. Even if the
433 tarval is not used by the code any more, it can still reference
434 the entity as tarvals live indepently of the entity referenced.
435 Further the tarval is hashed into a set. If a hash function
436 evaluation happens to collide with this tarval, we will vrfy that
437 it contains a proper entity and we will crash if the entity is
440 Unluckily, tarvals can neither be changed nor deleted, and to find
441 one, all existing reference modes have to be tried -> a facility
442 to retrieve all modes of a kind is needed. */
447 * Access routines for tarval fields ========================================
449 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
457 * Special value query functions ============================================
459 * These functions calculate and return a tarval representing the requested
461 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
462 * functions, but these are stored on initialization of the irmode module and
463 * therefore the irmode functions should be prefered to the functions below.
466 tarval *get_tarval_bad(void)
471 tarval *get_tarval_undefined(void)
474 return tarval_undefined;
476 tarval *get_tarval_b_false(void)
479 return tarval_b_false;
481 tarval *get_tarval_b_true(void)
484 return tarval_b_true;
486 tarval *get_tarval_P_void(void)
489 return tarval_P_void;
492 tarval *get_tarval_max(ir_mode *mode)
497 switch(get_mode_sort(mode))
500 case irms_control_flow:
506 case irms_internal_boolean:
507 return tarval_b_true;
509 case irms_float_number:
510 switch(get_mode_size_bits(mode))
513 fc_get_max(8, 23, NULL);
516 fc_get_max(11, 52, NULL);
519 fc_get_max(15, 64, NULL);
522 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
524 case irms_int_number:
526 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
527 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
532 tarval *get_tarval_min(ir_mode *mode)
537 switch(get_mode_sort(mode))
540 case irms_control_flow:
546 case irms_internal_boolean:
547 return tarval_b_false;
549 case irms_float_number:
550 switch(get_mode_size_bits(mode))
553 fc_get_min(8, 23, NULL);
556 fc_get_min(11, 52, NULL);
559 fc_get_min(15, 64, NULL);
562 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
564 case irms_int_number:
566 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
567 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
572 tarval *get_tarval_null(ir_mode *mode)
577 switch(get_mode_sort(mode))
579 case irms_control_flow:
582 case irms_internal_boolean:
586 case irms_float_number:
587 return new_tarval_from_double(0.0, mode);
589 case irms_int_number:
591 return new_tarval_from_long(0l, mode);
594 return tarval_P_void;
599 tarval *get_tarval_one(ir_mode *mode)
604 switch(get_mode_sort(mode))
606 case irms_control_flow:
609 case irms_internal_boolean:
614 case irms_float_number:
615 return new_tarval_from_double(1.0, mode);
617 case irms_int_number:
619 return new_tarval_from_long(1l, mode);
625 tarval *get_tarval_nan(ir_mode *mode)
630 if (get_mode_sort(mode) == irms_float_number) {
631 switch(get_mode_size_bits(mode))
634 fc_get_qnan(8, 23, NULL);
637 fc_get_qnan(11, 52, NULL);
640 fc_get_qnan(15, 64, NULL);
643 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
646 assert(0 && "tarval is not floating point");
651 tarval *get_tarval_inf(ir_mode *mode)
656 if (get_mode_sort(mode) == irms_float_number) {
657 switch(get_mode_size_bits(mode))
660 fc_get_plusinf(8, 23, NULL);
663 fc_get_plusinf(11, 52, NULL);
666 fc_get_plusinf(15, 64, NULL);
669 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
672 assert(0 && "tarval is not floating point");
678 * Arithmethic operations on tarvals ========================================
682 * test if negative number, 1 means 'yes'
684 int tarval_is_negative(tarval *a)
689 switch (get_mode_sort(a->mode))
691 case irms_int_number:
692 if (!mode_is_signed(a->mode)) return 0;
694 return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
696 case irms_float_number:
697 return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
700 assert(0 && "not implemented");
706 * test if null, 1 means 'yes'
708 int tarval_is_null(tarval *a)
710 ir_mode *m = get_tarval_mode(a);
712 return a == get_tarval_null(m);
718 pnc_number tarval_cmp(tarval *a, tarval *b)
724 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
725 if (a == tarval_undefined || b == tarval_undefined) return False;
726 if (a == b) return Eq;
727 if (get_tarval_mode(a) != get_tarval_mode(b)) return False;
729 /* Here the two tarvals are unequal and of the same mode */
730 switch (get_mode_sort(a->mode))
732 case irms_control_flow:
738 case irms_float_number:
739 switch (fc_comp(a->value, b->value)) {
741 case 0: assert(0 && "different tarvals compare equal"); return Eq;
744 default: return False;
746 case irms_int_number:
748 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
750 case irms_internal_boolean:
751 return (a == tarval_b_true)?(Gt):(Lt);
757 * convert to other mode
759 tarval *tarval_convert_to(tarval *src, ir_mode *m)
767 if (src->mode == m) return src;
769 switch (get_mode_sort(src->mode))
771 case irms_control_flow:
776 /* cast float to something */
777 case irms_float_number:
778 switch (get_mode_sort(m)) {
779 case irms_float_number:
780 switch (get_mode_size_bits(m))
783 fc_cast(src->value, 8, 23, NULL);
786 fc_cast(src->value, 11, 52, NULL);
789 fc_cast(src->value, 15, 64, NULL);
794 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
797 case irms_int_number:
798 switch (GET_FLOAT_TO_INT_MODE())
801 fc_int(src->value, NULL);
804 fc_rnd(src->value, NULL);
809 /* XXX floating point unit can't produce a value in integer
811 * an intermediate representation is needed here first. */
812 /* return get_tarval(); */
817 /* the rest can't be converted */
822 /* cast int to something */
823 case irms_int_number:
824 switch (get_mode_sort(m)) {
825 case irms_int_number:
827 return get_tarval_overflow(src->value, src->length, m);
829 case irms_internal_boolean:
830 /* XXX C semantics */
831 if (src == get_mode_null(src->mode)) return tarval_b_false;
832 else return tarval_b_true;
834 case irms_float_number:
835 /* XXX floating point unit does not understand internal integer
836 * representation, convert to string first, then create float from
838 buffer = alloca(100);
839 /* decimal string representation because hexadecimal output is
840 * interpreted unsigned by fc_val_from_str, so this is a HACK */
841 snprintf(buffer, 100, "%s",
842 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
843 switch (get_mode_size_bits(m))
846 fc_val_from_str(buffer, 0, 8, 23, NULL);
849 fc_val_from_str(buffer, 0, 11, 52, NULL);
852 fc_val_from_str(buffer, 0, 15, 64, NULL);
855 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
862 case irms_internal_boolean:
863 switch (get_mode_sort(m))
865 case irms_int_number:
866 if (src == tarval_b_true) return get_mode_one(m);
867 else return get_mode_null(m);
886 tarval *tarval_not(tarval *a)
892 assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
894 switch (get_mode_sort(a->mode))
896 case irms_int_number:
897 buffer = alloca(sc_get_buffer_length());
898 sc_not(a->value, buffer);
899 return get_tarval(buffer, a->length, a->mode);
907 * arithmetic negation
909 tarval *tarval_neg(tarval *a)
915 assert(mode_is_num(a->mode)); /* negation only for numerical values */
916 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
918 switch (get_mode_sort(a->mode))
920 case irms_int_number:
921 buffer = alloca(sc_get_buffer_length());
922 sc_neg(a->value, buffer);
923 return get_tarval_overflow(buffer, a->length, a->mode);
925 case irms_float_number:
926 fc_neg(a->value, NULL);
927 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
937 tarval *tarval_add(tarval *a, tarval *b)
944 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
946 switch (get_mode_sort(a->mode))
949 case irms_int_number:
950 /* modes of a,b are equal, so result has mode of a as this might be the character */
951 buffer = alloca(sc_get_buffer_length());
952 sc_add(a->value, b->value, buffer);
953 return get_tarval_overflow(buffer, a->length, a->mode);
955 case irms_float_number:
956 fc_add(a->value, b->value, NULL);
957 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
967 tarval *tarval_sub(tarval *a, tarval *b)
974 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == irms_character && mode_is_int(b->mode)));
976 switch (get_mode_sort(a->mode))
979 case irms_int_number:
980 /* modes of a,b are equal, so result has mode of a as this might be the character */
981 buffer = alloca(sc_get_buffer_length());
982 sc_sub(a->value, b->value, buffer);
983 return get_tarval_overflow(buffer, a->length, a->mode);
985 case irms_float_number:
986 fc_sub(a->value, b->value, NULL);
987 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
997 tarval *tarval_mul(tarval *a, tarval *b)
1004 assert((a->mode == b->mode) && mode_is_num(a->mode));
1006 switch (get_mode_sort(a->mode))
1008 case irms_int_number:
1009 /* modes of a,b are equal */
1010 buffer = alloca(sc_get_buffer_length());
1011 sc_mul(a->value, b->value, buffer);
1012 return get_tarval_overflow(buffer, a->length, a->mode);
1014 case irms_float_number:
1015 fc_mul(a->value, b->value, NULL);
1016 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1024 * floating point division
1026 tarval *tarval_quo(tarval *a, tarval *b)
1031 assert((a->mode == b->mode) && mode_is_float(a->mode));
1033 fc_div(a->value, b->value, NULL);
1034 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1039 * overflow is impossible, but look out for division by zero
1041 tarval *tarval_div(tarval *a, tarval *b)
1046 assert((a->mode == b->mode) && mode_is_int(a->mode));
1049 if (b == get_mode_null(b->mode)) return tarval_bad;
1050 /* modes of a,b are equal */
1051 sc_div(a->value, b->value, NULL);
1052 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1057 * overflow is impossible, but look out for division by zero
1059 tarval *tarval_mod(tarval *a, tarval *b)
1064 assert((a->mode == b->mode) && mode_is_int(a->mode));
1067 if (b == get_mode_null(b->mode)) return tarval_bad;
1068 /* modes of a,b are equal */
1069 sc_mod(a->value, b->value, NULL);
1070 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1076 tarval *tarval_abs(tarval *a)
1082 assert(mode_is_num(a->mode));
1084 switch (get_mode_sort(a->mode))
1086 case irms_int_number:
1087 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1089 buffer = alloca(sc_get_buffer_length());
1090 sc_neg(a->value, buffer);
1091 return get_tarval_overflow(buffer, a->length, a->mode);
1095 case irms_float_number:
1096 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1098 fc_neg(a->value, NULL);
1099 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1112 tarval *tarval_and(tarval *a, tarval *b)
1117 assert(a->mode == b->mode);
1119 switch(get_mode_sort(a->mode))
1121 case irms_internal_boolean:
1122 return (a == tarval_b_false) ? a : b;
1124 case irms_int_number:
1125 sc_and(a->value, b->value, NULL);
1126 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1129 assert(0 && "operation not defined on mode");
1137 tarval *tarval_or (tarval *a, tarval *b)
1142 assert(a->mode == b->mode);
1144 switch (get_mode_sort(a->mode))
1146 case irms_internal_boolean:
1147 return (a == tarval_b_true) ? a : b;
1149 case irms_int_number:
1150 sc_or(a->value, b->value, NULL);
1151 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1154 assert(0 && "operation not defined on mode");
1160 * bitwise exclusive or (xor)
1162 tarval *tarval_eor(tarval *a, tarval *b)
1167 assert((a->mode == b->mode));
1169 switch (get_mode_sort(a->mode))
1171 case irms_internal_boolean:
1172 return (a == b)? tarval_b_false : tarval_b_true;
1174 case irms_int_number:
1175 sc_xor(a->value, b->value, NULL);
1176 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1179 assert(0 && "operation not defined on mode");
1185 * bitwise left shift
1187 tarval *tarval_shl(tarval *a, tarval *b)
1189 char *temp_val = NULL;
1193 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1195 if (get_mode_modulo_shift(a->mode) != 0)
1197 temp_val = alloca(sc_get_buffer_length());
1199 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1200 sc_mod(b->value, temp_val, temp_val);
1203 temp_val = (char*)b->value;
1205 sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1206 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1210 * bitwise unsigned right shift
1212 tarval *tarval_shr(tarval *a, tarval *b)
1214 char *temp_val = NULL;
1218 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1220 if (get_mode_modulo_shift(a->mode) != 0)
1222 temp_val = alloca(sc_get_buffer_length());
1224 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1225 sc_mod(b->value, temp_val, temp_val);
1228 temp_val = (char*)b->value;
1230 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1231 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1235 * bitwise signed right shift
1237 tarval *tarval_shrs(tarval *a, tarval *b)
1239 char *temp_val = NULL;
1243 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1245 if (get_mode_modulo_shift(a->mode) != 0)
1247 temp_val = alloca(sc_get_buffer_length());
1249 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1250 sc_mod(b->value, temp_val, temp_val);
1253 temp_val = (char*)b->value;
1255 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1256 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1262 tarval *tarval_rot(tarval *a, tarval *b)
1264 char *temp_val = NULL;
1268 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1270 if (get_mode_modulo_shift(a->mode) != 0)
1272 temp_val = alloca(sc_get_buffer_length());
1274 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1275 sc_mod(b->value, temp_val, temp_val);
1278 temp_val = (char*)b->value;
1280 sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1281 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1288 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1290 static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1294 const tarval_mode_info *mode_info;
1295 const char *prefix, *suffix;
1299 mode_info = tv->mode->tv_priv;
1301 mode_info = &default_info;
1302 prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1303 suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1305 switch (get_mode_sort(tv->mode))
1307 case irms_int_number:
1308 case irms_character:
1309 switch (mode_info->mode_output) {
1312 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1316 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1322 str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1325 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1327 case irms_float_number:
1328 switch (mode_info->mode_output) {
1330 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1333 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1338 return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1342 case irms_reference:
1343 if (tv==tarval_P_void) return snprintf(buf, len, "NULL");
1344 if (tv->value != NULL)
1345 if (tarval_is_entity(tv)) {
1346 if (get_entity_peculiarity((entity *)tv->value) == peculiarity_existent)
1347 return snprintf(buf, len, "%s%s%s", prefix, get_entity_ld_name((entity *)tv->value), suffix);
1349 if (mode_info->mode_output == TVO_NATIVE)
1350 return snprintf(buf, len, "NULL");
1352 return snprintf(buf, len, "0");
1356 if (size > tv->length) {
1357 memcpy(buf, tv->value, tv->length);
1358 buf[tv->length] = '\0';
1362 memcpy(buf, tv->value, size-1);
1368 return snprintf(buf, len, "void");
1370 case irms_internal_boolean:
1371 switch (mode_info->mode_output) {
1377 return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1381 return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1384 case irms_control_flow:
1386 case irms_auxiliary:
1387 return snprintf(buf, len, "<BAD>");
1395 * Output of tarvals to stdio.
1397 int tarval_printf(tarval *tv) {
1401 res = tarval_snprintf(buf, sizeof(buf), tv);
1402 assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1408 char *tarval_bitpattern(tarval *tv)
1414 * access to the bitpattern
1416 unsigned char tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1418 switch (get_mode_sort(tv->mode)) {
1419 case irms_int_number:
1420 case irms_character:
1421 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1423 case irms_float_number:
1424 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1432 * Specify the output options of one mode.
1434 * This functions stores the modinfo, so DO NOT DESTROY it.
1436 * Returns zero on success.
1438 int tarval_set_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1442 mode->tv_priv = modeinfo;
1447 * Returns the output options of one mode.
1449 * This functions returns the modinfo of a given mode.
1451 const tarval_mode_info *tarval_get_mode_output_option(ir_mode *mode)
1455 return mode->tv_priv;
1459 /* Identifying some tarvals ??? */
1460 /* Implemented in old tv.c as such:
1461 * return 0 for additive neutral,
1462 * 1 for multiplicative neutral,
1463 * -1 for bitwise-and neutral
1466 * Implemented for compatibility */
1467 long tarval_classify(tarval *tv)
1470 if (!tv || tv == tarval_bad) return 2;
1472 if (tv == get_mode_null(tv->mode)) return 0;
1473 else if (tv == get_mode_one(tv->mode)) return 1;
1474 else if ((get_mode_sort(tv->mode) == irms_int_number)
1475 && (tv == new_tarval_from_long(-1, tv->mode))) return -1;
1481 * default mode_info for output as HEX
1483 static const tarval_mode_info hex_output = {
1490 * default mode_info for output as reference
1492 static const tarval_mode_info reference_output = {
1500 * Initialization of the tarval module: called before init_mode()
1502 void init_tarval_1(void)
1505 /* initialize the sets holding the tarvals with a comparison function and
1506 * an initial size, which is the expected number of constants */
1507 tarvals = new_set(memcmp, N_CONSTANTS);
1508 values = new_set(memcmp, N_CONSTANTS);
1509 /* init strcalc with precision of 68 to support floating point values with 64
1510 * bit mantissa (needs extra bits for rounding and overflow) */
1516 * Initialization of the tarval module: called after init_mode()
1518 void init_tarval_2(void)
1522 tarval_bad->mode = mode_BAD;
1523 tarval_undefined->mode = mode_ANY;
1524 tarval_b_true->mode = mode_b;
1525 tarval_b_false->mode = mode_b;
1526 tarval_P_void->mode = mode_P;
1529 * assign output modes that are compatible with the
1530 * old implementation: Hex output
1532 tarval_set_mode_output_option(mode_U, &hex_output);
1533 tarval_set_mode_output_option(mode_C, &hex_output);
1534 tarval_set_mode_output_option(mode_Bs, &hex_output);
1535 tarval_set_mode_output_option(mode_Bu, &hex_output);
1536 tarval_set_mode_output_option(mode_Hs, &hex_output);
1537 tarval_set_mode_output_option(mode_Hu, &hex_output);
1538 tarval_set_mode_output_option(mode_Is, &hex_output);
1539 tarval_set_mode_output_option(mode_Iu, &hex_output);
1540 tarval_set_mode_output_option(mode_Ls, &hex_output);
1541 tarval_set_mode_output_option(mode_Lu, &hex_output);
1542 tarval_set_mode_output_option(mode_P, &reference_output);
1545 /****************************************************************************
1547 ****************************************************************************/