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 /* Used to be in irmode.h, replaced now. */
90 # define is_chilCHIL(m) ((m) <= irm_L && (m) >= irm_c) /* old */
92 /* return a mode-specific value */
101 tv_val_d (tarval *tv)
107 tv_val_chil (tarval *tv)
113 tv_val_CHIL (tarval *tv)
119 tv_val_Z (tarval *tv)
125 tv_val_p (tarval *tv)
131 tv_val_b (tarval *tv)
137 tv_val_B (tarval *tv)
143 tv_val_s (tarval *tv)
149 /* Overflows `chil' signed integral `mode'? */
151 chil_overflow (tarval_chil chil, ir_mode *mode)
153 assert (is_chilCHIL(get_mode_modecode(mode)));
154 return (get_mode_min(mode) && get_mode_max(mode) /* only valid after firm initialization */
155 && (chil < tv_val_chil (get_mode_min(mode))
156 || tv_val_chil (get_mode_max(mode)) < chil));
160 /* Overflows `CHIL' unsigned integral `mode'? */
162 CHIL_overflow (tarval_CHIL CHIL, ir_mode *mode)
164 assert (is_chilCHIL(get_mode_modecode(mode)));
165 return (get_mode_max(mode) /* only valid after firm initialization */
166 && tv_val_CHIL (get_mode_max(mode)) < CHIL);
172 _tarval_vrfy (const tarval *val)
175 switch (get_mode_modecode(val->mode)) {
181 case irm_C: case irm_H: case irm_I: case irm_L:
182 assert (!CHIL_overflow (val->u.CHIL, val->mode)); break;
183 case irm_c: case irm_h: case irm_i: case irm_l:
184 assert (!chil_overflow (val->u.chil, val->mode)); break;
190 assert (val->u.p.ent->kind == k_entity);
191 assert ( val->u.p.xname || val->u.p.ent
192 || !tarval_p_void || (val == tarval_p_void));
196 assert (val->u.s.p); break;
198 assert (val->u.B.p); break;
200 assert ((unsigned)val->u.b <= 1); break;
202 assert (val->mode == mode_T);
213 pset_stats (tarvals);
219 /* Return the canonical tarval * for tv.
220 May destroy everything allocated on tv_obst after tv! */
222 tarval_identify (tarval *tv)
226 o = pset_insert (tarvals, tv, tarval_hash (tv));
229 obstack_free (&tv_obst, (void *)tv);
237 /* Return 0 iff a equals b. Bitwise identical NaNs compare equal. */
239 tarval_cmp (const void *p, const void *q)
247 if (a == b) return 0;
248 if ((void *)a->mode - (void *)b->mode)
249 return (void *)a->mode - (void *)b->mode;
251 switch (get_mode_modecode(a->mode)) {
254 return memcmp (&a->u.f, &b->u.f, sizeof (a->u.f));
256 return memcmp (&a->u.d, &b->u.d, sizeof (a->u.d));
258 case irm_C: case irm_H: case irm_I: case irm_L:
259 if (sizeof (int) == sizeof (tarval_CHIL)) {
260 return a->u.CHIL - b->u.CHIL;
262 return a->u.CHIL != b->u.CHIL;
264 case irm_c: case irm_h: case irm_i: case irm_l:
265 if (sizeof (int) == sizeof (tarval_chil)) {
266 return a->u.chil - b->u.chil;
268 return a->u.chil != b->u.chil;
271 return mpz_cmp (&a->u.Z, &b->u.Z);
277 if (a->u.p.ent || b->u.p.ent)
278 return (char *)a->u.p.ent - (char *)b->u.p.ent;
279 if (a->u.p.xname && b->u.p.xname)
280 return strcmp (a->u.p.xname, b->u.p.xname);
281 return a->u.p.xname - b->u.p.xname;
283 return a->u.b - b->u.b;
285 return ( a->u.B.n - b->u.B.n
286 ? memcmp (a->u.B.p, b->u.B.p, a->u.B.n)
287 : a->u.B.n - b->u.B.n);
288 case irm_s: case irm_S:
289 return ( a->u.s.n == b->u.s.n
290 ? memcmp (a->u.s.p, b->u.s.p, a->u.s.n)
291 : a->u.s.n - b->u.s.n);
298 tarval_hash (tarval *tv)
302 h = get_mode_modecode(tv->mode) * 0x421u;
303 switch (get_mode_modecode(tv->mode)) {
305 h = 0x94b527ce; break;
308 { union { float f; unsigned u; } u;
309 assert (sizeof (float) <= sizeof (unsigned));
310 u.u = 0; u.f = tv->u.f;
316 { union { double d; unsigned u[2]; } u;
317 assert (sizeof (double) <= 2*sizeof (unsigned));
318 u.u[0] = u.u[1] = 0; u.d = tv->u.d;
319 h ^= u.u[0] ^ u.u[1];
322 case irm_C: case irm_H: case irm_I: case irm_L:
323 h ^= tv->u.CHIL; break;
324 case irm_c: case irm_h: case irm_i: case irm_l:
325 h ^= tv->u.chil; break;
328 h ^= mpz_get_ui (&tv->u.Z); break;
330 h ^= (unsigned int) tv; break; /* tut das? */
334 /* @@@ lower bits not random, watch for collisions; perhaps
335 replace by tv->u.p.ent - (entity *)0 */
336 h ^= ((char *)tv->u.p.ent - (char *)0) / 64;
337 } else if (tv->u.p.xname) {
338 /* Of course, strlen() in a hash function is a mistake, but this
339 case should be really rare. */
340 h ^= ID_HASH (tv->u.p.xname, strlen (tv->u.p.xname));
348 h ^= tv->u.B.n; break; /* @@@ not really good */
350 h ^= tv->u.s.p[0]<<12 ^ tv->u.s.p[tv->u.s.n]<<4 ^ tv->u.s.n; break;
352 h ^= tv->u.s.p[0]<<4 ^ tv->u.s.p[tv->u.s.n]<<12 ^ tv->u.s.n; break;
361 /*** ***************** Initialization ************************************* ***/
366 obstack_init (&tv_obst);
367 obstack_alignment_mask (&tv_obst) = ALIGNOF (tarval) - 1;
368 assert (IS_POW2 (ALIGNOF (tarval)));
370 /* initialize the target value table */
371 tarvals = new_pset (tarval_cmp, TUNE_NCONSTANTS);
378 union ieee754_double x;
380 /* assumed by tarval_hash(): */
381 assert (sizeof (float) * CHAR_BIT == 32);
382 assert (sizeof (double) * CHAR_BIT == 64);
385 /* assumed by tarval_chil & friends: */
386 assert ( (irm_C == irm_c+1) && (irm_h == irm_C+1)
387 && (irm_H == irm_h+1) && (irm_i == irm_H+1)
388 && (irm_I == irm_i+1) && (irm_l == irm_I+1)
389 && (irm_L == irm_l+1));
391 /* assumed everywhere: */
392 for (i = 0; i <= irm_L-irm_c; i += 2) {
393 assert ( IS_POW2 (max_chil[i+1]+1)
394 && (min_chil[i] == -max_chil[i]-1)
395 && ((tarval_CHIL)max_chil[i+1] == (tarval_CHIL)max_chil[i]-min_chil[i]));
400 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
402 tarval_bad = tarval_identify (tv);
404 tarval_b_false = tarval_from_long (mode_b, 0);
405 tarval_b_true = tarval_from_long (mode_b, 1);
407 /* IsInf <-> exponent == 0x7ff && ! (bits | fraction_low) */
408 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
411 x.ieee.exponent = 0x7ff;
412 x.ieee.mantissa0 = 0;
413 x.ieee.mantissa1 = 0;
415 tarval_d_Inf = tarval_identify (tv);
417 /* IsNaN <-> exponent==0x7ff && (qnan_bit | bits | fraction_low) */
418 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
420 x.ieee_nan.negative = 0;
421 x.ieee_nan.exponent = 0x7ff;
422 x.ieee_nan.quiet_nan = 1; /* @@@ quiet or signalling? */
423 x.ieee_nan.mantissa0 = 42;
424 x.ieee_nan.mantissa1 = 0;
425 assert(x.d != x.d /* x.d is NaN */);
427 tarval_d_NaN = tarval_identify (tv);
429 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
431 tv->u.p.xname = NULL;
434 tarval_p_void = tarval_identify (tv);
436 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
439 tarval_mode_null [irm_f] = tarval_from_long (mode_f, 0);
440 tarval_mode_null [irm_d] = tarval_from_long (mode_d, 0);
441 tarval_mode_null [irm_c] = tarval_from_long (mode_c, 0);
442 tarval_mode_null [irm_C] = tarval_from_long (mode_C, 0);
443 tarval_mode_null [irm_h] = tarval_from_long (mode_h, 0);
444 tarval_mode_null [irm_H] = tarval_from_long (mode_H, 0);
445 tarval_mode_null [irm_i] = tarval_from_long (mode_i, 0);
446 tarval_mode_null [irm_I] = tarval_from_long (mode_I, 0);
447 tarval_mode_null [irm_l] = tarval_from_long (mode_l, 0);
448 tarval_mode_null [irm_L] = tarval_from_long (mode_L, 0);
449 tarval_mode_null [irm_b] = tarval_b_false;
450 tarval_mode_null [irm_p] = tarval_p_void;
455 /*** ********************** Constructors for tarvals ********************** ***/
457 /* copy from src to dst len chars omitting '_'. */
459 stripcpy (char *dst, const char *src, size_t len)
464 if (*src == '_') src++;
467 *d = 0; /* make it 0-terminated. */
474 tarval_Z_from_str (const char *s, size_t len, int base)
481 buf = alloca (len+1);
482 stripcpy (buf, s, len);
484 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
487 if (mpz_init_set_str (&tv->u.Z, buf, base)) assert (0);
489 assert(0 && "no support for Z in tv!");
492 return tarval_identify (tv);
497 tarval_B_from_str (const char *s, size_t len)
500 size_t n; /* size of B */
501 const char *r; /* read ptr */
502 unsigned x; /* bit store */
503 int b; /* bits in x */
504 int shift; /* base shift */
509 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
512 assert (s[0] == '0');
515 case 'O': shift = 3; break;
517 case 'X': shift = 4; break;
521 r = s+len; /* set r past input */
522 s += 2; /* skip header */
527 if (*r == '_') continue; /* skip _ styropor */
528 if (('0' <= *r) && (*r <= '9')) {
530 } else if (('a' <= *r) && (*r <= 'f')) {
532 } else { assert (('A' <= *r) && (*r <= 'F'));
536 x |= d << b; /* insert d into x above the b present bits */
537 b += shift; /* x now contains shift more bits */
539 if (b >= 8) { /* we've accumulated at least a byte */
540 char c = x & 0xFF; /* extract the lower 8 bits from x */
541 obstack_grow (&tv_obst, &c, 1); /* and stuff them into B */
542 x >>= 8; /* remove the lower 8 bits from x */
543 b -= 8; /* x now contains 8 bits fewer */
544 ++n; /* B grew a byte */
548 if (b >= 0) { /* flush the rest of the bits */
549 char c = x; /* extract them */
550 obstack_grow (&tv_obst, &c, 1); /* and stuff them into B */
551 ++n; /* B grew a byte */
554 { unsigned char *p = obstack_finish (&tv_obst);
555 unsigned char *q = p + n;
559 /* reverse p in place */
560 while (p < q) { char c = *p; *p++ = *q; *q-- = c; }
563 return tarval_identify (tv);
568 tarval_f_from_str (const char *s, size_t len)
576 buf = alloca (len+1);
577 stripcpy (buf, s, len);
579 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
581 tv->u.f = (float)strtod (buf, &eptr);
582 assert (eptr == buf+strlen(buf));
584 return tarval_identify (tv);
589 tarval_d_from_str (const char *s, size_t len)
597 buf = alloca (len+1);
598 stripcpy (buf, s, len);
600 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
602 tv->u.d = strtod (buf, &eptr);
603 assert (eptr == buf+strlen(buf));
605 return tarval_identify (tv);
610 tarval_s_from_str (const char *s, size_t len)
616 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
620 tv->u.s.p = obstack_copy (&tv_obst, s, len);
622 return tarval_identify (tv);
626 tarval_S_from_str (const char *s, size_t len)
632 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
636 tv->u.s.p = obstack_copy (&tv_obst, s, len);
638 return tarval_identify (tv);
641 tarval *tarval_int_from_str (const char *s, size_t len, int base, ir_mode *m) {
646 assert (mode_is_int(m));
649 buf = alloca (len+1);
650 stripcpy (buf, s, len);
653 val = strtol(buf, &eptr, base); /* strtoll */
654 assert (eptr == buf+strlen(buf));
655 if ((errno == ERANGE) &&
656 ((m == mode_l) || (m == mode_L)) ) {
657 printf("WARNING: Constant %s not representable. Continuing with %ld.\n",
661 return tarval_from_long(m, val);
664 /* Create a tarval with mode `m' and value `i' casted to the type that
665 represents such tarvals on host. The resulting value must be legal
668 tarval_from_long (ir_mode *m, long val)
674 if (m == mode_T) return tarval_bad;
676 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
679 switch (get_mode_modecode(m)) {
682 tv->u.f = val; break;
684 tv->u.d = val; break;
686 case irm_C: case irm_H: case irm_I: case irm_L:
687 tv->u.CHIL = val; break;
689 case irm_c: case irm_h: case irm_i: case irm_l:
690 tv->u.chil = val; break;
693 mpz_init_set_si (&tv->u.Z, val);
695 assert(0 && "no support for Z in tv!");
701 obstack_free (&tv_obst, tv);
702 return tarval_p_void;
704 tv->u.b = !!val; /* u.b must be 0 or 1 */
710 return tarval_identify (tv);
715 tarval_p_from_str (const char *xname)
721 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
724 tv->u.p.xname = obstack_copy0 (&tv_obst, xname, strlen (xname));
727 return tarval_identify (tv);
732 tarval_p_from_entity (entity *ent)
738 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
741 tv->u.p.xname = NULL;
744 return tarval_identify (tv);
748 /* Routines for building a tarval step by step follow.
749 Legal calling sequences:
751 No contructors except tarval_append() and tarval_append1 ()
752 tarval_finish_as() or tarval_cancel() */
754 /* Begin building a tarval. */
759 obstack_blank (&tv_obst, sizeof (tarval));
763 /* Append `n' chars from `p' to the tarval currently under construction. */
765 tarval_append (const char *p, size_t n)
768 obstack_grow (&tv_obst, p, n);
772 /* Append `ch' to the tarval currently under construction. */
774 tarval_append1 (char ch)
777 obstack_1grow (&tv_obst, ch);
781 /* Finish the tarval currently under construction and give id mode `m'.
782 `m' must be irm_C, irm_B, irm_s or irm_S.
783 Return NULL if the value does not make sense for this mode, this
784 can only happen in mode C. */
786 tarval_finish_as (ir_mode *m)
788 int size = obstack_object_size (&tv_obst) - sizeof (tarval);
791 char ch = 0; /* initialized to shut up gcc */
793 assert (BUILDING && (size >= 0));
795 if (size != 1) return tarval_cancel();
796 p = (unsigned char *)obstack_base (&tv_obst) + sizeof (tarval);
798 obstack_blank (&tv_obst, -size);
800 tv = obstack_finish (&tv_obst);
801 p = (unsigned char *)tv + sizeof (tarval);
804 switch (get_mode_modecode(m)) {
824 return tarval_identify (tv);
828 /* Cancel tarval building and return tarval_bad. */
833 obstack_free (&tv_obst, obstack_finish (&tv_obst));
839 /*** ****************** Arithmethic operations on tarvals ***************** ***/
841 /* Return `src' converted to mode `m' if representable, else NULL.
842 @@@ lots of conversions missing */
844 tarval_convert_to (tarval *src, ir_mode *m)
848 if (m == src->mode) return src;
850 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
853 switch (get_mode_modecode(src->mode)) {
856 if (m != mode_f) goto fail;
862 switch (get_mode_modecode(m)) {
864 case irm_C: case irm_H: case irm_I: case irm_L:
865 if (mpz_cmp_si (&src->u.Z, 0) < 0) goto fail;
866 if (mpz_size (&src->u.Z) > 1) goto fail;
867 tv->u.CHIL = mpz_get_ui (&src->u.Z);
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 = mpz_get_si (&src->u.Z);
873 if (chil_overflow (tv->u.chil, m)) goto fail;
877 tv ->u.b = !mpz_cmp_ui (&src->u.Z, 0);
881 if (mpz_cmp_ui (&src->u.Z, 0)) goto fail;
882 obstack_free (&tv_obst, tv);
883 return tarval_p_void;
892 case irm_c: case irm_h: case irm_i: case irm_l:
893 switch (get_mode_modecode(m)) {
894 case irm_c: case irm_h: case irm_i: case irm_l:
895 tv->u.chil = src->u.chil;
896 if (chil_overflow (tv->u.chil, m)) goto fail;
899 case irm_C: case irm_H: case irm_I: case irm_L:
900 tv->u.CHIL = src->u.chil;
901 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
906 mpz_init_set_si (&tv->u.Z, src->u.chil);
913 tv->u.b = !!src->u.chil;
919 case irm_C: case irm_H: case irm_I: case irm_L:
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.CHIL;
923 if (chil_overflow (tv->u.chil, m)) goto fail;
926 case irm_C: case irm_H: case irm_I: case irm_L:
927 tv->u.CHIL = src->u.CHIL;
928 if (CHIL_overflow (tv->u.CHIL, m)) goto fail;
933 mpz_init_set_ui (&tv->u.Z, src->u.CHIL);
940 tv->u.b = !!src->u.CHIL;
948 switch (get_mode_modecode(m)) {
949 case irm_c: case irm_h: case irm_i: case irm_l:
950 tv->u.chil = src->u.b;
953 case irm_C: case irm_H: case irm_I: case irm_L:
954 tv->u.CHIL = src->u.b;
962 obstack_free (&tv_obst, tv);
966 return tarval_identify (tv);
970 /* GL Why are there no ArmRoq comments, why is this not used? */
972 tarval_neg (tarval *a)
978 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
982 switch (get_mode_modecode(a->mode)) {
984 case irm_f: tv->u.f = -a->u.f; break;
985 case irm_d: tv->u.d = -a->u.d; break;
987 case irm_C: case irm_H: case irm_I: case irm_L:
988 tv->u.CHIL = -a->u.CHIL & tv_val_CHIL (get_mode_max(a->mode));
991 case irm_c: case irm_h: case irm_i: case irm_l:
992 tv->u.chil = -a->u.chil;
993 if ( chil_overflow (tv->u.chil, a->mode)
994 || ((tv->u.chil >= 0) == (a->u.chil >= 0))) {
995 obstack_free (&tv_obst, tv);
1001 mpz_init (&tv->u.Z);
1002 mpz_neg (&tv->u.Z, &a->u.Z);
1004 obstack_free (&tv_obst, tv);
1006 printf("\nWrong negation\n\n");
1010 case irm_b: tv->u.b = !a->u.b; break;
1014 return tarval_identify (tv);
1018 /* Compare `a' with `b'.
1019 Return one of irpn_Lt, irpn_Eq, irpn_Gt, irpn_Uo, or irpn_False if
1020 result is unknown. */
1022 tarval_comp (tarval *a, tarval *b)
1028 assert (a->mode == b->mode);
1030 switch (get_mode_modecode(a->mode)) {
1032 case irm_f: return ( a->u.f == b->u.f ? irpn_Eq
1033 : a->u.f > b->u.f ? irpn_Gt
1034 : a->u.f < b->u.f ? irpn_Lt
1036 case irm_d: return ( a->u.d == b->u.d ? irpn_Eq
1037 : a->u.d > b->u.d ? irpn_Gt
1038 : a->u.d < b->u.d ? irpn_Lt
1041 case irm_C: case irm_H: case irm_I: case irm_L:
1042 return ( a->u.CHIL == b->u.CHIL ? irpn_Eq
1043 : a->u.CHIL > b->u.CHIL ? irpn_Gt
1046 case irm_c: case irm_h: case irm_i: case irm_l:
1047 return ( a->u.chil == b->u.chil ? irpn_Eq
1048 : a->u.chil > b->u.chil ? irpn_Gt
1053 int cmp = mpz_cmp (&a->u.Z, &b->u.Z);
1054 return ( cmp == 0 ? irpn_Eq
1062 case irm_b: return ( a->u.b == b->u.b ? irpn_Eq
1063 : a->u.b > b->u.b ? irpn_Gt
1065 /* The following assumes that pointers are unsigned, which is valid
1066 for all sane CPUs (transputers are insane). */
1067 case irm_p: return ( a == b ? irpn_Eq
1068 : a == tarval_p_void ? irpn_Lt
1069 : b == tarval_p_void ? irpn_Gt
1070 : irpn_False); /* unknown */
1071 default: assert (0);
1076 /* Return `a+b' if computable, else NULL. Modes must be equal. */
1078 tarval_add (tarval *a, tarval *b)
1082 TARVAL_VRFY (a); TARVAL_VRFY (b);
1083 assert (a->mode == b->mode);
1085 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1089 switch (get_mode_modecode(a->mode)) {
1091 case irm_f: tv->u.f = a->u.f + b->u.f; break; /* @@@ overflow etc */
1092 case irm_d: tv->u.d = a->u.d + b->u.d; break; /* @@@ dto. */
1094 case irm_C: case irm_H: case irm_I: case irm_L:
1095 tv->u.CHIL = (a->u.CHIL + b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1098 case irm_c: case irm_h: case irm_i: case irm_l:
1099 tv->u.chil = a->u.chil + b->u.chil;
1100 if ( chil_overflow (tv->u.chil, a->mode)
1101 || ((tv->u.chil > a->u.chil) ^ (b->u.chil > 0))) {
1102 obstack_free (&tv_obst, tv);
1108 mpz_init (&tv->u.Z);
1109 mpz_add (&tv->u.Z, &a->u.Z, &b->u.Z);
1111 obstack_free (&tv_obst, tv);
1116 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1120 return tarval_identify (tv);
1124 /* Return `a-b' if computable, else NULL. Modes must be equal. */
1126 tarval_sub (tarval *a, tarval *b)
1130 TARVAL_VRFY (a); TARVAL_VRFY (b);
1131 assert (a->mode == b->mode);
1133 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1137 switch (get_mode_modecode(a->mode)) {
1139 case irm_f: tv->u.f = a->u.f - b->u.f; break; /* @@@ overflow etc */
1140 case irm_d: tv->u.d = a->u.d - b->u.d; break; /* @@@ dto. */
1142 case irm_C: case irm_H: case irm_I: case irm_L:
1143 tv->u.CHIL = (a->u.CHIL - b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1146 case irm_c: case irm_h: case irm_i: case irm_l:
1147 tv->u.chil = a->u.chil - b->u.chil;
1148 if ( chil_overflow (tv->u.chil, a->mode)
1149 || ((tv->u.chil > a->u.chil) ^ (b->u.chil < 0))) {
1150 obstack_free (&tv_obst, tv);
1156 mpz_init (&tv->u.Z);
1157 mpz_sub (&tv->u.Z, &a->u.Z, &b->u.Z);
1159 obstack_free (&tv_obst, tv);
1164 case irm_b: tv->u.b = a->u.b & ~b->u.b; break; /* u.b is in canonical form */
1168 return tarval_identify (tv);
1171 /* Return `a*b' if computable, else NULL. Modes must be equal. */
1173 tarval_mul (tarval *a, tarval *b)
1177 TARVAL_VRFY (a); TARVAL_VRFY (b);
1178 assert (a->mode == b->mode);
1180 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1184 switch (get_mode_modecode(a->mode)) {
1186 case irm_f: tv->u.f = a->u.f * b->u.f; break; /* @@@ overflow etc */
1187 case irm_d: tv->u.d = a->u.d * b->u.d; break; /* @@@ dto. */
1189 case irm_C: case irm_H: case irm_I: case irm_L:
1190 tv->u.CHIL = (a->u.CHIL * b->u.CHIL) & tv_val_CHIL (get_mode_max(a->mode));
1193 case irm_c: case irm_h: case irm_i: case irm_l:
1194 tv->u.chil = a->u.chil * b->u.chil;
1195 if ( chil_overflow (tv->u.chil, a->mode)
1196 || (b->u.chil && (tv->u.chil / b->u.chil != a->u.chil))) {
1197 obstack_free (&tv_obst, tv);
1203 mpz_init (&tv->u.Z);
1204 mpz_mul (&tv->u.Z, &a->u.Z, &b->u.Z);
1206 obstack_free (&tv_obst, tv);
1211 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1215 return tarval_identify (tv);
1219 /* Return floating-point `a/b' if computable, else NULL.
1220 Modes must be equal, non-floating-point operands are converted to irm_d. */
1222 tarval_quo (tarval *a, tarval *b)
1226 TARVAL_VRFY (a); TARVAL_VRFY (b);
1227 assert (a->mode == b->mode);
1229 switch (get_mode_modecode(a->mode)) {
1232 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1234 tv->u.f = a->u.f / b->u.f; /* @@@ overflow etc */
1237 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1239 tv->u.d = a->u.d / b->u.d; /* @@@ overflow etc */
1242 a = tarval_convert_to (a, mode_d);
1243 b = tarval_convert_to (b, mode_d);
1244 return a && b ? tarval_quo (a, b) : NULL;
1247 return tarval_identify (tv);
1251 /* Return `a/b' if computable, else NULL. Modes must be equal. */
1253 tarval_div (tarval *a, tarval *b)
1257 TARVAL_VRFY (a); TARVAL_VRFY (b);
1258 assert (a->mode == b->mode);
1260 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1264 switch (get_mode_modecode(a->mode)) {
1266 case irm_f: tv->u.f = floor (a->u.f / b->u.f); break; /* @@@ overflow etc */
1267 case irm_d: tv->u.d = floor (a->u.d / b->u.d); break; /* @@@ dto. */
1269 case irm_C: case irm_H: case irm_I: case irm_L:
1270 if (!b->u.CHIL) goto fail;
1271 tv->u.CHIL = a->u.CHIL / b->u.CHIL;
1274 case irm_c: case irm_h: case irm_i: case irm_l:
1276 || ((b->u.chil == -1) && (a->u.chil == tv_val_chil (get_mode_max(a->mode)) ))) {
1278 obstack_free (&tv_obst, tv);
1281 tv->u.chil = a->u.chil / b->u.chil;
1285 if (!mpz_cmp_ui (&b->u.Z, 0)) goto fail;
1286 mpz_init (&tv->u.Z);
1287 mpz_div (&tv->u.Z, &a->u.Z, &b->u.Z);
1293 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1297 return tarval_identify (tv);
1301 /* Return `a%b' if computable, else NULL. Modes must be equal. */
1303 tarval_mod (tarval *a, tarval *b)
1307 TARVAL_VRFY (a); TARVAL_VRFY (b);
1308 assert (a->mode == b->mode);
1310 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1314 switch (get_mode_modecode(a->mode)) {
1316 case irm_f: tv->u.f = fmod (a->u.f, b->u.f); break; /* @@@ overflow etc */
1317 case irm_d: tv->u.d = fmod (a->u.d, b->u.d); break; /* @@@ dto */
1319 case irm_C: case irm_H: case irm_I: case irm_L:
1320 if (!b->u.CHIL) goto fail;
1321 tv->u.CHIL = a->u.CHIL % b->u.CHIL;
1324 case irm_c: case irm_h: case irm_i: case irm_l:
1327 obstack_free (&tv_obst, tv);
1330 tv->u.chil = a->u.chil % b->u.chil;
1334 if (!mpz_cmp_ui (&b->u.Z, 0)) goto fail;
1335 mpz_init (&tv->u.Z);
1336 mpz_mod (&tv->u.Z, &a->u.Z, &b->u.Z);
1342 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1346 return tarval_identify (tv);
1349 /* Return |a| if computable, else Null. */
1350 /* is -max == min?? */
1352 tarval_abs (tarval *a) {
1354 if (tv_is_negative(a)) return tarval_neg(a);
1359 tv_is_negative(tarval *a) {
1361 switch (get_mode_modecode(a->mode)) {
1363 case irm_f: return (a->u.f<0); break;
1364 case irm_d: return (a->u.d<0); break;
1366 case irm_C: case irm_H: case irm_I: case irm_L:
1370 case irm_c: case irm_h: case irm_i: case irm_l:
1371 return (a->u.chil < 0);
1383 /* Return `a&b'. Modes must be equal. */
1385 tarval_and (tarval *a, tarval *b)
1389 TARVAL_VRFY (a); TARVAL_VRFY (b);
1390 assert (a->mode == b->mode);
1392 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1396 switch (get_mode_modecode(a->mode)) {
1398 case irm_C: case irm_H: case irm_I: case irm_L:
1399 tv->u.CHIL = a->u.CHIL & b->u.CHIL; break;
1401 case irm_c: case irm_h: case irm_i: case irm_l:
1402 tv->u.chil = a->u.chil & b->u.chil; break;
1405 mpz_init (&tv->u.Z);
1406 mpz_and (&tv->u.Z, &a->u.Z, &b->u.Z);
1412 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1416 return tarval_identify (tv);
1420 /* Return `a|b'. Modes must be equal. */
1422 tarval_or (tarval *a, tarval *b)
1426 TARVAL_VRFY (a); TARVAL_VRFY (b);
1427 assert (a->mode == b->mode);
1429 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1433 switch (get_mode_modecode(a->mode)) {
1435 case irm_C: case irm_H: case irm_I: case irm_L:
1436 tv->u.CHIL = a->u.CHIL | b->u.CHIL; break;
1438 case irm_c: case irm_h: case irm_i: case irm_l:
1439 tv->u.chil = a->u.chil | b->u.chil; break;
1442 mpz_init (&tv->u.Z);
1443 mpz_ior (&tv->u.Z, &a->u.Z, &b->u.Z);
1449 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1453 return tarval_identify (tv);
1457 /* Return `a^b'. Modes must be equal. */
1459 tarval_eor (tarval *a, tarval *b)
1463 TARVAL_VRFY (a); TARVAL_VRFY (b);
1464 assert (a->mode == b->mode);
1466 #if 1 /* see case irm_Z below */
1467 if (a->mode == mode_Z) return NULL;
1470 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1474 switch (get_mode_modecode(a->mode)) {
1476 case irm_C: case irm_H: case irm_I: case irm_L:
1477 tv->u.CHIL = a->u.CHIL ^ b->u.CHIL; break;
1479 case irm_c: case irm_h: case irm_i: case irm_l:
1480 tv->u.chil = a->u.chil ^ b->u.chil; break;
1483 /* gmp-1.3.2 declares but does not define mpz_xor() */
1484 mpz_init (&tv->u.Z);
1485 mpz_xor (&tv->u.Z, &a->u.Z, &b->u.Z);
1489 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1493 return tarval_identify (tv);
1497 /* Return `a<<b' if computable, else NULL. */
1499 tarval_shl (tarval *a, tarval *b)
1505 TARVAL_VRFY (a); TARVAL_VRFY (b);
1507 shift = tarval_ord (b, &b_is_huge);
1510 || ((shift >= get_mode_size(mode_l)*target_bits) && (a->mode != mode_Z))) {
1514 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1517 switch (get_mode_modecode(a->mode)) {
1519 case irm_C: case irm_H: case irm_I: case irm_L:
1520 tv->u.CHIL = a->u.CHIL << shift;
1523 case irm_c: case irm_h: case irm_i: case irm_l:
1524 tv->u.chil = a->u.chil << shift;
1528 mpz_init (&tv->u.Z);
1529 mpz_mul_2exp (&tv->u.Z, &a->u.Z, shift);
1534 default: assert (0);
1537 return tarval_identify (tv);
1541 /* Return `a>>b' if computable, else NULL.
1542 The interpretation of >> (sign extended or not) is implementaion
1543 dependent, i.e. this is neither shr nor shrs!! */
1545 tarval_shr (tarval *a, tarval *b)
1551 TARVAL_VRFY (a); TARVAL_VRFY (b);
1553 shift = tarval_ord (b, &b_is_huge);
1556 || ((shift >= get_mode_size(mode_l)*target_bits) && (a->mode != mode_Z))) {
1560 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1563 switch (get_mode_modecode(a->mode)) {
1565 case irm_C: case irm_H: case irm_I: case irm_L:
1566 tv->u.CHIL = a->u.CHIL >> shift;
1569 case irm_c: case irm_h: case irm_i: case irm_l:
1570 tv->u.chil = a->u.chil >> shift;
1574 mpz_init (&tv->u.Z);
1575 mpz_div_2exp (&tv->u.Z, &a->u.Z, shift);
1580 default: assert (0);
1583 return tarval_identify (tv);
1587 /* Classify `tv', which may be NULL.
1588 Return 0 if `tv' is the additive neutral element, 1 if `tv' is the
1589 multiplicative neutral element, and -1 if `tv' is the neutral
1590 element of bitwise and. */
1592 tarval_classify (tarval *tv)
1598 switch (get_mode_modecode(tv->mode)) {
1600 case irm_f: case irm_d:
1604 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_C))) - 1;
1606 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_H))) - 1;
1608 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_I))) - 1;
1610 return (long)((tv->u.CHIL+1) & tv_val_CHIL (get_mode_max(mode_L))) - 1;
1612 case irm_c: case irm_h: case irm_i: case irm_l:
1616 if (mpz_cmp_si (&tv->u.Z, 0)) return 0;
1617 else if (mpz_cmp_si (&tv->u.Z, 1)) return 1;
1618 else if (mpz_cmp_si (&tv->u.Z,-1)) return -1;
1632 tarval_s_fits (tarval *tv, long min, long max) {
1633 return (( mpz_cmp_si (&tv->u.Z, min) >= 0)
1634 && mpz_cmp_si (&tv->u.Z, max) <= 0);
1638 tarval_u_fits (tarval *tv, unsigned long max) {
1639 return (( mpz_sgn (&tv->u.Z) >= 0)
1640 && mpz_cmp_si (&tv->u.Z, max) <= 0);
1644 /* Convert `tv' into type `long', set `fail' if not representable.
1645 If `fail' gets set for an unsigned `tv', the correct result can be
1646 obtained by casting the result to `unsigned long'. */
1648 tarval_ord (tarval *tv, int *fail)
1652 switch (get_mode_modecode(tv->mode)) {
1654 case irm_C: case irm_H: case irm_I: case irm_L:
1655 *fail = tv->u.CHIL > tv_val_CHIL (get_mode_max(mode_l));
1658 case irm_c: case irm_h: case irm_i: case irm_l:
1663 *fail = ( (mpz_cmp_si (&tv->u.Z, tv_val_chil(get_mode_max(mode_l))) > 0)
1664 || (mpz_cmp_si (&tv->u.Z, tv_val_chil(get_mode_max(mode_l))) < 0));
1665 return mpz_get_si (&tv->u.Z);
1682 tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
1684 tarval *val = XP_GETARG (tarval *, 0);
1690 switch (get_mode_modecode(val->mode)) {
1692 case irm_T: /* none */
1693 printed = XPSR ("<bad>");
1696 case irm_f: /* float */
1697 sprintf (buf, "%1.9e", (float)(val->u.f));
1698 printed = XPF1R ("%s", buf);
1700 case irm_d: /* double */
1701 printed = XPF1R ("%1.30g", (double)(val->u.d));
1704 case irm_c: /* signed char */
1705 case irm_C: /* unsigned char */
1706 if ((isprint (val->u.chil)) &&
1707 (val->u.chil != '\\') && (val->u.chil != '\'')) {
1708 printed = XPF1R ("'%c'", val->u.chil);
1710 printed = XPF1R ("0x%x", (unsigned long)val->u.chil);
1714 case irm_h: case irm_i: case irm_l: /* signed num */
1715 printed = XPF1R ("%ld", (long)val->u.chil);
1717 case irm_H: case irm_I: case irm_L: /* unsigned num */
1718 printed = XPF1R ("%lu", (unsigned long)val->u.CHIL);
1721 case irm_Z: /* mp int */
1722 printed = XPF1R ("%Z", &val->u.Z);
1725 case irm_p: /* pointer */
1726 if (val->u.p.xname) {
1727 printed = XPR (val->u.p.xname);
1728 } else if (val->u.p.ent) {
1729 if (get_entity_peculiarity(val->u.p.ent) == existent)
1730 printed = XPF1R ("&(%I)", get_entity_ld_ident(val->u.p.ent));
1732 printed = XPSR ("(NULL)");
1734 assert (val == tarval_p_void);
1735 printed = XPSR ("(void)");
1739 case irm_b: /* boolean */
1740 if (val->u.b) printed = XPSR ("true");
1741 else printed = XPSR ("false");
1744 case irm_B: /* universal bits */
1745 printed = XPSR ("<@@@ some bits>");
1748 case irm_s: /* string */
1751 char *buf = alloca (val->u.s.n + 2);
1757 for (i = 0; i < val->u.s.n; ++i) {
1758 if (isprint (val->u.s.p[i])) {
1759 *bp++ = val->u.s.p[i];
1765 XPF1 ("'\\%03o'", val->u.s.p[i]);
1774 case irm_M: /* memory */
1775 case irm_R: /* region */
1785 get_tv_mode (tarval *tv)
1790 /* Returns the entity if the tv is a pointer to an entity, else
1792 entity *get_tv_entity(tarval *tv) {
1795 if (tv->mode == mode_p) {
1796 if (tv->u.p.xname) {
1799 } else if (tv->u.p.ent) {