1 /* TV --- Target Values, aka Constant Table.
2 Copyright (C) 1995, 1996 Christian von Roques */
6 /****i* tv/implementation
13 * Internal storage for tarvals, 1st draft:
14 * Integers as well as pointers are stored in a hex formatted string holding
15 * 16 characters. Booleans are not stored as there are only two of them.
17 * Floats are just reinterpreted as byte strings, because I am not sure if
18 * there is loss if I convert float to long double and back and furthermore
19 * the implementation of a fully ieee compatible floating point emulation
20 * is not sensible for now
21 * With this information it is easy to decide the kind of stored value:
22 * Integers have size 16, floats 4, doubles 8, long doubles 12.
25 /* This implementation assumes:
26 * both host and target have IEEE-754 floating-point arithmetic. */
28 /* !!! float and double divides MUST NOT SIGNAL !!! */
29 /* @@@ query the floating-point expception status flags */
31 /* @@@ Problem: All Values are stored twice, once as Univ_*s and a 2nd
32 time in their real target mode. :-( */
34 #define MAX_INT_LENGTH 8
35 #define CHAR_BUFFER_SIZE ((MAX_INT_LENGTH) * 2)
41 #include <assert.h> /* assertions */
42 #include <string.h> /* nice things for strings */
46 #include "set.h" /* to store tarvals in */
47 #include "tune.h" /* some constants */
48 #include "entity_t.h" /* needed to store pointers to entities */
49 #include "irmode.h" /* defines modes etc */
50 #include "irnode.h" /* defines boolean return values */
57 /****************************************************************************
58 * local definitions and macros
59 ****************************************************************************/
61 # define TARVAL_VERIFY(a) tarval_verify((a))
63 # define TARVAL_VERIFY(a) ((void)0)
66 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
67 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
69 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
70 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
72 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
74 static long long count = 0;
75 # define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
77 # define ANNOUNCE() ((void)0)
79 /****************************************************************************
81 ****************************************************************************/
82 static struct set *tarvals; /* container for tarval structs */
83 static struct set *values; /* container for values */
85 /****************************************************************************
87 ****************************************************************************/
89 static int hash_val(const void *value, unsigned int length);
90 static int hash_tv(tarval *tv);
91 static void _fail_verify(tarval *tv, const char* file, int line)
93 /* print a memory image of the tarval and throw an assertion */
95 printf("%s:%d: Invalid tarval:\n mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
97 printf("%s:%d: Invalid tarval (null)", file, line);
101 static void tarval_verify(tarval *tv)
107 if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
108 if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
110 if (!FIND_TARVAL(tv)) fail_verify(tv);
111 if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
117 static int hash_tv(tarval *tv)
119 return ((unsigned int)tv->value ^ (unsigned int)tv->mode) + tv->length;
122 static int hash_val(const void *value, unsigned int length)
125 unsigned int hash = 0;
127 /* scramble the byte - array */
128 for (i = 0; i < length; i++)
130 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
131 hash += (hash << 11) ^ (hash >> 17);
137 /* finds tarval with value/mode or creates new tarval*/
138 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
145 /* if there already is such a value, it is returned, else value
146 * is copied into the set */
147 tv.value = INSERT_VALUE(value, length);
151 /* if there is such a tarval, it is returned, else tv is copied
153 return (tarval *)INSERT_TARVAL(&tv);
156 static int overflows(tarval *tv)
158 switch (get_mode_sort(tv->mode))
162 if (sc_comp(tv->value, get_mode_max(tv->mode)->value) == 1) return 1;
163 if (sc_comp(tv->value, get_mode_min(tv->mode)->value) == -1) return 1;
167 if (fc_comp(tv->value, get_mode_max(tv->mode)->value) == 1) return 1;
168 if (fc_comp(tv->value, get_mode_min(tv->mode)->value) == -1) return 1;
178 /****************************************************************************
179 * public variables declared in tv.h
180 ****************************************************************************/
182 tarval *tarval_undefined;
183 tarval *tarval_b_false;
184 tarval *tarval_b_true;
185 tarval *tarval_P_void;
187 /****************************************************************************
188 * public functions declared in tv.h
189 ****************************************************************************/
191 * Constructors =============================================================
193 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
200 switch (get_mode_sort(mode))
206 case internal_boolean:
207 /* match tTrRuUeE/fFaAlLsSeE */
208 if (strcmp(str, "true")) return tarval_b_true;
209 else return tarval_b_false;
212 fc_val_from_str(str, len);
213 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
217 sc_val_from_str(str, len);
218 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
221 return get_tarval(str, len, mode);
224 assert(0); /* can't be reached, can it? */
228 int tarval_is_str(tarval *tv)
233 return ((get_mode_sort(tv->mode) == reference) && (tv->value != NULL) && (tv->length > 0));
235 char *tarval_to_str(tarval *tv)
238 assert(tarval_is_str(tv));
239 return (char *)tv->value;
243 tarval *new_tarval_from_long(long l, ir_mode *mode)
246 assert(mode && !(get_mode_sort(mode) == auxiliary));
248 switch(get_mode_sort(mode))
250 case internal_boolean:
251 /* XXX C-Semantics ! */
252 return (l)?(tarval_b_true):(tarval_b_false);
257 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
260 return new_tarval_from_double((long double)l, mode);
263 return (l)?(tarval_bad):(get_tarval(NULL, 0, mode)); /* null pointer or tarval_bad */
269 int tarval_is_long(tarval *tv)
272 return ((get_mode_sort(tv->mode) == int_number) || (get_mode_sort(tv->mode) == character));
274 /* this might overflow the machine's long, so use only with
276 long tarval_to_long(tarval* tv)
279 assert(tv && get_mode_sort(tv->mode) == int_number);
281 return sc_val_to_long(tv->value); /* might overflow */
284 tarval *new_tarval_from_double(long double d, ir_mode *mode)
287 assert(mode && (get_mode_sort(mode) == float_number));
289 fc_val_from_float(d);
290 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
292 int tarval_is_double(tarval *tv)
297 return (get_mode_sort(tv->mode) == float_number);
299 long double tarval_to_double(tarval *tv)
302 assert(tarval_is_double(tv));
304 return fc_val_to_float(tv->value);
307 /* The tarval represents the address of the entity. As the address must
308 be constant the entity must have as owner the global type. */
309 tarval *new_tarval_from_entity (entity *ent, ir_mode *mode)
313 assert(mode && (get_mode_sort(mode) == reference));
315 return get_tarval((void *)ent, 0, mode);
317 int tarval_is_entity(tarval *tv)
321 /* tv->value == NULL means dereferencing a null pointer */
322 return ((get_mode_sort(tv->mode) == reference) && (tv->value != NULL) && (tv->length == 0));
324 entity *tarval_to_entity(tarval *tv)
327 assert(tarval_is_entity(tv));
329 return (entity *)tv->value;
333 * Access routines for tarval fields ========================================
335 #ifdef TARVAL_ACCESS_DEFINES
336 # undef get_tarval_mode
338 ir_mode *get_tarval_mode (tarval *tv) /* get the mode of the tarval */
344 #ifdef TARVAL_ACCESS_DEFINES
345 # define get_tarval_mode(tv) (tv)->mode
349 * Special value query functions ============================================
351 * These functions calculate and return a tarval representing the requested
353 * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
354 * functions, but these are stored on initialization of the irmode module and
355 * therefore the irmode functions should be prefered to the functions below.
358 tarval *get_tarval_bad(void)
363 tarval *get_tarval_undefined(void)
366 return tarval_undefined;
368 tarval *get_tarval_b_false(void)
371 return tarval_b_false;
373 tarval *get_tarval_b_true(void)
376 return tarval_b_true;
378 tarval *get_tarval_P_void(void)
381 return tarval_P_void;
384 tarval *get_tarval_max(ir_mode *mode)
389 switch(get_mode_sort(mode))
396 case internal_boolean:
397 return tarval_b_true;
400 fc_get_max(get_mode_size_bits(mode));
401 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
405 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode));
406 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
411 tarval *get_tarval_min(ir_mode *mode)
416 switch(get_mode_sort(mode))
423 case internal_boolean:
424 return tarval_b_false;
427 fc_get_min(get_mode_size_bits(mode));
428 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
432 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode));
433 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
438 tarval *get_tarval_null(ir_mode *mode)
443 switch(get_mode_sort(mode))
446 case internal_boolean:
451 return new_tarval_from_double(0.0, mode);
455 return new_tarval_from_long(0l, mode);
458 return tarval_P_void;
463 tarval *get_tarval_one(ir_mode *mode)
468 switch(get_mode_sort(mode))
471 case internal_boolean:
477 return new_tarval_from_double(1.0, mode);
481 return new_tarval_from_long(1l, mode);
487 tarval *get_tarval_nan(ir_mode *mode)
491 assert(get_mode_sort(mode) == float_number);
494 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
497 tarval *get_tarval_inf(ir_mode *mode)
501 assert(get_mode_sort(mode) == float_number);
504 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
508 * Arithmethic operations on tarvals ========================================
511 /* test if negative number, 1 means 'yes' */
512 int tarval_is_negative(tarval *a)
517 switch (get_mode_sort(a->mode))
520 if (!mode_is_signed(a->mode)) return 0;
521 else return sc_comp(a->value, get_mode_null(a->mode)->value);
524 return fc_comp(a->value, get_mode_null(a->mode)->value);
527 assert(0 && "not implemented");
532 pnc_number tarval_cmp(tarval *a, tarval *b)
538 if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
539 if (a == tarval_undefined || b == tarval_undefined) return False;
540 if (a == b) return Eq;
541 if (get_tarval_mode(a) != get_tarval_mode(b)) return Uo;
543 /* Here the two tarvals are unequal and of the same mode */
544 switch (get_mode_sort(a->mode))
550 return (fc_comp(a->value, b->value)==1)?(Gt):(Lt);
554 return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
556 case internal_boolean:
557 return (a == tarval_b_true)?(Gt):(Lt);
565 tarval *tarval_convert_to(tarval *src, ir_mode *m)
573 if (src->mode == m) return src;
575 switch (get_mode_sort(src->mode))
584 switch (get_mode_sort(m))
589 tv.length = src->length;
590 tv.value = src->value;
595 return INSERT_TARVAL(&tv);
597 case internal_boolean:
598 /* XXX C semantics */
599 if (src == get_mode_null(src->mode)) return tarval_b_false;
600 else return tarval_b_true;
607 case internal_boolean:
608 switch (get_mode_sort(m))
611 if (src == tarval_b_true) return get_mode_one(m);
612 else return get_mode_null(m);
628 tarval *tarval_neg(tarval *a) /* negation */
632 assert(mode_is_num(a->mode)); /* negation only for numerical values */
633 assert(mode_is_signed(a->mode)); /* negation is difficult without negative numbers, isn't it */
635 switch (get_mode_sort(a->mode))
639 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
643 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), a->mode);
650 tarval *tarval_add(tarval *a, tarval *b) /* addition */
655 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == character && mode_is_int(b->mode)));
657 switch (get_mode_sort(a->mode))
661 /* modes of a,b are equal, so result has mode of a as this might be the character */
662 sc_add(a->value, b->value);
663 /* FIXME: Check for overflow */
664 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
667 /* FIXME: Overflow/Underflow/transition to inf when mode < 80bit */
668 fc_add(a->value, b->value);
669 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), a->mode);
676 tarval *tarval_sub(tarval *a, tarval *b) /* subtraction */
681 assert((a->mode == b->mode) || (get_mode_sort(a->mode) == character && mode_is_int(b->mode)));
683 switch (get_mode_sort(a->mode))
687 /* modes of a,b are equal, so result has mode of a as this might be the character */
688 sc_sub(a->value, b->value);
689 /* FIXME: check for overflow */
690 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
693 /* FIXME: Overflow/Underflow/transition to inf when mode < 80bit */
694 fc_add(a->value, b->value);
695 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), a->mode);
702 tarval *tarval_mul(tarval *a, tarval *b) /* multiplication */
707 assert((a->mode == b->mode) && mode_is_num(a->mode));
709 switch (get_mode_sort(a->mode))
712 /* modes of a,b are equal */
713 sc_mul(a->value, b->value);
714 /* FIXME: check for overflow */
715 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
718 /* FIXME: Overflow/Underflow/transition to inf when mode < 80bit */
719 fc_add(a->value, b->value);
720 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), a->mode);
727 tarval *tarval_quo(tarval *a, tarval *b) /* floating point division */
732 assert((a->mode == b->mode) && mode_is_float(a->mode));
734 /* FIXME: Overflow/Underflow/transition to inf when mode < 80bit */
735 fc_div(a->value, b->value);
736 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), a->mode);
739 tarval *tarval_div(tarval *a, tarval *b) /* integer division */
744 assert((a->mode == b->mode) && mode_is_int(a->mode));
746 /* modes of a,b are equal */
747 sc_div(a->value, b->value);
748 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
751 tarval *tarval_mod(tarval *a, tarval *b) /* remainder */
756 assert((a->mode == b->mode) && mode_is_int(a->mode));
758 /* modes of a,b are equal */
759 sc_mod(a->value, b->value);
760 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
763 tarval *tarval_abs(tarval *a) /* absolute value */
767 assert(mode_is_num(a->mode));
769 switch (get_mode_sort(a->mode))
772 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
775 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
788 tarval *tarval_and(tarval *a, tarval *b) /* bitwise and */
793 assert((a->mode == b->mode) && mode_is_int(a->mode));
795 sc_and(a->value, b->value);
796 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
798 tarval *tarval_or (tarval *a, tarval *b) /* bitwise or */
803 assert((a->mode == b->mode) && mode_is_int(a->mode));
805 sc_or(a->value, b->value);
806 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
808 tarval *tarval_eor(tarval *a, tarval *b) /* bitwise exclusive or (xor) */
813 assert((a->mode == b->mode) && mode_is_int(a->mode));
815 sc_or(a->value, b->value);
816 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
819 tarval *tarval_shl(tarval *a, tarval *b) /* bitwise left shift */
824 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
826 sc_shl(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode));
827 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
829 tarval *tarval_shr(tarval *a, tarval *b) /* bitwise unsigned right shift */
834 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
836 sc_shr(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode));
837 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
839 tarval *tarval_shrs(tarval *a, tarval *b) /* bitwise signed right shift */
844 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
846 sc_shrs(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode));
847 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
849 tarval *tarval_rot(tarval *a, tarval *b) /* bitwise rotation */
854 assert(mode_is_int(a->mode) && mode_is_int(b->mode));
856 sc_rot(a->value, b->value, get_mode_size_bits(a->mode), mode_is_signed(a->mode));
857 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
860 /** *********** Output of tarvals *********** **/
861 int tarval_print(XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
869 tv = XP_GETARG(tarval *, 0);
870 switch (get_mode_sort(tv->mode))
874 offset = 16 - (get_mode_size_bits(tv->mode)/4);
875 str = sc_print_hex(tv->value);
876 return XPF1R("0x%s", str + offset);
879 return XPF1R("%s", fc_print_dec(tv->value, buf, sizeof(buf)));
882 if (tv->value != NULL)
883 if (tarval_is_entity(tv))
884 if (get_entity_peculiarity((entity *)tv->value) == existent)
885 return XPF1R("&(%I)", get_entity_ld_ident((entity *)tv->value));
889 return XPMR((char*)tv->value, tv->length);
893 case internal_boolean:
894 if (tv == tarval_b_true) return XPSR("true");
895 else return XPSR("false");
898 return XPSR("<BAD>");
904 char *tarval_bitpattern(tarval *tv)
909 /* Identifying some tarvals ??? */
910 /* Implemented in old tv.c as such:
911 * return 0 for additive neutral,
912 * 1 for multiplicative neutral,
913 * -1 for bitwise-and neutral
916 * Implemented for completeness */
917 long tarval_classify(tarval *tv)
920 if (!tv || tv == tarval_bad) return 2;
922 if (tv == get_mode_null(tv->mode)) return 0;
923 else if (tv == get_mode_one(tv->mode)) return 1;
924 else if ((get_mode_sort(tv->mode) == int_number)
925 && (tv == new_tarval_from_long(-1, tv->mode))) return -1;
930 /** Initialization of the tarval module **/
931 void init_tarval_1(void)
934 /* initialize the sets holding the tarvals with a comparison function and
935 * an initial size, which is the expected number of constants */
936 tarvals = new_set(memcmp, TUNE_NCONSTANTS);
937 values = new_set(memcmp, TUNE_NCONSTANTS);
940 void init_tarval_2(void)
944 tarval_bad = (tarval*)malloc(sizeof(tarval));
945 tarval_bad->mode = NULL;
947 tarval_undefined = (tarval*)malloc(sizeof(tarval));
948 tarval_undefined->mode = NULL;
950 tarval_b_true = (tarval*)malloc(sizeof(tarval));
951 tarval_b_true->mode = mode_b;
953 tarval_b_false = (tarval*)malloc(sizeof(tarval));
954 tarval_b_false->mode = mode_b;
956 tarval_P_void = (tarval*)malloc(sizeof(tarval));
957 tarval_P_void->mode = mode_P;
960 /****************************************************************************
962 ****************************************************************************/
965 free_tarval_entity(entity *ent) {
966 /* There can be a tarval referencing this entity. Even if the
967 tarval is not used by the code any more, it can still reference
968 the entity as tarvals live forever (They live on an obstack.).
969 Further the tarval is hashed into a set. If a hash function
970 evaluation happens to collide with this tarval, we will vrfy that
971 it contains a proper entity and we will crash if the entity is
972 freed. We cannot remove tarvals from the obstack but we can
973 remove the entry in the hash table. */
974 /* this will be re-implemented later */