1 /* TV --- Target Values, aka Constant Table.
2 Copyright (C) 1995, 1996 Christian von Roques */
6 /* This implementation assumes:
7 * target characters/strings can be represented as type `char'/`char *',
8 * host's type `long'/`unsigned long' can hold values of mode `l'/`L',
9 * both host and target have two's complement integral arithmetic,
10 host's C operators `/' and `%' match target's div and mod.
11 target_max_<mode> == (1<<k)-1 for some k>0
12 target_min_<mode> == -target_max_<mode>-1
13 target_max_<Mode> == target_max_<mode>-target_min_<mode>
14 * both host and target have IEEE-754 floating-point arithmetic. */
16 /* !!! float and double divides MUST NOT SIGNAL !!! */
17 /* @@@ query the floating-point expception status flags */
19 /* @@@ ToDo: tarval_convert_to is not fully implemented! */
20 /* @@@ Problem: All Values are stored twice, once as Univ_*s and a 2nd
21 time in their real target mode. :-( */
22 /* @@@ Perhaps use a set instead of a pset: new tarvals allocated on
23 stack, copied into set by tarval_identify() if really new. If
24 tarval_identify() discards often enough, the extra copy for kept
25 values is cheaper than the extra obstack_alloc()/free() for
28 /* Defining this causes inclusions of functions renamed with new gmp.h */
29 #define _TARVAL_GMP_ 0
45 #define TOBSTACK_ID "tv"
54 static struct obstack tv_obst; /* obstack for all the target values */
55 static pset *tarvals; /* pset containing pointers to _all_ tarvals */
57 /* currently building an object with tarval_start() & friends ? */
58 #define BUILDING obstack_object_size (&tv_obst)
60 /* bcopy is not ISO C */
61 #define bcopy(X, Y, Z) memcpy((Y), (X), (Z))
64 /* special tarvals: */
66 tarval *tarval_b_false;
67 tarval *tarval_b_true;
70 tarval *tarval_p_void;
71 tarval *tarval_mode_null[irm_max];
74 /* @@@ depends on order of ir_mode */
75 static tarval_chil min_chil[8] = {
81 static tarval_chil max_chil[8] = {
82 TARGET_SIMAX (c), TARGET_UIMAX (C),
83 TARGET_SIMAX (h), TARGET_UIMAX (H),
84 TARGET_SIMAX (i), TARGET_UIMAX (I),
85 TARGET_SIMAX (l), TARGET_UIMAX (L)
89 /* return a mode-specific value */
104 tv_val_chil (tarval *tv)
110 tv_val_CHIL (tarval *tv)
116 tv_val_Z (tarval *tv)
122 tv_val_p (tarval *tv)
128 tv_val_b (tarval *tv)
134 tv_val_B (tarval *tv)
140 tv_val_s (tarval *tv)
146 /* Overflows `chil' signed integral `mode'? */
148 chil_overflow (tarval_chil chil, ir_mode *mode)
150 assert (is_chilCHIL(get_mode_modecode(mode)));
151 return (get_mode_min(mode) && get_mode_max(mode) /* only valid after firm initialization */
152 && (chil < tv_val_chil (get_mode_min(mode))
153 || tv_val_chil (get_mode_max(mode)) < chil));
157 /* Overflows `CHIL' unsigned integral `mode'? */
159 CHIL_overflow (tarval_CHIL CHIL, ir_mode *mode)
161 assert (is_chilCHIL(get_mode_modecode(mode)));
162 return (get_mode_max(mode) /* only valid after firm initialization */
163 && tv_val_CHIL (get_mode_max(mode)) < CHIL);
169 _tarval_vrfy (const tarval *val)
172 switch (get_mode_modecode(val->mode)) {
178 case irm_C: case irm_H: case irm_I: case irm_L:
179 assert (!CHIL_overflow (val->u.CHIL, val->mode)); break;
180 case irm_c: case irm_h: case irm_i: case irm_l:
181 assert (!chil_overflow (val->u.chil, val->mode)); break;
187 assert (val->u.p.ent->kind == k_entity);
188 assert ( val->u.p.xname || val->u.p.ent
189 || !tarval_p_void || (val == tarval_p_void));
193 assert (val->u.s.p); break;
195 assert (val->u.B.p); break;
197 assert ((unsigned)val->u.b <= 1); break;
199 assert (val->mode == mode_T);
210 pset_stats (tarvals);
216 /* Return the canonical tarval * for tv.
217 May destroy everything allocated on tv_obst after tv! */
219 tarval_identify (tarval *tv)
223 o = pset_insert (tarvals, tv, tarval_hash (tv));
226 obstack_free (&tv_obst, (void *)tv);
234 /* Return 0 iff a equals b. Bitwise identical NaNs compare equal. */
236 tarval_cmp (const void *p, const void *q)
244 if (a == b) return 0;
245 if ((void *)a->mode - (void *)b->mode)
246 return (void *)a->mode - (void *)b->mode;
248 switch (get_mode_modecode(a->mode)) {
251 return memcmp (&a->u.f, &b->u.f, sizeof (a->u.f));
253 return memcmp (&a->u.d, &b->u.d, sizeof (a->u.d));
255 case irm_C: case irm_H: case irm_I: case irm_L:
256 if (sizeof (int) == sizeof (tarval_CHIL)) {
257 return a->u.CHIL - b->u.CHIL;
259 return a->u.CHIL != b->u.CHIL;
261 case irm_c: case irm_h: case irm_i: case irm_l:
262 if (sizeof (int) == sizeof (tarval_chil)) {
263 return a->u.chil - b->u.chil;
265 return a->u.chil != b->u.chil;
268 return mpz_cmp (&a->u.Z, &b->u.Z);
274 if (a->u.p.ent || b->u.p.ent)
275 return (char *)a->u.p.ent - (char *)b->u.p.ent;
276 if (a->u.p.xname && b->u.p.xname)
277 return strcmp (a->u.p.xname, b->u.p.xname);
278 return a->u.p.xname - b->u.p.xname;
280 return a->u.b - b->u.b;
282 return ( a->u.B.n - b->u.B.n
283 ? memcmp (a->u.B.p, b->u.B.p, a->u.B.n)
284 : a->u.B.n - b->u.B.n);
285 case irm_s: case irm_S:
286 return ( a->u.s.n == b->u.s.n
287 ? memcmp (a->u.s.p, b->u.s.p, a->u.s.n)
288 : a->u.s.n - b->u.s.n);
295 tarval_hash (tarval *tv)
299 h = get_mode_modecode(tv->mode) * 0x421u;
300 switch (get_mode_modecode(tv->mode)) {
302 h = 0x94b527ce; break;
305 { union { float f; unsigned u; } u;
306 assert (sizeof (float) <= sizeof (unsigned));
307 u.u = 0; u.f = tv->u.f;
313 { union { double d; unsigned u[2]; } u;
314 assert (sizeof (double) <= 2*sizeof (unsigned));
315 u.u[0] = u.u[1] = 0; u.d = tv->u.d;
316 h ^= u.u[0] ^ u.u[1];
319 case irm_C: case irm_H: case irm_I: case irm_L:
320 h ^= tv->u.CHIL; break;
321 case irm_c: case irm_h: case irm_i: case irm_l:
322 h ^= tv->u.chil; break;
325 h ^= mpz_get_ui (&tv->u.Z); break;
327 h ^= (unsigned int) tv; break; /* tut das? */
331 /* @@@ lower bits not random, watch for collisions; perhaps
332 replace by tv->u.p.ent - (entity *)0 */
333 h ^= ((char *)tv->u.p.ent - (char *)0) / 64;
334 } else if (tv->u.p.xname) {
335 /* Of course, strlen() in a hash function is a mistake, but this
336 case should be really rare. */
337 h ^= ID_HASH (tv->u.p.xname, strlen (tv->u.p.xname));
345 h ^= tv->u.B.n; break; /* @@@ not really good */
347 h ^= tv->u.s.p[0]<<12 ^ tv->u.s.p[tv->u.s.n]<<4 ^ tv->u.s.n; break;
349 h ^= tv->u.s.p[0]<<4 ^ tv->u.s.p[tv->u.s.n]<<12 ^ tv->u.s.n; break;
358 /*** ***************** Initialization ************************************* ***/
363 obstack_init (&tv_obst);
364 obstack_alignment_mask (&tv_obst) = ALIGNOF (tarval) - 1;
365 assert (IS_POW2 (ALIGNOF (tarval)));
367 /* initialize the target value table */
368 tarvals = new_pset (tarval_cmp, TUNE_NCONSTANTS);
375 union ieee754_double x;
377 /* assumed by tarval_hash(): */
378 assert (sizeof (float) * CHAR_BIT == 32);
379 assert (sizeof (double) * CHAR_BIT == 64);
382 /* assumed by tarval_chil & friends: */
383 assert ( (irm_C == irm_c+1) && (irm_h == irm_C+1)
384 && (irm_H == irm_h+1) && (irm_i == irm_H+1)
385 && (irm_I == irm_i+1) && (irm_l == irm_I+1)
386 && (irm_L == irm_l+1));
388 /* assumed everywhere: */
389 for (i = 0; i <= irm_L-irm_c; i += 2) {
390 assert ( IS_POW2 (max_chil[i+1]+1)
391 && (min_chil[i] == -max_chil[i]-1)
392 && ((tarval_CHIL)max_chil[i+1] == (tarval_CHIL)max_chil[i]-min_chil[i]));
397 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
399 tarval_bad = tarval_identify (tv);
401 tarval_b_false = tarval_from_long (mode_b, 0);
402 tarval_b_true = tarval_from_long (mode_b, 1);
404 /* IsInf <-> exponent == 0x7ff && ! (bits | fraction_low) */
405 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
408 x.ieee.exponent = 0x7ff;
409 x.ieee.mantissa0 = 0;
410 x.ieee.mantissa1 = 0;
412 tarval_d_Inf = tarval_identify (tv);
414 /* IsNaN <-> exponent==0x7ff && (qnan_bit | bits | fraction_low) */
415 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
417 x.ieee_nan.negative = 0;
418 x.ieee_nan.exponent = 0x7ff;
419 x.ieee_nan.quiet_nan = 1; /* @@@ quiet or signalling? */
420 x.ieee_nan.mantissa0 = 42;
421 x.ieee_nan.mantissa1 = 0;
422 assert(x.d != x.d /* x.d is NaN */);
424 tarval_d_NaN = tarval_identify (tv);
426 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
428 tv->u.p.xname = NULL;
431 tarval_p_void = tarval_identify (tv);
433 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
436 tarval_mode_null [irm_f] = tarval_from_long (mode_f, 0);
437 tarval_mode_null [irm_d] = tarval_from_long (mode_d, 0);
438 tarval_mode_null [irm_c] = tarval_from_long (mode_c, 0);
439 tarval_mode_null [irm_C] = tarval_from_long (mode_C, 0);
440 tarval_mode_null [irm_h] = tarval_from_long (mode_h, 0);
441 tarval_mode_null [irm_H] = tarval_from_long (mode_H, 0);
442 tarval_mode_null [irm_i] = tarval_from_long (mode_i, 0);
443 tarval_mode_null [irm_I] = tarval_from_long (mode_I, 0);
444 tarval_mode_null [irm_l] = tarval_from_long (mode_l, 0);
445 tarval_mode_null [irm_L] = tarval_from_long (mode_L, 0);
446 tarval_mode_null [irm_b] = tarval_b_false;
447 tarval_mode_null [irm_p] = tarval_p_void;
452 /*** ********************** Constructors for tarvals ********************** ***/
454 /* copy from src to dst len chars omitting '_'. */
456 stripcpy (char *dst, const char *src, size_t len)
461 if (*src == '_') src++;
464 *d = 0; /* make it 0-terminated. */
471 tarval_Z_from_str (const char *s, size_t len, int base)
478 buf = alloca (len+1);
479 stripcpy (buf, s, len);
481 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
484 if (mpz_init_set_str (&tv->u.Z, buf, base)) assert (0);
486 assert(0 && "no support for Z in tv!");
489 return tarval_identify (tv);
494 tarval_B_from_str (const char *s, size_t len)
497 size_t n; /* size of B */
498 const char *r; /* read ptr */
499 unsigned x; /* bit store */
500 int b; /* bits in x */
501 int shift; /* base shift */
506 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
509 assert (s[0] == '0');
512 case 'O': shift = 3; break;
514 case 'X': shift = 4; break;
518 r = s+len; /* set r past input */
519 s += 2; /* skip header */
524 if (*r == '_') continue; /* skip _ styropor */
525 if (('0' <= *r) && (*r <= '9')) {
527 } else if (('a' <= *r) && (*r <= 'f')) {
529 } else { assert (('A' <= *r) && (*r <= 'F'));
533 x |= d << b; /* insert d into x above the b present bits */
534 b += shift; /* x now contains shift more bits */
536 if (b >= 8) { /* we've accumulated at least a byte */
537 char c = x & 0xFF; /* extract the lower 8 bits from x */
538 obstack_grow (&tv_obst, &c, 1); /* and stuff them into B */
539 x >>= 8; /* remove the lower 8 bits from x */
540 b -= 8; /* x now contains 8 bits fewer */
541 ++n; /* B grew a byte */
545 if (b >= 0) { /* flush the rest of the bits */
546 char c = x; /* extract them */
547 obstack_grow (&tv_obst, &c, 1); /* and stuff them into B */
548 ++n; /* B grew a byte */
551 { unsigned char *p = obstack_finish (&tv_obst);
552 unsigned char *q = p + n;
556 /* reverse p in place */
557 while (p < q) { char c = *p; *p++ = *q; *q-- = c; }
560 return tarval_identify (tv);
565 tarval_f_from_str (const char *s, size_t len)
573 buf = alloca (len+1);
574 stripcpy (buf, s, len);
576 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
578 tv->u.f = (float)strtod (buf, &eptr);
579 assert (eptr == buf+strlen(buf));
581 return tarval_identify (tv);
586 tarval_d_from_str (const char *s, size_t len)
594 buf = alloca (len+1);
595 stripcpy (buf, s, len);
597 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
599 tv->u.d = strtod (buf, &eptr);
600 assert (eptr == buf+strlen(buf));
602 return tarval_identify (tv);
607 tarval_s_from_str (const char *s, size_t len)
613 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
617 tv->u.s.p = obstack_copy (&tv_obst, s, len);
619 return tarval_identify (tv);
623 tarval_S_from_str (const char *s, size_t len)
629 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
633 tv->u.s.p = obstack_copy (&tv_obst, s, len);
635 return tarval_identify (tv);
638 tarval *tarval_int_from_str (const char *s, size_t len, int base, ir_mode *m) {
643 assert (mode_is_int(m));
646 buf = alloca (len+1);
647 stripcpy (buf, s, len);
650 val = strtol(buf, &eptr, base); /* strtoll */
651 assert (eptr == buf+strlen(buf));
652 if ((errno == ERANGE) &&
653 ((m == mode_l) || (m == mode_L)) ) {
654 printf("WARNING: Constant %s not representable. Continuing with %ld.\n",
658 return tarval_from_long(m, val);
661 /* Create a tarval with mode `m' and value `i' casted to the type that
662 represents such tarvals on host. The resulting value must be legal
665 tarval_from_long (ir_mode *m, long val)
671 if (m == mode_T) return tarval_bad;
673 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
676 switch (get_mode_modecode(m)) {
679 tv->u.f = val; break;
681 tv->u.d = val; break;
683 case irm_C: case irm_H: case irm_I: case irm_L:
684 tv->u.CHIL = val; break;
686 case irm_c: case irm_h: case irm_i: case irm_l:
687 tv->u.chil = val; break;
690 mpz_init_set_si (&tv->u.Z, val);
692 assert(0 && "no support for Z in tv!");
698 obstack_free (&tv_obst, tv);
699 return tarval_p_void;
701 tv->u.b = !!val; /* u.b must be 0 or 1 */
707 return tarval_identify (tv);
712 tarval_p_from_str (const char *xname)
718 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
721 tv->u.p.xname = obstack_copy0 (&tv_obst, xname, strlen (xname));
724 return tarval_identify (tv);
729 tarval_p_from_entity (entity *ent)
735 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
738 tv->u.p.xname = NULL;
741 return tarval_identify (tv);
745 /* Routines for building a tarval step by step follow.
746 Legal calling sequences:
748 No contructors except tarval_append() and tarval_append1 ()
749 tarval_finish_as() or tarval_cancel() */
751 /* Begin building a tarval. */
756 obstack_blank (&tv_obst, sizeof (tarval));
760 /* Append `n' chars from `p' to the tarval currently under construction. */
762 tarval_append (const char *p, size_t n)
765 obstack_grow (&tv_obst, p, n);
769 /* Append `ch' to the tarval currently under construction. */
771 tarval_append1 (char ch)
774 obstack_1grow (&tv_obst, ch);
778 /* Finish the tarval currently under construction and give id mode `m'.
779 `m' must be irm_C, irm_B, irm_s or irm_S.
780 Return NULL if the value does not make sense for this mode, this
781 can only happen in mode C. */
783 tarval_finish_as (ir_mode *m)
785 int size = obstack_object_size (&tv_obst) - sizeof (tarval);
788 char ch = 0; /* initialized to shut up gcc */
790 assert (BUILDING && (size >= 0));
792 if (size != 1) return tarval_cancel();
793 p = (unsigned char *)obstack_base (&tv_obst) + sizeof (tarval);
795 obstack_blank (&tv_obst, -size);
797 tv = obstack_finish (&tv_obst);
798 p = (unsigned char *)tv + sizeof (tarval);
801 switch (get_mode_modecode(m)) {
821 return tarval_identify (tv);
825 /* Cancel tarval building and return tarval_bad. */
830 obstack_free (&tv_obst, obstack_finish (&tv_obst));
836 /*** ****************** Arithmethic operations on tarvals ***************** ***/
838 /* Return `src' converted to mode `m' if representable, else NULL.
839 @@@ lots of conversions missing */
841 tarval_convert_to (tarval *src, ir_mode *m)
845 if (m == src->mode) return src;
847 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
850 switch (get_mode_modecode(src->mode)) {
853 if (m != mode_f) goto fail;
859 switch (get_mode_modecode(m)) {
861 case irm_C: case irm_H: case irm_I: case irm_L:
862 if (mpz_cmp_si (&src->u.Z, 0) < 0) goto fail;
863 if (mpz_size (&src->u.Z) > 1) goto fail;
864 tv->u.CHIL = mpz_get_ui (&src->u.Z);
865 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
868 case irm_c: case irm_h: case irm_i: case irm_l:
869 tv->u.chil = mpz_get_si (&src->u.Z);
870 if (chil_overflow (tv->u.chil, m)) goto fail;
874 tv ->u.b = !mpz_cmp_ui (&src->u.Z, 0);
878 if (mpz_cmp_ui (&src->u.Z, 0)) goto fail;
879 obstack_free (&tv_obst, tv);
880 return tarval_p_void;
889 case irm_c: case irm_h: case irm_i: case irm_l:
890 switch (get_mode_modecode(m)) {
891 case irm_c: case irm_h: case irm_i: case irm_l:
892 tv->u.chil = src->u.chil;
893 if (chil_overflow (tv->u.chil, m)) goto fail;
896 case irm_C: case irm_H: case irm_I: case irm_L:
897 tv->u.CHIL = src->u.chil;
898 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
903 mpz_init_set_si (&tv->u.Z, src->u.chil);
910 tv->u.b = !!src->u.chil;
916 case irm_C: case irm_H: case irm_I: case irm_L:
917 switch (get_mode_modecode(m)) {
918 case irm_c: case irm_h: case irm_i: case irm_l:
919 tv->u.chil = src->u.CHIL;
920 if (chil_overflow (tv->u.chil, m)) goto fail;
923 case irm_C: case irm_H: case irm_I: case irm_L:
924 tv->u.CHIL = src->u.CHIL;
925 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
930 mpz_init_set_ui (&tv->u.Z, src->u.CHIL);
937 tv->u.b = !!src->u.CHIL;
945 switch (get_mode_modecode(m)) {
946 case irm_c: case irm_h: case irm_i: case irm_l:
947 tv->u.chil = src->u.b;
950 case irm_C: case irm_H: case irm_I: case irm_L:
951 tv->u.CHIL = src->u.b;
959 obstack_free (&tv_obst, tv);
963 return tarval_identify (tv);
967 /* GL Why are there no ArmRoq comments, why is this not used? */
969 tarval_neg (tarval *a)
975 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
979 switch (get_mode_modecode(a->mode)) {
981 case irm_f: tv->u.f = -a->u.f; break;
982 case irm_d: tv->u.d = -a->u.d; break;
984 case irm_C: case irm_H: case irm_I: case irm_L:
985 tv->u.CHIL = -a->u.CHIL & tv_val_CHIL (get_mode_max(a->mode));
988 case irm_c: case irm_h: case irm_i: case irm_l:
989 tv->u.chil = -a->u.chil;
990 if ( chil_overflow (tv->u.chil, a->mode)
991 || ((tv->u.chil >= 0) == (a->u.chil >= 0))) {
992 obstack_free (&tv_obst, tv);
999 mpz_neg (&tv->u.Z, &a->u.Z);
1001 obstack_free (&tv_obst, tv);
1003 printf("\nWrong negation\n\n");
1007 case irm_b: tv->u.b = !a->u.b; break;
1011 return tarval_identify (tv);
1015 /* Compare `a' with `b'.
1016 Return one of irpn_Lt, irpn_Eq, irpn_Gt, irpn_Uo, or irpn_False if
1017 result is unknown. */
1019 tarval_comp (tarval *a, tarval *b)
1025 assert (a->mode == b->mode);
1027 switch (get_mode_modecode(a->mode)) {
1029 case irm_f: return ( a->u.f == b->u.f ? irpn_Eq
1030 : a->u.f > b->u.f ? irpn_Gt
1031 : a->u.f < b->u.f ? irpn_Lt
1033 case irm_d: return ( a->u.d == b->u.d ? irpn_Eq
1034 : a->u.d > b->u.d ? irpn_Gt
1035 : a->u.d < b->u.d ? irpn_Lt
1038 case irm_C: case irm_H: case irm_I: case irm_L:
1039 return ( a->u.CHIL == b->u.CHIL ? irpn_Eq
1040 : a->u.CHIL > b->u.CHIL ? irpn_Gt
1043 case irm_c: case irm_h: case irm_i: case irm_l:
1044 return ( a->u.chil == b->u.chil ? irpn_Eq
1045 : a->u.chil > b->u.chil ? irpn_Gt
1050 int cmp = mpz_cmp (&a->u.Z, &b->u.Z);
1051 return ( cmp == 0 ? irpn_Eq
1059 case irm_b: return ( a->u.b == b->u.b ? irpn_Eq
1060 : a->u.b > b->u.b ? irpn_Gt
1062 /* The following assumes that pointers are unsigned, which is valid
1063 for all sane CPUs (transputers are insane). */
1064 case irm_p: return ( a == b ? irpn_Eq
1065 : a == tarval_p_void ? irpn_Lt
1066 : b == tarval_p_void ? irpn_Gt
1067 : irpn_False); /* unknown */
1068 default: assert (0);
1073 /* Return `a+b' if computable, else NULL. Modes must be equal. */
1075 tarval_add (tarval *a, tarval *b)
1079 TARVAL_VRFY (a); TARVAL_VRFY (b);
1080 assert (a->mode == b->mode);
1082 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1086 switch (get_mode_modecode(a->mode)) {
1088 case irm_f: tv->u.f = a->u.f + b->u.f; break; /* @@@ overflow etc */
1089 case irm_d: tv->u.d = a->u.d + b->u.d; break; /* @@@ dto. */
1091 case irm_C: case irm_H: case irm_I: case irm_L:
1092 tv->u.CHIL = (a->u.CHIL + b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1095 case irm_c: case irm_h: case irm_i: case irm_l:
1096 tv->u.chil = a->u.chil + b->u.chil;
1097 if ( chil_overflow (tv->u.chil, a->mode)
1098 || ((tv->u.chil > a->u.chil) ^ (b->u.chil > 0))) {
1099 obstack_free (&tv_obst, tv);
1105 mpz_init (&tv->u.Z);
1106 mpz_add (&tv->u.Z, &a->u.Z, &b->u.Z);
1108 obstack_free (&tv_obst, tv);
1113 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1117 return tarval_identify (tv);
1121 /* Return `a-b' if computable, else NULL. Modes must be equal. */
1123 tarval_sub (tarval *a, tarval *b)
1127 TARVAL_VRFY (a); TARVAL_VRFY (b);
1128 assert (a->mode == b->mode);
1130 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1134 switch (get_mode_modecode(a->mode)) {
1136 case irm_f: tv->u.f = a->u.f - b->u.f; break; /* @@@ overflow etc */
1137 case irm_d: tv->u.d = a->u.d - b->u.d; break; /* @@@ dto. */
1139 case irm_C: case irm_H: case irm_I: case irm_L:
1140 tv->u.CHIL = (a->u.CHIL - b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1143 case irm_c: case irm_h: case irm_i: case irm_l:
1144 tv->u.chil = a->u.chil - b->u.chil;
1145 if ( chil_overflow (tv->u.chil, a->mode)
1146 || ((tv->u.chil > a->u.chil) ^ (b->u.chil < 0))) {
1147 obstack_free (&tv_obst, tv);
1153 mpz_init (&tv->u.Z);
1154 mpz_sub (&tv->u.Z, &a->u.Z, &b->u.Z);
1156 obstack_free (&tv_obst, tv);
1161 case irm_b: tv->u.b = a->u.b & ~b->u.b; break; /* u.b is in canonical form */
1165 return tarval_identify (tv);
1168 /* Return `a*b' if computable, else NULL. Modes must be equal. */
1170 tarval_mul (tarval *a, tarval *b)
1174 TARVAL_VRFY (a); TARVAL_VRFY (b);
1175 assert (a->mode == b->mode);
1177 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1181 switch (get_mode_modecode(a->mode)) {
1183 case irm_f: tv->u.f = a->u.f * b->u.f; break; /* @@@ overflow etc */
1184 case irm_d: tv->u.d = a->u.d * b->u.d; break; /* @@@ dto. */
1186 case irm_C: case irm_H: case irm_I: case irm_L:
1187 tv->u.CHIL = (a->u.CHIL * b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1190 case irm_c: case irm_h: case irm_i: case irm_l:
1191 tv->u.chil = a->u.chil * b->u.chil;
1192 if ( chil_overflow (tv->u.chil, a->mode)
1193 || (b->u.chil && (tv->u.chil / b->u.chil != a->u.chil))) {
1194 obstack_free (&tv_obst, tv);
1200 mpz_init (&tv->u.Z);
1201 mpz_mul (&tv->u.Z, &a->u.Z, &b->u.Z);
1203 obstack_free (&tv_obst, tv);
1208 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1212 return tarval_identify (tv);
1216 /* Return floating-point `a/b' if computable, else NULL.
1217 Modes must be equal, non-floating-point operands are converted to irm_d. */
1219 tarval_quo (tarval *a, tarval *b)
1223 TARVAL_VRFY (a); TARVAL_VRFY (b);
1224 assert (a->mode == b->mode);
1226 switch (get_mode_modecode(a->mode)) {
1229 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1231 tv->u.f = a->u.f / b->u.f; /* @@@ overflow etc */
1234 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1236 tv->u.d = a->u.d / b->u.d; /* @@@ overflow etc */
1239 a = tarval_convert_to (a, mode_d);
1240 b = tarval_convert_to (b, mode_d);
1241 return a && b ? tarval_quo (a, b) : NULL;
1244 return tarval_identify (tv);
1248 /* Return `a/b' if computable, else NULL. Modes must be equal. */
1250 tarval_div (tarval *a, tarval *b)
1254 TARVAL_VRFY (a); TARVAL_VRFY (b);
1255 assert (a->mode == b->mode);
1257 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1261 switch (get_mode_modecode(a->mode)) {
1263 case irm_f: tv->u.f = floor (a->u.f / b->u.f); break; /* @@@ overflow etc */
1264 case irm_d: tv->u.d = floor (a->u.d / b->u.d); break; /* @@@ dto. */
1266 case irm_C: case irm_H: case irm_I: case irm_L:
1267 if (!b->u.CHIL) goto fail;
1268 tv->u.CHIL = a->u.CHIL / b->u.CHIL;
1271 case irm_c: case irm_h: case irm_i: case irm_l:
1273 || ((b->u.chil == -1) && (a->u.chil == tv_val_chil (get_mode_max(a->mode)) ))) {
1275 obstack_free (&tv_obst, tv);
1278 tv->u.chil = a->u.chil / b->u.chil;
1282 if (!mpz_cmp_ui (&b->u.Z, 0)) goto fail;
1283 mpz_init (&tv->u.Z);
1284 mpz_div (&tv->u.Z, &a->u.Z, &b->u.Z);
1290 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1294 return tarval_identify (tv);
1298 /* Return `a%b' if computable, else NULL. Modes must be equal. */
1300 tarval_mod (tarval *a, tarval *b)
1304 TARVAL_VRFY (a); TARVAL_VRFY (b);
1305 assert (a->mode == b->mode);
1307 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1311 switch (get_mode_modecode(a->mode)) {
1313 case irm_f: tv->u.f = fmod (a->u.f, b->u.f); break; /* @@@ overflow etc */
1314 case irm_d: tv->u.d = fmod (a->u.d, b->u.d); break; /* @@@ dto */
1316 case irm_C: case irm_H: case irm_I: case irm_L:
1317 if (!b->u.CHIL) goto fail;
1318 tv->u.CHIL = a->u.CHIL % b->u.CHIL;
1321 case irm_c: case irm_h: case irm_i: case irm_l:
1324 obstack_free (&tv_obst, tv);
1327 tv->u.chil = a->u.chil % b->u.chil;
1331 if (!mpz_cmp_ui (&b->u.Z, 0)) goto fail;
1332 mpz_init (&tv->u.Z);
1333 mpz_mod (&tv->u.Z, &a->u.Z, &b->u.Z);
1339 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1343 return tarval_identify (tv);
1346 /* Return |a| if computable, else Null. */
1347 /* is -max == min?? */
1349 tarval_abs (tarval *a) {
1351 if (tv_is_negative(a)) return tarval_neg(a);
1356 tv_is_negative(tarval *a) {
1358 switch (get_mode_modecode(a->mode)) {
1360 case irm_f: return (a->u.f<0); break;
1361 case irm_d: return (a->u.d<0); break;
1363 case irm_C: case irm_H: case irm_I: case irm_L:
1367 case irm_c: case irm_h: case irm_i: case irm_l:
1368 return (a->u.chil < 0);
1380 /* Return `a&b'. Modes must be equal. */
1382 tarval_and (tarval *a, tarval *b)
1386 TARVAL_VRFY (a); TARVAL_VRFY (b);
1387 assert (a->mode == b->mode);
1389 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1393 switch (get_mode_modecode(a->mode)) {
1395 case irm_C: case irm_H: case irm_I: case irm_L:
1396 tv->u.CHIL = a->u.CHIL & b->u.CHIL; break;
1398 case irm_c: case irm_h: case irm_i: case irm_l:
1399 tv->u.chil = a->u.chil & b->u.chil; break;
1402 mpz_init (&tv->u.Z);
1403 mpz_and (&tv->u.Z, &a->u.Z, &b->u.Z);
1409 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1413 return tarval_identify (tv);
1417 /* Return `a|b'. Modes must be equal. */
1419 tarval_or (tarval *a, tarval *b)
1423 TARVAL_VRFY (a); TARVAL_VRFY (b);
1424 assert (a->mode == b->mode);
1426 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1430 switch (get_mode_modecode(a->mode)) {
1432 case irm_C: case irm_H: case irm_I: case irm_L:
1433 tv->u.CHIL = a->u.CHIL | b->u.CHIL; break;
1435 case irm_c: case irm_h: case irm_i: case irm_l:
1436 tv->u.chil = a->u.chil | b->u.chil; break;
1439 mpz_init (&tv->u.Z);
1440 mpz_ior (&tv->u.Z, &a->u.Z, &b->u.Z);
1446 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1450 return tarval_identify (tv);
1454 /* Return `a^b'. Modes must be equal. */
1456 tarval_eor (tarval *a, tarval *b)
1460 TARVAL_VRFY (a); TARVAL_VRFY (b);
1461 assert (a->mode == b->mode);
1463 #if 1 /* see case irm_Z below */
1464 if (a->mode == mode_Z) return NULL;
1467 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1471 switch (get_mode_modecode(a->mode)) {
1473 case irm_C: case irm_H: case irm_I: case irm_L:
1474 tv->u.CHIL = a->u.CHIL ^ b->u.CHIL; break;
1476 case irm_c: case irm_h: case irm_i: case irm_l:
1477 tv->u.chil = a->u.chil ^ b->u.chil; break;
1480 /* gmp-1.3.2 declares but does not define mpz_xor() */
1481 mpz_init (&tv->u.Z);
1482 mpz_xor (&tv->u.Z, &a->u.Z, &b->u.Z);
1486 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1490 return tarval_identify (tv);
1494 /* Return `a<<b' if computable, else NULL. */
1496 tarval_shl (tarval *a, tarval *b)
1502 TARVAL_VRFY (a); TARVAL_VRFY (b);
1504 shift = tarval_ord (b, &b_is_huge);
1507 || ((shift >= get_mode_size(mode_l)*target_bits) && (a->mode != mode_Z))) {
1511 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1514 switch (get_mode_modecode(a->mode)) {
1516 case irm_C: case irm_H: case irm_I: case irm_L:
1517 tv->u.CHIL = a->u.CHIL << shift;
1520 case irm_c: case irm_h: case irm_i: case irm_l:
1521 tv->u.chil = a->u.chil << shift;
1525 mpz_init (&tv->u.Z);
1526 mpz_mul_2exp (&tv->u.Z, &a->u.Z, shift);
1531 default: assert (0);
1534 return tarval_identify (tv);
1538 /* Return `a>>b' if computable, else NULL.
1539 The interpretation of >> (sign extended or not) is implementaion
1540 dependent, i.e. this is neither shr nor shrs!! */
1542 tarval_shr (tarval *a, tarval *b)
1548 TARVAL_VRFY (a); TARVAL_VRFY (b);
1550 shift = tarval_ord (b, &b_is_huge);
1553 || ((shift >= get_mode_size(mode_l)*target_bits) && (a->mode != mode_Z))) {
1557 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1560 switch (get_mode_modecode(a->mode)) {
1562 case irm_C: case irm_H: case irm_I: case irm_L:
1563 tv->u.CHIL = a->u.CHIL >> shift;
1566 case irm_c: case irm_h: case irm_i: case irm_l:
1567 tv->u.chil = a->u.chil >> shift;
1571 mpz_init (&tv->u.Z);
1572 mpz_div_2exp (&tv->u.Z, &a->u.Z, shift);
1577 default: assert (0);
1580 return tarval_identify (tv);
1584 /* Classify `tv', which may be NULL.
1585 Return 0 if `tv' is the additive neutral element, 1 if `tv' is the
1586 multiplicative neutral element, and -1 if `tv' is the neutral
1587 element of bitwise and. */
1589 tarval_classify (tarval *tv)
1595 switch (get_mode_modecode(tv->mode)) {
1597 case irm_f: case irm_d:
1601 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_C))) - 1;
1603 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_H))) - 1;
1605 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_I))) - 1;
1607 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_L))) - 1;
1609 case irm_c: case irm_h: case irm_i: case irm_l:
1613 if (mpz_cmp_si (&tv->u.Z, 0)) return 0;
1614 else if (mpz_cmp_si (&tv->u.Z, 1)) return 1;
1615 else if (mpz_cmp_si (&tv->u.Z,-1)) return -1;
1629 tarval_s_fits (tarval *tv, long min, long max) {
1630 return (( mpz_cmp_si (&tv->u.Z, min) >= 0)
1631 && mpz_cmp_si (&tv->u.Z, max) <= 0);
1635 tarval_u_fits (tarval *tv, unsigned long max) {
1636 return (( mpz_sgn (&tv->u.Z) >= 0)
1637 && mpz_cmp_si (&tv->u.Z, max) <= 0);
1641 /* Convert `tv' into type `long', set `fail' if not representable.
1642 If `fail' gets set for an unsigned `tv', the correct result can be
1643 obtained by casting the result to `unsigned long'. */
1645 tarval_ord (tarval *tv, int *fail)
1649 switch (get_mode_modecode(tv->mode)) {
1651 case irm_C: case irm_H: case irm_I: case irm_L:
1652 *fail = tv->u.CHIL > tv_val_CHIL (get_mode_max(mode_l));
1655 case irm_c: case irm_h: case irm_i: case irm_l:
1660 *fail = ( (mpz_cmp_si (&tv->u.Z, tv_val_chil(get_mode_max(mode_l))) > 0)
1661 || (mpz_cmp_si (&tv->u.Z, tv_val_chil(get_mode_max(mode_l))) < 0));
1662 return mpz_get_si (&tv->u.Z);
1679 tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
1681 tarval *val = XP_GETARG (tarval *, 0);
1687 switch (get_mode_modecode(val->mode)) {
1689 case irm_T: /* none */
1690 printed = XPSR ("<bad>");
1693 case irm_f: /* float */
1694 sprintf (buf, "%1.9e", (float)(val->u.f));
1695 printed = XPF1R ("%s", buf);
1697 case irm_d: /* double */
1698 printed = XPF1R ("%1.30g", (double)(val->u.d));
1701 case irm_c: /* signed char */
1702 case irm_C: /* unsigned char */
1703 if ((isprint (val->u.chil)) &&
1704 (val->u.chil != '\\') && (val->u.chil != '\'')) {
1705 printed = XPF1R ("'%c'", val->u.chil);
1707 printed = XPF1R ("0x%x", (unsigned long)val->u.chil);
1711 case irm_h: case irm_i: case irm_l: /* signed num */
1712 printed = XPF1R ("%ld", (long)val->u.chil);
1714 case irm_H: case irm_I: case irm_L: /* unsigned num */
1715 printed = XPF1R ("%lu", (unsigned long)val->u.CHIL);
1718 case irm_Z: /* mp int */
1719 printed = XPF1R ("%Z", &val->u.Z);
1722 case irm_p: /* pointer */
1723 if (val->u.p.xname) {
1724 printed = XPR (val->u.p.xname);
1725 } else if (val->u.p.ent) {
1726 if (get_entity_peculiarity(val->u.p.ent) == existent)
1727 printed = XPF1R ("&(%I)", get_entity_ld_ident(val->u.p.ent));
1729 printed = XPSR ("(NULL)");
1731 assert (val == tarval_p_void);
1732 printed = XPSR ("(void)");
1736 case irm_b: /* boolean */
1737 if (val->u.b) printed = XPSR ("true");
1738 else printed = XPSR ("false");
1741 case irm_B: /* universal bits */
1742 printed = XPSR ("<@@@ some bits>");
1745 case irm_s: /* string */
1748 char *buf = alloca (val->u.s.n + 2);
1754 for (i = 0; i < val->u.s.n; ++i) {
1755 if (isprint (val->u.s.p[i])) {
1756 *bp++ = val->u.s.p[i];
1762 XPF1 ("'\\%03o'", val->u.s.p[i]);
1771 case irm_M: /* memory */
1772 case irm_R: /* region */
1782 get_tv_mode (tarval *tv)
1787 /* Returns the entity if the tv is a pointer to an entity, else
1789 entity *get_tv_entity(tarval *tv) {
1792 if (tv->mode == mode_p) {
1793 if (tv->u.p.xname) {
1796 } else if (tv->u.p.ent) {