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_d_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.d = strtod (buf, &eptr);
575 assert (eptr == buf+strlen(buf));
577 return tarval_identify (tv);
582 tarval_s_from_str (const char *s, size_t len)
588 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
592 tv->u.s.p = obstack_copy (&tv_obst, s, len);
594 return tarval_identify (tv);
598 tarval_S_from_str (const char *s, size_t len)
604 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
608 tv->u.s.p = obstack_copy (&tv_obst, s, len);
610 return tarval_identify (tv);
613 tarval *tarval_int_from_str (const char *s, size_t len, int base, ir_mode *m) {
618 assert (mode_is_int(m));
621 buf = alloca (len+1);
622 stripcpy (buf, s, len);
625 val = strtol(buf, &eptr, base); /* strtoll */
626 assert (eptr == buf+strlen(buf));
627 if ((errno == ERANGE) &&
628 ((m == mode_l) || (m == mode_L)) ) {
629 printf("WARNING: Constant %s not representable. Continuing with %ld.\n",
633 return tarval_from_long(m, val);
636 /* Create a tarval with mode `m' and value `i' casted to the type that
637 represents such tarvals on host. The resulting value must be legal
640 tarval_from_long (ir_mode *m, long val)
646 if (m == mode_T) return tarval_bad;
648 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
651 switch (get_mode_modecode(m)) {
654 tv->u.f = val; break;
656 tv->u.d = val; break;
658 case irm_C: case irm_H: case irm_I: case irm_L:
659 tv->u.CHIL = val; break;
661 case irm_c: case irm_h: case irm_i: case irm_l:
662 tv->u.chil = val; break;
665 mpz_init_set_si (&tv->u.Z, val);
667 assert(0 && "no support for Z in tv!");
673 obstack_free (&tv_obst, tv);
674 return tarval_p_void;
676 tv->u.b = !!val; /* u.b must be 0 or 1 */
682 return tarval_identify (tv);
687 tarval_p_from_str (const char *xname)
693 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
696 tv->u.p.xname = obstack_copy0 (&tv_obst, xname, strlen (xname));
699 return tarval_identify (tv);
704 tarval_p_from_entity (entity *ent)
710 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
713 tv->u.p.xname = NULL;
716 return tarval_identify (tv);
720 /* Routines for building a tarval step by step follow.
721 Legal calling sequences:
723 No contructors except tarval_append() and tarval_append1 ()
724 tarval_finish_as() or tarval_cancel() */
726 /* Begin building a tarval. */
731 obstack_blank (&tv_obst, sizeof (tarval));
735 /* Append `n' chars from `p' to the tarval currently under construction. */
737 tarval_append (const char *p, size_t n)
740 obstack_grow (&tv_obst, p, n);
744 /* Append `ch' to the tarval currently under construction. */
746 tarval_append1 (char ch)
749 obstack_1grow (&tv_obst, ch);
753 /* Finish the tarval currently under construction and give id mode `m'.
754 `m' must be irm_C, irm_B, irm_s or irm_S.
755 Return NULL if the value does not make sense for this mode, this
756 can only happen in mode C. */
758 tarval_finish_as (ir_mode *m)
760 int size = obstack_object_size (&tv_obst) - sizeof (tarval);
763 char ch = 0; /* initialized to shut up gcc */
765 assert (BUILDING && (size >= 0));
767 if (size != 1) return tarval_cancel();
768 p = (unsigned char *)obstack_base (&tv_obst) + sizeof (tarval);
770 obstack_blank (&tv_obst, -size);
772 tv = obstack_finish (&tv_obst);
773 p = (unsigned char *)tv + sizeof (tarval);
776 switch (get_mode_modecode(m)) {
796 return tarval_identify (tv);
800 /* Cancel tarval building and return tarval_bad. */
805 obstack_free (&tv_obst, obstack_finish (&tv_obst));
811 /*** ****************** Arithmethic operations on tarvals ***************** ***/
813 /* Return `src' converted to mode `m' if representable, else NULL.
814 @@@ lots of conversions missing */
816 tarval_convert_to (tarval *src, ir_mode *m)
820 if (m == src->mode) return src;
822 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
825 switch (get_mode_modecode(src->mode)) {
828 if (m != mode_f) goto fail;
834 switch (get_mode_modecode(m)) {
836 case irm_C: case irm_H: case irm_I: case irm_L:
837 if (mpz_cmp_si (&src->u.Z, 0) < 0) goto fail;
838 if (mpz_size (&src->u.Z) > 1) goto fail;
839 tv->u.CHIL = mpz_get_ui (&src->u.Z);
840 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
843 case irm_c: case irm_h: case irm_i: case irm_l:
844 tv->u.chil = mpz_get_si (&src->u.Z);
845 if (chil_overflow (tv->u.chil, m)) goto fail;
849 tv ->u.b = !mpz_cmp_ui (&src->u.Z, 0);
853 if (mpz_cmp_ui (&src->u.Z, 0)) goto fail;
854 obstack_free (&tv_obst, tv);
855 return tarval_p_void;
864 case irm_c: case irm_h: case irm_i: case irm_l:
865 switch (get_mode_modecode(m)) {
866 case irm_c: case irm_h: case irm_i: case irm_l:
867 tv->u.chil = src->u.chil;
868 if (chil_overflow (tv->u.chil, m)) goto fail;
871 case irm_C: case irm_H: case irm_I: case irm_L:
872 tv->u.CHIL = src->u.chil;
873 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
878 mpz_init_set_si (&tv->u.Z, src->u.chil);
885 tv->u.b = !!src->u.chil;
891 case irm_C: case irm_H: case irm_I: case irm_L:
892 switch (get_mode_modecode(m)) {
893 case irm_c: case irm_h: case irm_i: case irm_l:
894 tv->u.chil = src->u.CHIL;
895 if (chil_overflow (tv->u.chil, m)) goto fail;
898 case irm_C: case irm_H: case irm_I: case irm_L:
899 tv->u.CHIL = src->u.CHIL;
900 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
905 mpz_init_set_ui (&tv->u.Z, src->u.CHIL);
912 tv->u.b = !!src->u.CHIL;
920 switch (get_mode_modecode(m)) {
921 case irm_c: case irm_h: case irm_i: case irm_l:
922 tv->u.chil = src->u.b;
925 case irm_C: case irm_H: case irm_I: case irm_L:
926 tv->u.CHIL = src->u.b;
934 obstack_free (&tv_obst, tv);
938 return tarval_identify (tv);
942 /* GL Why are there no ArmRoq comments, why is this not used? */
944 tarval_neg (tarval *a)
950 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
954 switch (get_mode_modecode(a->mode)) {
956 case irm_f: tv->u.f = -a->u.f; break;
957 case irm_d: tv->u.d = -a->u.d; break;
959 case irm_C: case irm_H: case irm_I: case irm_L:
960 tv->u.CHIL = -a->u.CHIL & tv_val_CHIL (get_mode_max(a->mode));
963 case irm_c: case irm_h: case irm_i: case irm_l:
964 tv->u.chil = -a->u.chil;
965 if ( chil_overflow (tv->u.chil, a->mode)
966 || ((tv->u.chil >= 0) == (a->u.chil >= 0))) {
967 obstack_free (&tv_obst, tv);
974 mpz_neg (&tv->u.Z, &a->u.Z);
976 obstack_free (&tv_obst, tv);
978 printf("\nWrong negation\n\n");
982 case irm_b: tv->u.b = !a->u.b; break;
986 return tarval_identify (tv);
990 /* Compare `a' with `b'.
991 Return one of irpn_Lt, irpn_Eq, irpn_Gt, irpn_Uo, or irpn_False if
992 result is unknown. */
994 tarval_comp (tarval *a, tarval *b)
1000 assert (a->mode == b->mode);
1002 switch (get_mode_modecode(a->mode)) {
1004 case irm_f: return ( a->u.f == b->u.f ? irpn_Eq
1005 : a->u.f > b->u.f ? irpn_Gt
1006 : a->u.f < b->u.f ? irpn_Lt
1008 case irm_d: return ( a->u.d == b->u.d ? irpn_Eq
1009 : a->u.d > b->u.d ? irpn_Gt
1010 : a->u.d < b->u.d ? irpn_Lt
1013 case irm_C: case irm_H: case irm_I: case irm_L:
1014 return ( a->u.CHIL == b->u.CHIL ? irpn_Eq
1015 : a->u.CHIL > b->u.CHIL ? irpn_Gt
1018 case irm_c: case irm_h: case irm_i: case irm_l:
1019 return ( a->u.chil == b->u.chil ? irpn_Eq
1020 : a->u.chil > b->u.chil ? irpn_Gt
1025 int cmp = mpz_cmp (&a->u.Z, &b->u.Z);
1026 return ( cmp == 0 ? irpn_Eq
1034 case irm_b: return ( a->u.b == b->u.b ? irpn_Eq
1035 : a->u.b > b->u.b ? irpn_Gt
1037 /* The following assumes that pointers are unsigned, which is valid
1038 for all sane CPUs (transputers are insane). */
1039 case irm_p: return ( a == b ? irpn_Eq
1040 : a == tarval_p_void ? irpn_Lt
1041 : b == tarval_p_void ? irpn_Gt
1042 : irpn_False); /* unknown */
1043 default: assert (0);
1048 /* Return `a+b' if computable, else NULL. Modes must be equal. */
1050 tarval_add (tarval *a, tarval *b)
1054 TARVAL_VRFY (a); TARVAL_VRFY (b);
1055 assert (a->mode == b->mode);
1057 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1061 switch (get_mode_modecode(a->mode)) {
1063 case irm_f: tv->u.f = a->u.f + b->u.f; break; /* @@@ overflow etc */
1064 case irm_d: tv->u.d = a->u.d + b->u.d; break; /* @@@ dto. */
1066 case irm_C: case irm_H: case irm_I: case irm_L:
1067 tv->u.CHIL = (a->u.CHIL + b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1070 case irm_c: case irm_h: case irm_i: case irm_l:
1071 tv->u.chil = a->u.chil + b->u.chil;
1072 if ( chil_overflow (tv->u.chil, a->mode)
1073 || ((tv->u.chil > a->u.chil) ^ (b->u.chil > 0))) {
1074 obstack_free (&tv_obst, tv);
1080 mpz_init (&tv->u.Z);
1081 mpz_add (&tv->u.Z, &a->u.Z, &b->u.Z);
1083 obstack_free (&tv_obst, tv);
1088 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1092 return tarval_identify (tv);
1096 /* Return `a-b' if computable, else NULL. Modes must be equal. */
1098 tarval_sub (tarval *a, tarval *b)
1102 TARVAL_VRFY (a); TARVAL_VRFY (b);
1103 assert (a->mode == b->mode);
1105 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1109 switch (get_mode_modecode(a->mode)) {
1111 case irm_f: tv->u.f = a->u.f - b->u.f; break; /* @@@ overflow etc */
1112 case irm_d: tv->u.d = a->u.d - b->u.d; break; /* @@@ dto. */
1114 case irm_C: case irm_H: case irm_I: case irm_L:
1115 tv->u.CHIL = (a->u.CHIL - b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1118 case irm_c: case irm_h: case irm_i: case irm_l:
1119 tv->u.chil = a->u.chil - b->u.chil;
1120 if ( chil_overflow (tv->u.chil, a->mode)
1121 || ((tv->u.chil > a->u.chil) ^ (b->u.chil < 0))) {
1122 obstack_free (&tv_obst, tv);
1128 mpz_init (&tv->u.Z);
1129 mpz_sub (&tv->u.Z, &a->u.Z, &b->u.Z);
1131 obstack_free (&tv_obst, tv);
1136 case irm_b: tv->u.b = a->u.b & ~b->u.b; break; /* u.b is in canonical form */
1140 return tarval_identify (tv);
1143 /* Return `a*b' if computable, else NULL. Modes must be equal. */
1145 tarval_mul (tarval *a, tarval *b)
1149 TARVAL_VRFY (a); TARVAL_VRFY (b);
1150 assert (a->mode == b->mode);
1152 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1156 switch (get_mode_modecode(a->mode)) {
1158 case irm_f: tv->u.f = a->u.f * b->u.f; break; /* @@@ overflow etc */
1159 case irm_d: tv->u.d = a->u.d * b->u.d; break; /* @@@ dto. */
1161 case irm_C: case irm_H: case irm_I: case irm_L:
1162 tv->u.CHIL = (a->u.CHIL * b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1165 case irm_c: case irm_h: case irm_i: case irm_l:
1166 tv->u.chil = a->u.chil * b->u.chil;
1167 if ( chil_overflow (tv->u.chil, a->mode)
1168 || (b->u.chil && (tv->u.chil / b->u.chil != a->u.chil))) {
1169 obstack_free (&tv_obst, tv);
1175 mpz_init (&tv->u.Z);
1176 mpz_mul (&tv->u.Z, &a->u.Z, &b->u.Z);
1178 obstack_free (&tv_obst, tv);
1183 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1187 return tarval_identify (tv);
1191 /* Return floating-point `a/b' if computable, else NULL.
1192 Modes must be equal, non-floating-point operands are converted to irm_d. */
1194 tarval_quo (tarval *a, tarval *b)
1198 TARVAL_VRFY (a); TARVAL_VRFY (b);
1199 assert (a->mode == b->mode);
1201 switch (get_mode_modecode(a->mode)) {
1204 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1206 tv->u.f = a->u.f / b->u.f; /* @@@ overflow etc */
1209 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1211 tv->u.d = a->u.d / b->u.d; /* @@@ overflow etc */
1214 a = tarval_convert_to (a, mode_d);
1215 b = tarval_convert_to (b, mode_d);
1216 return a && b ? tarval_quo (a, b) : NULL;
1219 return tarval_identify (tv);
1223 /* Return `a/b' if computable, else NULL. Modes must be equal. */
1225 tarval_div (tarval *a, tarval *b)
1229 TARVAL_VRFY (a); TARVAL_VRFY (b);
1230 assert (a->mode == b->mode);
1232 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1236 switch (get_mode_modecode(a->mode)) {
1238 case irm_f: tv->u.f = floor (a->u.f / b->u.f); break; /* @@@ overflow etc */
1239 case irm_d: tv->u.d = floor (a->u.d / b->u.d); break; /* @@@ dto. */
1241 case irm_C: case irm_H: case irm_I: case irm_L:
1242 if (!b->u.CHIL) goto fail;
1243 tv->u.CHIL = a->u.CHIL / b->u.CHIL;
1246 case irm_c: case irm_h: case irm_i: case irm_l:
1248 || ((b->u.chil == -1) && (a->u.chil == tv_val_chil (get_mode_max(a->mode)) ))) {
1250 obstack_free (&tv_obst, tv);
1253 tv->u.chil = a->u.chil / b->u.chil;
1257 if (!mpz_cmp_ui (&b->u.Z, 0)) goto fail;
1258 mpz_init (&tv->u.Z);
1259 mpz_div (&tv->u.Z, &a->u.Z, &b->u.Z);
1265 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1269 return tarval_identify (tv);
1273 /* Return `a%b' if computable, else NULL. Modes must be equal. */
1275 tarval_mod (tarval *a, tarval *b)
1279 TARVAL_VRFY (a); TARVAL_VRFY (b);
1280 assert (a->mode == b->mode);
1282 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1286 switch (get_mode_modecode(a->mode)) {
1288 case irm_f: tv->u.f = fmod (a->u.f, b->u.f); break; /* @@@ overflow etc */
1289 case irm_d: tv->u.d = fmod (a->u.d, b->u.d); break; /* @@@ dto */
1291 case irm_C: case irm_H: case irm_I: case irm_L:
1292 if (!b->u.CHIL) goto fail;
1293 tv->u.CHIL = a->u.CHIL % b->u.CHIL;
1296 case irm_c: case irm_h: case irm_i: case irm_l:
1299 obstack_free (&tv_obst, tv);
1302 tv->u.chil = a->u.chil % b->u.chil;
1306 if (!mpz_cmp_ui (&b->u.Z, 0)) goto fail;
1307 mpz_init (&tv->u.Z);
1308 mpz_mod (&tv->u.Z, &a->u.Z, &b->u.Z);
1314 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1318 return tarval_identify (tv);
1321 /* Return |a| if computable, else Null. */
1322 /* is -max == min?? */
1324 tarval_abs (tarval *a) {
1326 if (tv_is_negative(a)) return tarval_neg(a);
1331 tv_is_negative(tarval *a) {
1333 switch (get_mode_modecode(a->mode)) {
1335 case irm_f: return (a->u.f<0); break;
1336 case irm_d: return (a->u.d<0); break;
1338 case irm_C: case irm_H: case irm_I: case irm_L:
1342 case irm_c: case irm_h: case irm_i: case irm_l:
1343 return (a->u.chil < 0);
1355 /* Return `a&b'. Modes must be equal. */
1357 tarval_and (tarval *a, tarval *b)
1361 TARVAL_VRFY (a); TARVAL_VRFY (b);
1362 assert (a->mode == b->mode);
1364 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1368 switch (get_mode_modecode(a->mode)) {
1370 case irm_C: case irm_H: case irm_I: case irm_L:
1371 tv->u.CHIL = a->u.CHIL & b->u.CHIL; break;
1373 case irm_c: case irm_h: case irm_i: case irm_l:
1374 tv->u.chil = a->u.chil & b->u.chil; break;
1377 mpz_init (&tv->u.Z);
1378 mpz_and (&tv->u.Z, &a->u.Z, &b->u.Z);
1384 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1388 return tarval_identify (tv);
1392 /* Return `a|b'. Modes must be equal. */
1394 tarval_or (tarval *a, tarval *b)
1398 TARVAL_VRFY (a); TARVAL_VRFY (b);
1399 assert (a->mode == b->mode);
1401 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1405 switch (get_mode_modecode(a->mode)) {
1407 case irm_C: case irm_H: case irm_I: case irm_L:
1408 tv->u.CHIL = a->u.CHIL | b->u.CHIL; break;
1410 case irm_c: case irm_h: case irm_i: case irm_l:
1411 tv->u.chil = a->u.chil | b->u.chil; break;
1414 mpz_init (&tv->u.Z);
1415 mpz_ior (&tv->u.Z, &a->u.Z, &b->u.Z);
1421 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1425 return tarval_identify (tv);
1429 /* Return `a^b'. Modes must be equal. */
1431 tarval_eor (tarval *a, tarval *b)
1435 TARVAL_VRFY (a); TARVAL_VRFY (b);
1436 assert (a->mode == b->mode);
1438 #if 1 /* see case irm_Z below */
1439 if (a->mode == mode_Z) return NULL;
1442 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1446 switch (get_mode_modecode(a->mode)) {
1448 case irm_C: case irm_H: case irm_I: case irm_L:
1449 tv->u.CHIL = a->u.CHIL ^ b->u.CHIL; break;
1451 case irm_c: case irm_h: case irm_i: case irm_l:
1452 tv->u.chil = a->u.chil ^ b->u.chil; break;
1455 /* gmp-1.3.2 declares but does not define mpz_xor() */
1456 mpz_init (&tv->u.Z);
1457 mpz_xor (&tv->u.Z, &a->u.Z, &b->u.Z);
1461 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1465 return tarval_identify (tv);
1469 /* Return `a<<b' if computable, else NULL. */
1471 tarval_shl (tarval *a, tarval *b)
1477 TARVAL_VRFY (a); TARVAL_VRFY (b);
1479 shift = tarval_ord (b, &b_is_huge);
1482 || ((shift >= get_mode_size(mode_l)*target_bits) && (a->mode != mode_Z))) {
1486 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1489 switch (get_mode_modecode(a->mode)) {
1491 case irm_C: case irm_H: case irm_I: case irm_L:
1492 tv->u.CHIL = a->u.CHIL << shift;
1495 case irm_c: case irm_h: case irm_i: case irm_l:
1496 tv->u.chil = a->u.chil << shift;
1500 mpz_init (&tv->u.Z);
1501 mpz_mul_2exp (&tv->u.Z, &a->u.Z, shift);
1506 default: assert (0);
1509 return tarval_identify (tv);
1513 /* Return `a>>b' if computable, else NULL.
1514 The interpretation of >> (sign extended or not) is implementaion
1515 dependent, i.e. this is neither shr nor shrs!! */
1517 tarval_shr (tarval *a, tarval *b)
1523 TARVAL_VRFY (a); TARVAL_VRFY (b);
1525 shift = tarval_ord (b, &b_is_huge);
1528 || ((shift >= get_mode_size(mode_l)*target_bits) && (a->mode != mode_Z))) {
1532 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1535 switch (get_mode_modecode(a->mode)) {
1537 case irm_C: case irm_H: case irm_I: case irm_L:
1538 tv->u.CHIL = a->u.CHIL >> shift;
1541 case irm_c: case irm_h: case irm_i: case irm_l:
1542 tv->u.chil = a->u.chil >> shift;
1546 mpz_init (&tv->u.Z);
1547 mpz_div_2exp (&tv->u.Z, &a->u.Z, shift);
1552 default: assert (0);
1555 return tarval_identify (tv);
1559 /* Classify `tv', which may be NULL.
1560 Return 0 if `tv' is the additive neutral element, 1 if `tv' is the
1561 multiplicative neutral element, and -1 if `tv' is the neutral
1562 element of bitwise and. */
1564 tarval_classify (tarval *tv)
1570 switch (get_mode_modecode(tv->mode)) {
1572 case irm_f: case irm_d:
1576 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_C))) - 1;
1578 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_H))) - 1;
1580 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_I))) - 1;
1582 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_L))) - 1;
1584 case irm_c: case irm_h: case irm_i: case irm_l:
1588 if (mpz_cmp_si (&tv->u.Z, 0)) return 0;
1589 else if (mpz_cmp_si (&tv->u.Z, 1)) return 1;
1590 else if (mpz_cmp_si (&tv->u.Z,-1)) return -1;
1604 tarval_s_fits (tarval *tv, long min, long max) {
1605 return (( mpz_cmp_si (&tv->u.Z, min) >= 0)
1606 && mpz_cmp_si (&tv->u.Z, max) <= 0);
1610 tarval_u_fits (tarval *tv, unsigned long max) {
1611 return (( mpz_sgn (&tv->u.Z) >= 0)
1612 && mpz_cmp_si (&tv->u.Z, max) <= 0);
1616 /* Convert `tv' into type `long', set `fail' if not representable.
1617 If `fail' gets set for an unsigned `tv', the correct result can be
1618 obtained by casting the result to `unsigned long'. */
1620 tarval_ord (tarval *tv, int *fail)
1624 switch (get_mode_modecode(tv->mode)) {
1626 case irm_C: case irm_H: case irm_I: case irm_L:
1627 *fail = tv->u.CHIL > tv_val_CHIL (get_mode_max(mode_l));
1630 case irm_c: case irm_h: case irm_i: case irm_l:
1635 *fail = ( (mpz_cmp_si (&tv->u.Z, tv_val_chil(get_mode_max(mode_l))) > 0)
1636 || (mpz_cmp_si (&tv->u.Z, tv_val_chil(get_mode_max(mode_l))) < 0));
1637 return mpz_get_si (&tv->u.Z);
1654 tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
1656 tarval *val = XP_GETARG (tarval *, 0);
1661 switch (get_mode_modecode(val->mode)) {
1663 case irm_T: /* none */
1664 printed = XPSR ("<bad>");
1667 case irm_f: /* float */
1668 printed = XPF1R ("%g", (double)(val->u.f));
1670 case irm_d: /* double */
1671 printed = XPF1R ("%g", (double)(val->u.d));
1674 case irm_c: /* signed char */
1675 case irm_C: /* unsigned char */
1676 if (isprint (val->u.chil)) {
1677 printed = XPF1R ("'%c'", val->u.chil);
1679 printed = XPF1R ("0x%x", (unsigned long)val->u.chil);
1683 case irm_h: case irm_i: case irm_l: /* signed num */
1684 printed = XPF1R ("%ld", (long)val->u.chil);
1686 case irm_H: case irm_I: case irm_L: /* unsigned num */
1687 printed = XPF1R ("%lu", (unsigned long)val->u.CHIL);
1690 case irm_Z: /* mp int */
1691 printed = XPF1R ("%Z", &val->u.Z);
1694 case irm_p: /* pointer */
1695 if (val->u.p.xname) {
1696 printed = XPR (val->u.p.xname);
1697 } else if (val->u.p.ent) {
1698 printed = XPF1R ("(%I)", get_entity_ld_ident(val->u.p.ent));
1700 assert (val == tarval_p_void);
1701 printed = XPSR ("(void)");
1705 case irm_b: /* boolean */
1706 if (val->u.b) printed = XPSR ("true");
1707 else printed = XPSR ("false");
1710 case irm_B: /* universal bits */
1711 printed = XPSR ("<@@@ some bits>");
1714 case irm_s: /* string */
1717 char *buf = alloca (val->u.s.n + 2);
1723 for (i = 0; i < val->u.s.n; ++i) {
1724 if (isprint (val->u.s.p[i])) {
1725 *bp++ = val->u.s.p[i];
1731 XPF1 ("'\\%03o'", val->u.s.p[i]);
1740 case irm_M: /* memory */
1741 case irm_R: /* region */
1751 get_tv_mode (tarval *tv)