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 /* special tarvals: */
62 tarval *tarval_b_false;
63 tarval *tarval_b_true;
66 tarval *tarval_p_void;
67 tarval *tarval_mode_null[irm_max];
70 /* @@@ depends on order of ir_mode */
71 static tarval_chil min_chil[8] = {
77 static tarval_chil max_chil[8] = {
78 TARGET_SIMAX (c), TARGET_UIMAX (C),
79 TARGET_SIMAX (h), TARGET_UIMAX (H),
80 TARGET_SIMAX (i), TARGET_UIMAX (I),
81 TARGET_SIMAX (l), TARGET_UIMAX (L)
85 /* return a mode-specific value */
100 tv_val_chil (tarval *tv)
106 tv_val_CHIL (tarval *tv)
112 tv_val_Z (tarval *tv)
118 tv_val_p (tarval *tv)
124 tv_val_b (tarval *tv)
130 tv_val_B (tarval *tv)
136 tv_val_s (tarval *tv)
142 /* Overflows `chil' signed integral `mode'? */
144 chil_overflow (tarval_chil chil, ir_mode *mode)
146 assert (is_chilCHIL(get_mode_modecode(mode)));
147 return (get_mode_min(mode) && get_mode_max(mode) /* only valid after firm initialization */
148 && (chil < tv_val_chil (get_mode_min(mode))
149 || tv_val_chil (get_mode_max(mode)) < chil));
153 /* Overflows `CHIL' unsigned integral `mode'? */
155 CHIL_overflow (tarval_CHIL CHIL, ir_mode *mode)
157 assert (is_chilCHIL(get_mode_modecode(mode)));
158 return (get_mode_max(mode) /* only valid after firm initialization */
159 && tv_val_CHIL (get_mode_max(mode)) < CHIL);
165 _tarval_vrfy (const tarval *val)
168 switch (get_mode_modecode(val->mode)) {
174 case irm_C: case irm_H: case irm_I: case irm_L:
175 assert (!CHIL_overflow (val->u.CHIL, val->mode)); break;
176 case irm_c: case irm_h: case irm_i: case irm_l:
177 assert (!chil_overflow (val->u.chil, val->mode)); break;
183 assert (val->u.p.ent->kind == k_entity);
184 assert ( val->u.p.xname || val->u.p.ent
185 || !tarval_p_void || (val == tarval_p_void));
189 assert (val->u.s.p); break;
191 assert (val->u.B.p); break;
193 assert ((unsigned)val->u.b <= 1); break;
195 assert (val->mode == mode_T);
206 pset_stats (tarvals);
212 /* Return the canonical tarval * for tv.
213 May destroy everything allocated on tv_obst after tv! */
215 tarval_identify (tarval *tv)
219 o = pset_insert (tarvals, tv, tarval_hash (tv));
222 obstack_free (&tv_obst, (void *)tv);
230 /* Return 0 iff a equals b. Bitwise identical NaNs compare equal. */
232 tarval_cmp (const void *p, const void *q)
240 if (a == b) return 0;
241 if ((void *)a->mode - (void *)b->mode)
242 return (void *)a->mode - (void *)b->mode;
244 switch (get_mode_modecode(a->mode)) {
247 return memcmp (&a->u.f, &b->u.f, sizeof (a->u.f));
249 return memcmp (&a->u.d, &b->u.d, sizeof (a->u.d));
251 case irm_C: case irm_H: case irm_I: case irm_L:
252 if (sizeof (int) == sizeof (tarval_CHIL)) {
253 return a->u.CHIL - b->u.CHIL;
255 return a->u.CHIL != b->u.CHIL;
257 case irm_c: case irm_h: case irm_i: case irm_l:
258 if (sizeof (int) == sizeof (tarval_chil)) {
259 return a->u.chil - b->u.chil;
261 return a->u.chil != b->u.chil;
264 return mpz_cmp (&a->u.Z, &b->u.Z);
270 if (a->u.p.ent || b->u.p.ent)
271 return (char *)a->u.p.ent - (char *)b->u.p.ent;
272 if (a->u.p.xname && b->u.p.xname)
273 return strcmp (a->u.p.xname, b->u.p.xname);
274 return a->u.p.xname - b->u.p.xname;
276 return a->u.b - b->u.b;
278 return ( a->u.B.n - b->u.B.n
279 ? memcmp (a->u.B.p, b->u.B.p, a->u.B.n)
280 : a->u.B.n - b->u.B.n);
281 case irm_s: case irm_S:
282 return ( a->u.s.n == b->u.s.n
283 ? memcmp (a->u.s.p, b->u.s.p, a->u.s.n)
284 : a->u.s.n - b->u.s.n);
291 tarval_hash (tarval *tv)
295 h = get_mode_modecode(tv->mode) * 0x421u;
296 switch (get_mode_modecode(tv->mode)) {
298 h = 0x94b527ce; break;
301 { union { float f; unsigned u; } u;
302 assert (sizeof (float) <= sizeof (unsigned));
303 u.u = 0; u.f = tv->u.f;
309 { union { double d; unsigned u[2]; } u;
310 assert (sizeof (double) <= 2*sizeof (unsigned));
311 u.u[0] = u.u[1] = 0; u.d = tv->u.d;
312 h ^= u.u[0] ^ u.u[1];
315 case irm_C: case irm_H: case irm_I: case irm_L:
316 h ^= tv->u.CHIL; break;
317 case irm_c: case irm_h: case irm_i: case irm_l:
318 h ^= tv->u.chil; break;
321 h ^= mpz_get_ui (&tv->u.Z); break;
323 h ^= (unsigned int) tv; break; /* tut das? */
327 /* @@@ lower bits not random, watch for collisions; perhaps
328 replace by tv->u.p.ent - (entity *)0 */
329 h ^= ((char *)tv->u.p.ent - (char *)0) / 64;
330 } else if (tv->u.p.xname) {
331 /* Of course, strlen() in a hash function is a mistake, but this
332 case should be really rare. */
333 h ^= ID_HASH (tv->u.p.xname, strlen (tv->u.p.xname));
341 h ^= tv->u.B.n; break; /* @@@ not really good */
343 h ^= tv->u.s.p[0]<<12 ^ tv->u.s.p[tv->u.s.n]<<4 ^ tv->u.s.n; break;
345 h ^= tv->u.s.p[0]<<4 ^ tv->u.s.p[tv->u.s.n]<<12 ^ tv->u.s.n; break;
354 /*** ***************** Initialization ************************************* ***/
359 obstack_init (&tv_obst);
360 obstack_alignment_mask (&tv_obst) = ALIGNOF (tarval) - 1;
361 assert (IS_POW2 (ALIGNOF (tarval)));
363 /* initialize the target value table */
364 tarvals = new_pset (tarval_cmp, TUNE_NCONSTANTS);
371 union ieee754_double x;
373 /* assumed by tarval_hash(): */
374 assert (sizeof (float) * CHAR_BIT == 32);
375 assert (sizeof (double) * CHAR_BIT == 64);
378 /* assumed by tarval_chil & friends: */
379 assert ( (irm_C == irm_c+1) && (irm_h == irm_C+1)
380 && (irm_H == irm_h+1) && (irm_i == irm_H+1)
381 && (irm_I == irm_i+1) && (irm_l == irm_I+1)
382 && (irm_L == irm_l+1));
384 /* assumed everywhere: */
385 for (i = 0; i <= irm_L-irm_c; i += 2) {
386 assert ( IS_POW2 (max_chil[i+1]+1)
387 && (min_chil[i] == -max_chil[i]-1)
388 && ((tarval_CHIL)max_chil[i+1] == (tarval_CHIL)max_chil[i]-min_chil[i]));
393 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
395 tarval_bad = tarval_identify (tv);
397 tarval_b_false = tarval_from_long (mode_b, 0);
398 tarval_b_true = tarval_from_long (mode_b, 1);
400 /* IsInf <-> exponent == 0x7ff && ! (bits | fraction_low) */
401 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
404 x.ieee.exponent = 0x7ff;
405 x.ieee.mantissa0 = 0;
406 x.ieee.mantissa1 = 0;
408 tarval_d_Inf = tarval_identify (tv);
410 /* IsNaN <-> exponent==0x7ff && (qnan_bit | bits | fraction_low) */
411 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
413 x.ieee_nan.negative = 0;
414 x.ieee_nan.exponent = 0x7ff;
415 x.ieee_nan.quiet_nan = 1; /* @@@ quiet or signalling? */
416 x.ieee_nan.mantissa0 = 42;
417 x.ieee_nan.mantissa1 = 0;
418 assert(x.d != x.d /* x.d is NaN */);
420 tarval_d_NaN = tarval_identify (tv);
422 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
424 tv->u.p.xname = NULL;
427 tarval_p_void = tarval_identify (tv);
429 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
432 tarval_mode_null [irm_f] = tarval_from_long (mode_f, 0);
433 tarval_mode_null [irm_d] = tarval_from_long (mode_d, 0);
434 tarval_mode_null [irm_c] = tarval_from_long (mode_c, 0);
435 tarval_mode_null [irm_C] = tarval_from_long (mode_C, 0);
436 tarval_mode_null [irm_h] = tarval_from_long (mode_h, 0);
437 tarval_mode_null [irm_H] = tarval_from_long (mode_H, 0);
438 tarval_mode_null [irm_i] = tarval_from_long (mode_i, 0);
439 tarval_mode_null [irm_I] = tarval_from_long (mode_I, 0);
440 tarval_mode_null [irm_l] = tarval_from_long (mode_l, 0);
441 tarval_mode_null [irm_L] = tarval_from_long (mode_L, 0);
442 tarval_mode_null [irm_b] = tarval_b_false;
443 tarval_mode_null [irm_p] = tarval_p_void;
448 /*** ********************** Constructors for tarvals ********************** ***/
450 /* copy from src to dst len chars omitting '_'. */
452 stripcpy (char *dst, const char *src, size_t len)
457 if (*src == '_') src++;
460 *d = 0; /* make it 0-terminated. */
467 tarval_Z_from_str (const char *s, size_t len, int base)
474 buf = alloca (len+1);
475 stripcpy (buf, s, len);
477 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
480 if (mpz_init_set_str (&tv->u.Z, buf, base)) assert (0);
482 assert(0 && "no support for Z in tv!");
485 return tarval_identify (tv);
490 tarval_B_from_str (const char *s, size_t len)
493 size_t n; /* size of B */
494 const char *r; /* read ptr */
495 unsigned x; /* bit store */
496 int b; /* bits in x */
497 int shift; /* base shift */
502 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
505 assert (s[0] == '0');
508 case 'O': shift = 3; break;
510 case 'X': shift = 4; break;
514 r = s+len; /* set r past input */
515 s += 2; /* skip header */
520 if (*r == '_') continue; /* skip _ styropor */
521 if (('0' <= *r) && (*r <= '9')) {
523 } else if (('a' <= *r) && (*r <= 'f')) {
525 } else { assert (('A' <= *r) && (*r <= 'F'));
529 x |= d << b; /* insert d into x above the b present bits */
530 b += shift; /* x now contains shift more bits */
532 if (b >= 8) { /* we've accumulated at least a byte */
533 char c = x & 0xFF; /* extract the lower 8 bits from x */
534 obstack_grow (&tv_obst, &c, 1); /* and stuff them into B */
535 x >>= 8; /* remove the lower 8 bits from x */
536 b -= 8; /* x now contains 8 bits fewer */
537 ++n; /* B grew a byte */
541 if (b >= 0) { /* flush the rest of the bits */
542 char c = x; /* extract them */
543 obstack_grow (&tv_obst, &c, 1); /* and stuff them into B */
544 ++n; /* B grew a byte */
547 { unsigned char *p = obstack_finish (&tv_obst);
548 unsigned char *q = p + n;
552 /* reverse p in place */
553 while (p < q) { char c = *p; *p++ = *q; *q-- = c; }
556 return tarval_identify (tv);
561 tarval_f_from_str (const char *s, size_t len)
569 buf = alloca (len+1);
570 stripcpy (buf, s, len);
572 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
574 tv->u.f = (float)strtod (buf, &eptr);
575 assert (eptr == buf+strlen(buf));
577 return tarval_identify (tv);
582 tarval_d_from_str (const char *s, size_t len)
590 buf = alloca (len+1);
591 stripcpy (buf, s, len);
593 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
595 tv->u.d = strtod (buf, &eptr);
596 assert (eptr == buf+strlen(buf));
598 return tarval_identify (tv);
603 tarval_s_from_str (const char *s, size_t len)
609 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
613 tv->u.s.p = obstack_copy (&tv_obst, s, len);
615 return tarval_identify (tv);
619 tarval_S_from_str (const char *s, size_t len)
625 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
629 tv->u.s.p = obstack_copy (&tv_obst, s, len);
631 return tarval_identify (tv);
634 tarval *tarval_int_from_str (const char *s, size_t len, int base, ir_mode *m) {
639 assert (mode_is_int(m));
642 buf = alloca (len+1);
643 stripcpy (buf, s, len);
646 val = strtol(buf, &eptr, base); /* strtoll */
647 assert (eptr == buf+strlen(buf));
648 if ((errno == ERANGE) &&
649 ((m == mode_l) || (m == mode_L)) ) {
650 printf("WARNING: Constant %s not representable. Continuing with %ld.\n",
654 return tarval_from_long(m, val);
657 /* Create a tarval with mode `m' and value `i' casted to the type that
658 represents such tarvals on host. The resulting value must be legal
661 tarval_from_long (ir_mode *m, long val)
667 if (m == mode_T) return tarval_bad;
669 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
672 switch (get_mode_modecode(m)) {
675 tv->u.f = val; break;
677 tv->u.d = val; break;
679 case irm_C: case irm_H: case irm_I: case irm_L:
680 tv->u.CHIL = val; break;
682 case irm_c: case irm_h: case irm_i: case irm_l:
683 tv->u.chil = val; break;
686 mpz_init_set_si (&tv->u.Z, val);
688 assert(0 && "no support for Z in tv!");
694 obstack_free (&tv_obst, tv);
695 return tarval_p_void;
697 tv->u.b = !!val; /* u.b must be 0 or 1 */
703 return tarval_identify (tv);
708 tarval_p_from_str (const char *xname)
714 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
717 tv->u.p.xname = obstack_copy0 (&tv_obst, xname, strlen (xname));
720 return tarval_identify (tv);
725 tarval_p_from_entity (entity *ent)
731 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
734 tv->u.p.xname = NULL;
737 return tarval_identify (tv);
741 /* Routines for building a tarval step by step follow.
742 Legal calling sequences:
744 No contructors except tarval_append() and tarval_append1 ()
745 tarval_finish_as() or tarval_cancel() */
747 /* Begin building a tarval. */
752 obstack_blank (&tv_obst, sizeof (tarval));
756 /* Append `n' chars from `p' to the tarval currently under construction. */
758 tarval_append (const char *p, size_t n)
761 obstack_grow (&tv_obst, p, n);
765 /* Append `ch' to the tarval currently under construction. */
767 tarval_append1 (char ch)
770 obstack_1grow (&tv_obst, ch);
774 /* Finish the tarval currently under construction and give id mode `m'.
775 `m' must be irm_C, irm_B, irm_s or irm_S.
776 Return NULL if the value does not make sense for this mode, this
777 can only happen in mode C. */
779 tarval_finish_as (ir_mode *m)
781 int size = obstack_object_size (&tv_obst) - sizeof (tarval);
784 char ch = 0; /* initialized to shut up gcc */
786 assert (BUILDING && (size >= 0));
788 if (size != 1) return tarval_cancel();
789 p = (unsigned char *)obstack_base (&tv_obst) + sizeof (tarval);
791 obstack_blank (&tv_obst, -size);
793 tv = obstack_finish (&tv_obst);
794 p = (unsigned char *)tv + sizeof (tarval);
797 switch (get_mode_modecode(m)) {
817 return tarval_identify (tv);
821 /* Cancel tarval building and return tarval_bad. */
826 obstack_free (&tv_obst, obstack_finish (&tv_obst));
832 /*** ****************** Arithmethic operations on tarvals ***************** ***/
834 /* Return `src' converted to mode `m' if representable, else NULL.
835 @@@ lots of conversions missing */
837 tarval_convert_to (tarval *src, ir_mode *m)
841 if (m == src->mode) return src;
843 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
846 switch (get_mode_modecode(src->mode)) {
849 if (m != mode_f) goto fail;
855 switch (get_mode_modecode(m)) {
857 case irm_C: case irm_H: case irm_I: case irm_L:
858 if (mpz_cmp_si (&src->u.Z, 0) < 0) goto fail;
859 if (mpz_size (&src->u.Z) > 1) goto fail;
860 tv->u.CHIL = mpz_get_ui (&src->u.Z);
861 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
864 case irm_c: case irm_h: case irm_i: case irm_l:
865 tv->u.chil = mpz_get_si (&src->u.Z);
866 if (chil_overflow (tv->u.chil, m)) goto fail;
870 tv ->u.b = !mpz_cmp_ui (&src->u.Z, 0);
874 if (mpz_cmp_ui (&src->u.Z, 0)) goto fail;
875 obstack_free (&tv_obst, tv);
876 return tarval_p_void;
885 case irm_c: case irm_h: case irm_i: case irm_l:
886 switch (get_mode_modecode(m)) {
887 case irm_c: case irm_h: case irm_i: case irm_l:
888 tv->u.chil = src->u.chil;
889 if (chil_overflow (tv->u.chil, m)) goto fail;
892 case irm_C: case irm_H: case irm_I: case irm_L:
893 tv->u.CHIL = src->u.chil;
894 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
899 mpz_init_set_si (&tv->u.Z, src->u.chil);
906 tv->u.b = !!src->u.chil;
912 case irm_C: case irm_H: case irm_I: case irm_L:
913 switch (get_mode_modecode(m)) {
914 case irm_c: case irm_h: case irm_i: case irm_l:
915 tv->u.chil = src->u.CHIL;
916 if (chil_overflow (tv->u.chil, m)) goto fail;
919 case irm_C: case irm_H: case irm_I: case irm_L:
920 tv->u.CHIL = src->u.CHIL;
921 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
926 mpz_init_set_ui (&tv->u.Z, src->u.CHIL);
933 tv->u.b = !!src->u.CHIL;
941 switch (get_mode_modecode(m)) {
942 case irm_c: case irm_h: case irm_i: case irm_l:
943 tv->u.chil = src->u.b;
946 case irm_C: case irm_H: case irm_I: case irm_L:
947 tv->u.CHIL = src->u.b;
955 obstack_free (&tv_obst, tv);
959 return tarval_identify (tv);
963 /* GL Why are there no ArmRoq comments, why is this not used? */
965 tarval_neg (tarval *a)
971 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
975 switch (get_mode_modecode(a->mode)) {
977 case irm_f: tv->u.f = -a->u.f; break;
978 case irm_d: tv->u.d = -a->u.d; break;
980 case irm_C: case irm_H: case irm_I: case irm_L:
981 tv->u.CHIL = -a->u.CHIL & tv_val_CHIL (get_mode_max(a->mode));
984 case irm_c: case irm_h: case irm_i: case irm_l:
985 tv->u.chil = -a->u.chil;
986 if ( chil_overflow (tv->u.chil, a->mode)
987 || ((tv->u.chil >= 0) == (a->u.chil >= 0))) {
988 obstack_free (&tv_obst, tv);
995 mpz_neg (&tv->u.Z, &a->u.Z);
997 obstack_free (&tv_obst, tv);
999 printf("\nWrong negation\n\n");
1003 case irm_b: tv->u.b = !a->u.b; break;
1007 return tarval_identify (tv);
1011 /* Compare `a' with `b'.
1012 Return one of irpn_Lt, irpn_Eq, irpn_Gt, irpn_Uo, or irpn_False if
1013 result is unknown. */
1015 tarval_comp (tarval *a, tarval *b)
1021 assert (a->mode == b->mode);
1023 switch (get_mode_modecode(a->mode)) {
1025 case irm_f: return ( a->u.f == b->u.f ? irpn_Eq
1026 : a->u.f > b->u.f ? irpn_Gt
1027 : a->u.f < b->u.f ? irpn_Lt
1029 case irm_d: return ( a->u.d == b->u.d ? irpn_Eq
1030 : a->u.d > b->u.d ? irpn_Gt
1031 : a->u.d < b->u.d ? irpn_Lt
1034 case irm_C: case irm_H: case irm_I: case irm_L:
1035 return ( a->u.CHIL == b->u.CHIL ? irpn_Eq
1036 : a->u.CHIL > b->u.CHIL ? irpn_Gt
1039 case irm_c: case irm_h: case irm_i: case irm_l:
1040 return ( a->u.chil == b->u.chil ? irpn_Eq
1041 : a->u.chil > b->u.chil ? irpn_Gt
1046 int cmp = mpz_cmp (&a->u.Z, &b->u.Z);
1047 return ( cmp == 0 ? irpn_Eq
1055 case irm_b: return ( a->u.b == b->u.b ? irpn_Eq
1056 : a->u.b > b->u.b ? irpn_Gt
1058 /* The following assumes that pointers are unsigned, which is valid
1059 for all sane CPUs (transputers are insane). */
1060 case irm_p: return ( a == b ? irpn_Eq
1061 : a == tarval_p_void ? irpn_Lt
1062 : b == tarval_p_void ? irpn_Gt
1063 : irpn_False); /* unknown */
1064 default: assert (0);
1069 /* Return `a+b' if computable, else NULL. Modes must be equal. */
1071 tarval_add (tarval *a, tarval *b)
1075 TARVAL_VRFY (a); TARVAL_VRFY (b);
1076 assert (a->mode == b->mode);
1078 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1082 switch (get_mode_modecode(a->mode)) {
1084 case irm_f: tv->u.f = a->u.f + b->u.f; break; /* @@@ overflow etc */
1085 case irm_d: tv->u.d = a->u.d + b->u.d; break; /* @@@ dto. */
1087 case irm_C: case irm_H: case irm_I: case irm_L:
1088 tv->u.CHIL = (a->u.CHIL + b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1091 case irm_c: case irm_h: case irm_i: case irm_l:
1092 tv->u.chil = a->u.chil + b->u.chil;
1093 if ( chil_overflow (tv->u.chil, a->mode)
1094 || ((tv->u.chil > a->u.chil) ^ (b->u.chil > 0))) {
1095 obstack_free (&tv_obst, tv);
1101 mpz_init (&tv->u.Z);
1102 mpz_add (&tv->u.Z, &a->u.Z, &b->u.Z);
1104 obstack_free (&tv_obst, tv);
1109 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1113 return tarval_identify (tv);
1117 /* Return `a-b' if computable, else NULL. Modes must be equal. */
1119 tarval_sub (tarval *a, tarval *b)
1123 TARVAL_VRFY (a); TARVAL_VRFY (b);
1124 assert (a->mode == b->mode);
1126 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1130 switch (get_mode_modecode(a->mode)) {
1132 case irm_f: tv->u.f = a->u.f - b->u.f; break; /* @@@ overflow etc */
1133 case irm_d: tv->u.d = a->u.d - b->u.d; break; /* @@@ dto. */
1135 case irm_C: case irm_H: case irm_I: case irm_L:
1136 tv->u.CHIL = (a->u.CHIL - b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1139 case irm_c: case irm_h: case irm_i: case irm_l:
1140 tv->u.chil = a->u.chil - b->u.chil;
1141 if ( chil_overflow (tv->u.chil, a->mode)
1142 || ((tv->u.chil > a->u.chil) ^ (b->u.chil < 0))) {
1143 obstack_free (&tv_obst, tv);
1149 mpz_init (&tv->u.Z);
1150 mpz_sub (&tv->u.Z, &a->u.Z, &b->u.Z);
1152 obstack_free (&tv_obst, tv);
1157 case irm_b: tv->u.b = a->u.b & ~b->u.b; break; /* u.b is in canonical form */
1161 return tarval_identify (tv);
1164 /* Return `a*b' if computable, else NULL. Modes must be equal. */
1166 tarval_mul (tarval *a, tarval *b)
1170 TARVAL_VRFY (a); TARVAL_VRFY (b);
1171 assert (a->mode == b->mode);
1173 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1177 switch (get_mode_modecode(a->mode)) {
1179 case irm_f: tv->u.f = a->u.f * b->u.f; break; /* @@@ overflow etc */
1180 case irm_d: tv->u.d = a->u.d * b->u.d; break; /* @@@ dto. */
1182 case irm_C: case irm_H: case irm_I: case irm_L:
1183 tv->u.CHIL = (a->u.CHIL * b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1186 case irm_c: case irm_h: case irm_i: case irm_l:
1187 tv->u.chil = a->u.chil * b->u.chil;
1188 if ( chil_overflow (tv->u.chil, a->mode)
1189 || (b->u.chil && (tv->u.chil / b->u.chil != a->u.chil))) {
1190 obstack_free (&tv_obst, tv);
1196 mpz_init (&tv->u.Z);
1197 mpz_mul (&tv->u.Z, &a->u.Z, &b->u.Z);
1199 obstack_free (&tv_obst, tv);
1204 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1208 return tarval_identify (tv);
1212 /* Return floating-point `a/b' if computable, else NULL.
1213 Modes must be equal, non-floating-point operands are converted to irm_d. */
1215 tarval_quo (tarval *a, tarval *b)
1219 TARVAL_VRFY (a); TARVAL_VRFY (b);
1220 assert (a->mode == b->mode);
1222 switch (get_mode_modecode(a->mode)) {
1225 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1227 tv->u.f = a->u.f / b->u.f; /* @@@ overflow etc */
1230 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1232 tv->u.d = a->u.d / b->u.d; /* @@@ overflow etc */
1235 a = tarval_convert_to (a, mode_d);
1236 b = tarval_convert_to (b, mode_d);
1237 return a && b ? tarval_quo (a, b) : NULL;
1240 return tarval_identify (tv);
1244 /* Return `a/b' if computable, else NULL. Modes must be equal. */
1246 tarval_div (tarval *a, tarval *b)
1250 TARVAL_VRFY (a); TARVAL_VRFY (b);
1251 assert (a->mode == b->mode);
1253 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1257 switch (get_mode_modecode(a->mode)) {
1259 case irm_f: tv->u.f = floor (a->u.f / b->u.f); break; /* @@@ overflow etc */
1260 case irm_d: tv->u.d = floor (a->u.d / b->u.d); break; /* @@@ dto. */
1262 case irm_C: case irm_H: case irm_I: case irm_L:
1263 if (!b->u.CHIL) goto fail;
1264 tv->u.CHIL = a->u.CHIL / b->u.CHIL;
1267 case irm_c: case irm_h: case irm_i: case irm_l:
1269 || ((b->u.chil == -1) && (a->u.chil == tv_val_chil (get_mode_max(a->mode)) ))) {
1271 obstack_free (&tv_obst, tv);
1274 tv->u.chil = a->u.chil / b->u.chil;
1278 if (!mpz_cmp_ui (&b->u.Z, 0)) goto fail;
1279 mpz_init (&tv->u.Z);
1280 mpz_div (&tv->u.Z, &a->u.Z, &b->u.Z);
1286 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1290 return tarval_identify (tv);
1294 /* Return `a%b' if computable, else NULL. Modes must be equal. */
1296 tarval_mod (tarval *a, tarval *b)
1300 TARVAL_VRFY (a); TARVAL_VRFY (b);
1301 assert (a->mode == b->mode);
1303 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1307 switch (get_mode_modecode(a->mode)) {
1309 case irm_f: tv->u.f = fmod (a->u.f, b->u.f); break; /* @@@ overflow etc */
1310 case irm_d: tv->u.d = fmod (a->u.d, b->u.d); break; /* @@@ dto */
1312 case irm_C: case irm_H: case irm_I: case irm_L:
1313 if (!b->u.CHIL) goto fail;
1314 tv->u.CHIL = a->u.CHIL % b->u.CHIL;
1317 case irm_c: case irm_h: case irm_i: case irm_l:
1320 obstack_free (&tv_obst, tv);
1323 tv->u.chil = a->u.chil % b->u.chil;
1327 if (!mpz_cmp_ui (&b->u.Z, 0)) goto fail;
1328 mpz_init (&tv->u.Z);
1329 mpz_mod (&tv->u.Z, &a->u.Z, &b->u.Z);
1335 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1339 return tarval_identify (tv);
1342 /* Return |a| if computable, else Null. */
1343 /* is -max == min?? */
1345 tarval_abs (tarval *a) {
1347 if (tv_is_negative(a)) return tarval_neg(a);
1352 tv_is_negative(tarval *a) {
1354 switch (get_mode_modecode(a->mode)) {
1356 case irm_f: return (a->u.f<0); break;
1357 case irm_d: return (a->u.d<0); break;
1359 case irm_C: case irm_H: case irm_I: case irm_L:
1363 case irm_c: case irm_h: case irm_i: case irm_l:
1364 return (a->u.chil < 0);
1376 /* Return `a&b'. Modes must be equal. */
1378 tarval_and (tarval *a, tarval *b)
1382 TARVAL_VRFY (a); TARVAL_VRFY (b);
1383 assert (a->mode == b->mode);
1385 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1389 switch (get_mode_modecode(a->mode)) {
1391 case irm_C: case irm_H: case irm_I: case irm_L:
1392 tv->u.CHIL = a->u.CHIL & b->u.CHIL; break;
1394 case irm_c: case irm_h: case irm_i: case irm_l:
1395 tv->u.chil = a->u.chil & b->u.chil; break;
1398 mpz_init (&tv->u.Z);
1399 mpz_and (&tv->u.Z, &a->u.Z, &b->u.Z);
1405 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1409 return tarval_identify (tv);
1413 /* Return `a|b'. Modes must be equal. */
1415 tarval_or (tarval *a, tarval *b)
1419 TARVAL_VRFY (a); TARVAL_VRFY (b);
1420 assert (a->mode == b->mode);
1422 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1426 switch (get_mode_modecode(a->mode)) {
1428 case irm_C: case irm_H: case irm_I: case irm_L:
1429 tv->u.CHIL = a->u.CHIL | b->u.CHIL; break;
1431 case irm_c: case irm_h: case irm_i: case irm_l:
1432 tv->u.chil = a->u.chil | b->u.chil; break;
1435 mpz_init (&tv->u.Z);
1436 mpz_ior (&tv->u.Z, &a->u.Z, &b->u.Z);
1442 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1446 return tarval_identify (tv);
1450 /* Return `a^b'. Modes must be equal. */
1452 tarval_eor (tarval *a, tarval *b)
1456 TARVAL_VRFY (a); TARVAL_VRFY (b);
1457 assert (a->mode == b->mode);
1459 #if 1 /* see case irm_Z below */
1460 if (a->mode == mode_Z) return NULL;
1463 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1467 switch (get_mode_modecode(a->mode)) {
1469 case irm_C: case irm_H: case irm_I: case irm_L:
1470 tv->u.CHIL = a->u.CHIL ^ b->u.CHIL; break;
1472 case irm_c: case irm_h: case irm_i: case irm_l:
1473 tv->u.chil = a->u.chil ^ b->u.chil; break;
1476 /* gmp-1.3.2 declares but does not define mpz_xor() */
1477 mpz_init (&tv->u.Z);
1478 mpz_xor (&tv->u.Z, &a->u.Z, &b->u.Z);
1482 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1486 return tarval_identify (tv);
1490 /* Return `a<<b' if computable, else NULL. */
1492 tarval_shl (tarval *a, tarval *b)
1498 TARVAL_VRFY (a); TARVAL_VRFY (b);
1500 shift = tarval_ord (b, &b_is_huge);
1503 || ((shift >= get_mode_size(mode_l)*target_bits) && (a->mode != mode_Z))) {
1507 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1510 switch (get_mode_modecode(a->mode)) {
1512 case irm_C: case irm_H: case irm_I: case irm_L:
1513 tv->u.CHIL = a->u.CHIL << shift;
1516 case irm_c: case irm_h: case irm_i: case irm_l:
1517 tv->u.chil = a->u.chil << shift;
1521 mpz_init (&tv->u.Z);
1522 mpz_mul_2exp (&tv->u.Z, &a->u.Z, shift);
1527 default: assert (0);
1530 return tarval_identify (tv);
1534 /* Return `a>>b' if computable, else NULL.
1535 The interpretation of >> (sign extended or not) is implementaion
1536 dependent, i.e. this is neither shr nor shrs!! */
1538 tarval_shr (tarval *a, tarval *b)
1544 TARVAL_VRFY (a); TARVAL_VRFY (b);
1546 shift = tarval_ord (b, &b_is_huge);
1549 || ((shift >= get_mode_size(mode_l)*target_bits) && (a->mode != mode_Z))) {
1553 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1556 switch (get_mode_modecode(a->mode)) {
1558 case irm_C: case irm_H: case irm_I: case irm_L:
1559 tv->u.CHIL = a->u.CHIL >> shift;
1562 case irm_c: case irm_h: case irm_i: case irm_l:
1563 tv->u.chil = a->u.chil >> shift;
1567 mpz_init (&tv->u.Z);
1568 mpz_div_2exp (&tv->u.Z, &a->u.Z, shift);
1573 default: assert (0);
1576 return tarval_identify (tv);
1580 /* Classify `tv', which may be NULL.
1581 Return 0 if `tv' is the additive neutral element, 1 if `tv' is the
1582 multiplicative neutral element, and -1 if `tv' is the neutral
1583 element of bitwise and. */
1585 tarval_classify (tarval *tv)
1591 switch (get_mode_modecode(tv->mode)) {
1593 case irm_f: case irm_d:
1597 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_C))) - 1;
1599 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_H))) - 1;
1601 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_I))) - 1;
1603 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_L))) - 1;
1605 case irm_c: case irm_h: case irm_i: case irm_l:
1609 if (mpz_cmp_si (&tv->u.Z, 0)) return 0;
1610 else if (mpz_cmp_si (&tv->u.Z, 1)) return 1;
1611 else if (mpz_cmp_si (&tv->u.Z,-1)) return -1;
1625 tarval_s_fits (tarval *tv, long min, long max) {
1626 return (( mpz_cmp_si (&tv->u.Z, min) >= 0)
1627 && mpz_cmp_si (&tv->u.Z, max) <= 0);
1631 tarval_u_fits (tarval *tv, unsigned long max) {
1632 return (( mpz_sgn (&tv->u.Z) >= 0)
1633 && mpz_cmp_si (&tv->u.Z, max) <= 0);
1637 /* Convert `tv' into type `long', set `fail' if not representable.
1638 If `fail' gets set for an unsigned `tv', the correct result can be
1639 obtained by casting the result to `unsigned long'. */
1641 tarval_ord (tarval *tv, int *fail)
1645 switch (get_mode_modecode(tv->mode)) {
1647 case irm_C: case irm_H: case irm_I: case irm_L:
1648 *fail = tv->u.CHIL > tv_val_CHIL (get_mode_max(mode_l));
1651 case irm_c: case irm_h: case irm_i: case irm_l:
1656 *fail = ( (mpz_cmp_si (&tv->u.Z, tv_val_chil(get_mode_max(mode_l))) > 0)
1657 || (mpz_cmp_si (&tv->u.Z, tv_val_chil(get_mode_max(mode_l))) < 0));
1658 return mpz_get_si (&tv->u.Z);
1675 tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
1677 tarval *val = XP_GETARG (tarval *, 0);
1683 switch (get_mode_modecode(val->mode)) {
1685 case irm_T: /* none */
1686 printed = XPSR ("<bad>");
1689 case irm_f: /* float */
1690 sprintf (buf, "%1.9e", (float)(val->u.f));
1691 printed = XPF1R ("%s", buf);
1693 case irm_d: /* double */
1694 printed = XPF1R ("%1.30g", (double)(val->u.d));
1697 case irm_c: /* signed char */
1698 case irm_C: /* unsigned char */
1699 if (isprint (val->u.chil)) {
1700 printed = XPF1R ("'%c'", val->u.chil);
1702 printed = XPF1R ("0x%x", (unsigned long)val->u.chil);
1706 case irm_h: case irm_i: case irm_l: /* signed num */
1707 printed = XPF1R ("%ld", (long)val->u.chil);
1709 case irm_H: case irm_I: case irm_L: /* unsigned num */
1710 printed = XPF1R ("%lu", (unsigned long)val->u.CHIL);
1713 case irm_Z: /* mp int */
1714 printed = XPF1R ("%Z", &val->u.Z);
1717 case irm_p: /* pointer */
1718 if (val->u.p.xname) {
1719 printed = XPR (val->u.p.xname);
1720 } else if (val->u.p.ent) {
1721 if (get_entity_peculiarity(val->u.p.ent) == existent)
1722 printed = XPF1R ("&(%I)", get_entity_ld_ident(val->u.p.ent));
1724 printed = XPSR ("(NULL)");
1726 assert (val == tarval_p_void);
1727 printed = XPSR ("(void)");
1731 case irm_b: /* boolean */
1732 if (val->u.b) printed = XPSR ("true");
1733 else printed = XPSR ("false");
1736 case irm_B: /* universal bits */
1737 printed = XPSR ("<@@@ some bits>");
1740 case irm_s: /* string */
1743 char *buf = alloca (val->u.s.n + 2);
1749 for (i = 0; i < val->u.s.n; ++i) {
1750 if (isprint (val->u.s.p[i])) {
1751 *bp++ = val->u.s.p[i];
1757 XPF1 ("'\\%03o'", val->u.s.p[i]);
1766 case irm_M: /* memory */
1767 case irm_R: /* region */
1777 get_tv_mode (tarval *tv)