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
42 #define TOBSTACK_ID "tv"
52 static struct obstack tv_obst; /* obstack for all the target values */
53 static pset *tarvals; /* pset containing pointers to _all_ tarvals */
55 /* currently building an object with tarval_start() & friends ? */
56 #define BUILDING obstack_object_size (&tv_obst)
58 /* bcopy is not ISO C */
59 #define bcopy(X, Y, Z) memcpy((Y), (X), (Z))
62 /* special tarvals: */
64 tarval *tarval_b_false;
65 tarval *tarval_b_true;
68 tarval *tarval_P_void;
69 tarval *tarval_mode_null[irm_max];
70 tarval *tarval_mode_min[irm_max];
71 tarval *tarval_mode_max[irm_max];
73 tarval *get_tarval_bad () { return tarval_bad; }
74 tarval *get_tarval_b_false () { return tarval_b_false; }
75 tarval *get_tarval_b_true () { return tarval_b_true; }
76 tarval *get_tarval_D_NaN () { return tarval_D_NaN; }
77 tarval *get_tarval_D_Inf () { return tarval_D_Inf; }
78 tarval *get_tarval_P_void () { return tarval_P_void; }
79 tarval *get_tarval_mode_null(ir_mode *mode)
80 { return tarval_mode_null[get_mode_modecode(mode)]; }
81 tarval *get_tarval_mode_min (ir_mode *mode)
82 { return tarval_mode_min[get_mode_modecode(mode)]; }
83 tarval *get_tarval_mode_max (ir_mode *mode)
84 { return tarval_mode_max[get_mode_modecode(mode)]; }
87 /* @@@ depends on order of ir_mode */
88 static tarval_sInt min_sInt[8] = {
94 static tarval_sInt max_sInt[8] = {
95 TARGET_SIMAX (c), TARGET_UIMAX (C),
96 TARGET_SIMAX (h), TARGET_UIMAX (H),
97 TARGET_SIMAX (i), TARGET_UIMAX (I),
98 TARGET_SIMAX (l), TARGET_UIMAX (L)
102 /* Used to be in irmode.h, replaced now. */
103 # define is_Int(m) ((m) <= irm_Lu && (m) >= irm_Bs) /* old */
105 /* return a mode-specific value */
108 tv_val_F (tarval *tv)
114 tv_val_D (tarval *tv)
120 tv_val_E (tarval *tv)
126 tv_val_sInt (tarval *tv)
132 tv_val_uInt (tarval *tv)
138 tv_val_P (tarval *tv)
144 tv_val_b (tarval *tv)
150 /* Overflows `sInt' signed integral `mode'? */
152 sInt_overflow (tarval_sInt sInt, ir_mode *mode)
154 assert (is_Int(get_mode_modecode(mode)));
155 return (get_mode_min(mode) && get_mode_max(mode) /* only valid after firm initialization */
156 && (sInt < tv_val_sInt (get_mode_min(mode))
157 || tv_val_sInt (get_mode_max(mode)) < sInt));
161 /* Overflows `uInt' unsigned integral `mode'? */
163 uInt_overflow (tarval_uInt uInt, ir_mode *mode)
165 assert (is_Int(get_mode_modecode(mode)));
166 return (get_mode_max(mode) /* only valid after firm initialization */
167 && tv_val_uInt (get_mode_max(mode)) < uInt);
173 _tarval_vrfy (const tarval *val)
176 switch (get_mode_modecode(val->mode)) {
183 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
184 assert (!uInt_overflow (val->u.uInt, val->mode)); break;
185 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
186 assert (!sInt_overflow (val->u.sInt, val->mode)); break;
191 assert (val->u.P.ent->kind == k_entity);
192 assert ( val->u.P.xname || val->u.P.ent
193 || !tarval_P_void || (val == tarval_P_void));
196 assert ((unsigned)val->u.b <= 1); break;
198 assert (val->mode == mode_T);
209 pset_stats (tarvals);
215 /* Return the canonical tarval * for tv.
216 May destroy everything allocated on tv_obst after tv! */
218 tarval_identify (tarval *tv)
222 o = pset_insert (tarvals, tv, tarval_hash (tv));
225 obstack_free (&tv_obst, (void *)tv);
233 /* Return 0 iff a equals b. Bitwise identical NaNs compare equal. */
235 tarval_cmp (const void *p, const void *q)
243 if (a == b) return 0;
244 if ((void *)a->mode - (void *)b->mode)
245 return (void *)a->mode - (void *)b->mode;
247 switch (get_mode_modecode(a->mode)) {
250 return memcmp (&a->u.F, &b->u.F, sizeof (a->u.F));
252 return memcmp (&a->u.D, &b->u.D, sizeof (a->u.D));
254 return memcmp (&a->u.E, &b->u.E, sizeof (a->u.E));
256 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
257 if (sizeof (int) == sizeof (tarval_uInt)) {
258 return a->u.uInt - b->u.uInt;
260 return a->u.uInt != b->u.uInt;
262 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
263 if (sizeof (int) == sizeof (tarval_sInt)) {
264 return a->u.sInt - b->u.sInt;
266 return a->u.sInt != b->u.sInt;
268 return a->u.C - b->u.C;
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;
283 tarval_hash (tarval *tv)
287 h = get_mode_modecode(tv->mode) * 0x421u;
288 switch (get_mode_modecode(tv->mode)) {
290 h = 0x94b527ce; break;
293 { union { float f; unsigned u; } u;
294 assert (sizeof (float) <= sizeof (unsigned));
295 u.u = 0; u.f = tv->u.F;
301 { union { double d; unsigned u[2]; } u;
302 assert (sizeof (double) <= 2*sizeof (unsigned));
303 u.u[0] = u.u[1] = 0; u.d = tv->u.D;
304 h ^= u.u[0] ^ u.u[1];
308 { union { long double e; unsigned u[3]; } u;
309 assert (sizeof (long double) <= 3*sizeof (unsigned));
310 u.u[0] = u.u[1] = u.u[2] = 0; u.e = tv->u.E;
311 h ^= u.u[0] ^ u.u[1] ^ u.u[2];
314 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
315 h ^= tv->u.uInt; break;
316 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
317 h ^= tv->u.sInt; break;
322 /* @@@ lower bits not random, watch for collisions; perhaps
323 replace by tv->u.p.ent - (entity *)0 */
324 h ^= ((char *)tv->u.P.ent - (char *)0) / 64;
325 } else if (tv->u.P.xname) {
326 /* Of course, strlen() in a hash function is a mistake, but this
327 case should be really rare. */
328 h ^= ID_HASH (tv->u.P.xname, strlen (tv->u.P.xname));
342 /*** ***************** Initialization ************************************* ***/
347 obstack_init (&tv_obst);
348 obstack_alignment_mask (&tv_obst) = ALIGNOF (tarval) - 1;
349 assert (IS_POW2 (ALIGNOF (tarval)));
351 /* initialize the target value table */
352 tarvals = new_pset (tarval_cmp, TUNE_NCONSTANTS);
359 union ieee754_double x;
361 /* assumed by tarval_hash(): */
362 assert (sizeof (float) * CHAR_BIT == 32);
363 assert (sizeof (double) * CHAR_BIT == 64);
366 /* assumed by tarval_sInt & friends: */
367 assert ( (irm_C == irm_c+1) && (irm_h == irm_C+1)
368 && (irm_H == irm_h+1) && (irm_i == irm_H+1)
369 && (irm_I == irm_i+1) && (irm_l == irm_I+1)
370 && (irm_L == irm_l+1));
372 /* assumed everywhere: */
373 for (i = 0; i <= irm_L-irm_c; i += 2) {
374 assert ( IS_POW2 (max_sInt[i+1]+1)
375 && (min_sInt[i] == -max_sInt[i]-1)
376 && ((tarval_uInt)max_sInt[i+1] == (tarval_uInt)max_sInt[i]-min_sInt[i]));
381 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
383 tarval_bad = tarval_identify (tv);
385 tarval_b_false = tarval_from_long (mode_b, 0);
386 tarval_b_true = tarval_from_long (mode_b, 1);
388 /* IsInf <-> exponent == 0x7ff && ! (bits | fraction_low) */
389 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
392 x.ieee.exponent = 0x7ff;
393 x.ieee.mantissa0 = 0;
394 x.ieee.mantissa1 = 0;
396 tarval_D_Inf = tarval_identify (tv);
398 /* IsNaN <-> exponent==0x7ff && (qnan_bit | bits | fraction_low) */
399 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
401 x.ieee_nan.negative = 0;
402 x.ieee_nan.exponent = 0x7ff;
403 x.ieee_nan.quiet_nan = 1; /* @@@ quiet or signalling? */
404 x.ieee_nan.mantissa0 = 42;
405 x.ieee_nan.mantissa1 = 0;
406 assert(x.d != x.d /* x.d is NaN */);
408 tarval_D_NaN = tarval_identify (tv);
410 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
412 tv->u.P.xname = NULL;
415 tarval_P_void = tarval_identify (tv);
417 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
420 tarval_mode_null [irm_F] = tarval_from_long (mode_F, 0);
421 tarval_mode_null [irm_D] = tarval_from_long (mode_D, 0);
422 tarval_mode_null [irm_E] = tarval_from_long (mode_E, 0);
423 tarval_mode_null [irm_Bs] = tarval_from_long (mode_Bs, 0);
424 tarval_mode_null [irm_Bu] = tarval_from_long (mode_Bu, 0);
425 tarval_mode_null [irm_Hs] = tarval_from_long (mode_Hs, 0);
426 tarval_mode_null [irm_Hu] = tarval_from_long (mode_Hu, 0);
427 tarval_mode_null [irm_Is] = tarval_from_long (mode_Is, 0);
428 tarval_mode_null [irm_Iu] = tarval_from_long (mode_Iu, 0);
429 tarval_mode_null [irm_Ls] = tarval_from_long (mode_Ls, 0);
430 tarval_mode_null [irm_Lu] = tarval_from_long (mode_Lu, 0);
431 tarval_mode_null [irm_C] = tarval_from_long (mode_C, 0);
432 tarval_mode_null [irm_b] = tarval_b_false;
433 tarval_mode_null [irm_P] = tarval_P_void;
437 /*** ********************** Constructors for tarvals ********************** ***/
439 /* copy from src to dst len chars omitting '_'. */
441 stripcpy (char *dst, const char *src, size_t len)
446 if (*src == '_') src++;
449 *d = 0; /* make it 0-terminated. */
455 tarval_F_from_str (const char *s, size_t len)
463 buf = alloca (len+1);
464 stripcpy (buf, s, len);
466 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
468 tv->u.F = (float)strtod (buf, &eptr);
469 assert (eptr == buf+strlen(buf));
471 return tarval_identify (tv);
476 tarval_D_from_str (const char *s, size_t len)
484 buf = alloca (len+1);
485 stripcpy (buf, s, len);
487 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
489 tv->u.D = strtod (buf, &eptr);
490 assert (eptr == buf+strlen(buf));
492 return tarval_identify (tv);
496 tarval_int_from_str (const char *s, size_t len, int base, ir_mode *m)
502 assert (mode_is_int(m));
505 buf = alloca (len+1);
506 stripcpy (buf, s, len);
509 val = strtol(buf, &eptr, base); /* strtoll */
510 assert (eptr == buf+strlen(buf));
511 if ((errno == ERANGE) &&
512 ((m == mode_Ls) || (m == mode_Lu)) ) {
513 printf("WARNING: Constant %s not representable. Continuing with %ld.\n",
517 return tarval_from_long(m, val);
520 /* Create a tarval with mode `m' and value `val' casted to the type that
521 represents such tarvals on host. The resulting value must be legal
524 tarval_from_long (ir_mode *m, long val)
530 if (m == mode_T) return tarval_bad;
532 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
535 switch (get_mode_modecode(m)) {
538 tv->u.F = val; break;
540 tv->u.D = val; break;
542 /* @@@ not yet implemented */
545 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
546 tv->u.uInt = val; break;
548 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
549 tv->u.sInt = val; break;
552 obstack_free (&tv_obst, tv);
553 return tarval_P_void;
555 tv->u.C = val; break;
557 tv->u.b = !!val; /* u.b must be 0 or 1 */
563 return tarval_identify (tv);
568 tarval_P_from_str (const char *xname)
574 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
577 tv->u.P.xname = obstack_copy0 (&tv_obst, xname, strlen (xname));
580 return tarval_identify (tv);
585 tarval_P_from_entity (entity *ent)
591 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
594 tv->u.P.xname = NULL;
597 return tarval_identify (tv);
601 /* Routines for building a tarval step by step follow.
602 Legal calling sequences:
604 No constructors except tarval_append() and tarval_append1 ()
605 tarval_finish_as() or tarval_cancel() */
607 /* Begin building a tarval. */
612 obstack_blank (&tv_obst, sizeof (tarval));
616 /* Append `n' chars from `p' to the tarval currently under construction. */
618 tarval_append (const char *p, size_t n)
621 obstack_grow (&tv_obst, p, n);
625 /* Append `ch' to the tarval currently under construction. */
627 tarval_append1 (char ch)
630 obstack_1grow (&tv_obst, ch);
634 /* Finish the tarval currently under construction and give id mode `m'.
636 Return NULL if the value does not make sense for this mode, this
637 can only happen in mode C. */
639 tarval_finish_as (ir_mode *m)
641 int size = obstack_object_size (&tv_obst) - sizeof (tarval);
644 char ch = 0; /* initialized to shut up gcc */
646 assert (BUILDING && (size >= 0));
648 if (size != 1) return tarval_cancel();
649 p = (unsigned char *)obstack_base (&tv_obst) + sizeof (tarval);
651 obstack_blank (&tv_obst, -size);
653 tv = obstack_finish (&tv_obst);
654 p = (unsigned char *)tv + sizeof (tarval);
657 switch (get_mode_modecode(m)) {
668 return tarval_identify (tv);
672 /* Cancel tarval building and return tarval_bad. */
677 obstack_free (&tv_obst, obstack_finish (&tv_obst));
682 /*** ****************** Arithmethic operations on tarvals ***************** ***/
684 /* Return `src' converted to mode `m' if representable, else NULL.
685 @@@ lots of conversions missing */
687 tarval_convert_to (tarval *src, ir_mode *m)
691 if (m == src->mode) return src;
693 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
696 switch (get_mode_modecode(src->mode)) {
699 if (m != mode_F) goto fail;
703 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
704 switch (get_mode_modecode(m)) {
705 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
706 tv->u.sInt = src->u.sInt;
707 if (sInt_overflow (tv->u.sInt, m)) goto fail;
710 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
711 tv->u.uInt = src->u.sInt;
712 if (uInt_overflow (tv->u.uInt, m)) goto fail;
716 tv->u.b = !!src->u.sInt;
722 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
723 switch (get_mode_modecode(m)) {
724 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
725 tv->u.sInt = src->u.uInt;
726 if (sInt_overflow (tv->u.sInt, m)) goto fail;
729 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
730 tv->u.uInt = src->u.uInt;
731 if (uInt_overflow (tv->u.uInt, m)) goto fail;
735 tv->u.b = !!src->u.uInt;
743 switch (get_mode_modecode(m)) {
744 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
745 tv->u.sInt = src->u.b;
748 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
749 tv->u.uInt = src->u.b;
757 obstack_free (&tv_obst, tv);
761 return tarval_identify (tv);
765 /* GL Why are there no ArmRoq comments, why is this not used? */
767 tarval_neg (tarval *a)
773 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
777 switch (get_mode_modecode(a->mode)) {
779 case irm_F: tv->u.F = -a->u.F; break;
780 case irm_D: tv->u.D = -a->u.D; break;
782 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
783 tv->u.uInt = -a->u.uInt & tv_val_uInt (get_mode_max(a->mode));
786 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
787 tv->u.sInt = -a->u.sInt;
788 if ( sInt_overflow (tv->u.sInt, a->mode)
789 || ((tv->u.sInt >= 0) == (a->u.sInt >= 0))) {
790 obstack_free (&tv_obst, tv);
794 case irm_b: tv->u.b = !a->u.b; break;
798 return tarval_identify (tv);
802 /* Compare `a' with `b'.
803 Return one of irpn_Lt, irpn_Eq, irpn_Gt, irpn_Uo, or irpn_False if
804 result is unknown. */
806 tarval_comp (tarval *a, tarval *b)
812 assert (a->mode == b->mode);
814 switch (get_mode_modecode(a->mode)) {
816 case irm_F: return ( a->u.F == b->u.F ? irpn_Eq
817 : a->u.F > b->u.F ? irpn_Gt
818 : a->u.F < b->u.F ? irpn_Lt
820 case irm_D: return ( a->u.D == b->u.D ? irpn_Eq
821 : a->u.D > b->u.D ? irpn_Gt
822 : a->u.D < b->u.D ? irpn_Lt
824 case irm_E: return ( a->u.E == b-> u.E ? irpn_Eq
825 : a->u.E > b->u.E ? irpn_Gt
826 : a->u.E < b->u.E ? irpn_Lt
829 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
830 return ( a->u.uInt == b->u.uInt ? irpn_Eq
831 : a->u.uInt > b->u.uInt ? irpn_Gt
834 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
835 return ( a->u.sInt == b->u.sInt ? irpn_Eq
836 : a->u.sInt > b->u.sInt ? irpn_Gt
838 case irm_b: return ( a->u.b == b->u.b ? irpn_Eq
839 : a->u.b > b->u.b ? irpn_Gt
841 /* The following assumes that pointers are unsigned, which is valid
842 for all sane CPUs (transputers are insane). */
843 case irm_P: return ( a == b ? irpn_Eq
844 : a == tarval_P_void ? irpn_Lt
845 : b == tarval_P_void ? irpn_Gt
846 : irpn_False); /* unknown */
852 /* Return `a+b' if computable, else NULL. Modes must be equal. */
854 tarval_add (tarval *a, tarval *b)
858 TARVAL_VRFY (a); TARVAL_VRFY (b);
859 assert (a->mode == b->mode);
861 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
865 switch (get_mode_modecode(a->mode)) {
867 case irm_F: tv->u.F = a->u.F + b->u.F; break; /* @@@ overflow etc */
868 case irm_D: tv->u.D = a->u.D + b->u.D; break; /* @@@ dto. */
869 case irm_E: tv->u.E = a->u.E + b->u.E; break; /* @@@ dto. */
871 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
872 tv->u.uInt = (a->u.uInt + b->u.uInt) & tv_val_uInt (get_mode_max(a->mode));
875 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
876 tv->u.sInt = a->u.sInt + b->u.sInt;
877 if ( sInt_overflow (tv->u.sInt, a->mode)
878 || ((tv->u.sInt > a->u.sInt) ^ (b->u.sInt > 0))) {
879 obstack_free (&tv_obst, tv);
883 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
887 return tarval_identify (tv);
891 /* Return `a-b' if computable, else NULL. Modes must be equal. */
893 tarval_sub (tarval *a, tarval *b)
897 TARVAL_VRFY (a); TARVAL_VRFY (b);
898 assert (a->mode == b->mode);
900 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
904 switch (get_mode_modecode(a->mode)) {
906 case irm_F: tv->u.F = a->u.F - b->u.F; break; /* @@@ overflow etc */
907 case irm_D: tv->u.D = a->u.D - b->u.D; break; /* @@@ dto. */
908 case irm_E: tv->u.E = a->u.E - b->u.E; break; /* @@@ dto. */
910 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
911 tv->u.uInt = (a->u.uInt - b->u.uInt) & tv_val_uInt (get_mode_max(a->mode));
914 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
915 tv->u.sInt = a->u.sInt - b->u.sInt;
916 if ( sInt_overflow (tv->u.sInt, a->mode)
917 || ((tv->u.sInt > a->u.sInt) ^ (b->u.sInt < 0))) {
918 obstack_free (&tv_obst, tv);
922 case irm_b: tv->u.b = a->u.b & ~b->u.b; break; /* u.b is in canonical form */
926 return tarval_identify (tv);
929 /* Return `a*b' if computable, else NULL. Modes must be equal. */
931 tarval_mul (tarval *a, tarval *b)
935 TARVAL_VRFY (a); TARVAL_VRFY (b);
936 assert (a->mode == b->mode);
938 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
942 switch (get_mode_modecode(a->mode)) {
944 case irm_F: tv->u.F = a->u.F * b->u.F; break; /* @@@ overflow etc */
945 case irm_D: tv->u.D = a->u.D * b->u.D; break; /* @@@ dto. */
946 case irm_E: tv->u.E = a->u.E * b->u.E; break; /* @@@ dto. */
948 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
949 tv->u.uInt = (a->u.uInt * b->u.uInt) & tv_val_uInt (get_mode_max(a->mode));
952 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
953 tv->u.sInt = a->u.sInt * b->u.sInt;
954 if ( sInt_overflow (tv->u.sInt, a->mode)
955 || (b->u.sInt && (tv->u.sInt / b->u.sInt != a->u.sInt))) {
956 obstack_free (&tv_obst, tv);
960 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
964 return tarval_identify (tv);
968 /* Return floating-point `a/b' if computable, else NULL.
969 Modes must be equal, non-floating-point operands are converted to irm_D. */
971 tarval_quo (tarval *a, tarval *b)
975 TARVAL_VRFY (a); TARVAL_VRFY (b);
976 assert (a->mode == b->mode);
978 switch (get_mode_modecode(a->mode)) {
981 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
983 tv->u.F = a->u.F / b->u.F; /* @@@ overflow etc */
986 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
988 tv->u.D = a->u.D / b->u.D; /* @@@ overflow etc */
991 a = tarval_convert_to (a, mode_D);
992 b = tarval_convert_to (b, mode_D);
993 return a && b ? tarval_quo (a, b) : NULL;
996 return tarval_identify (tv);
1000 /* Return `a/b' if computable, else NULL. Modes must be equal. */
1002 tarval_div (tarval *a, tarval *b)
1006 TARVAL_VRFY (a); TARVAL_VRFY (b);
1007 assert (a->mode == b->mode);
1009 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1013 switch (get_mode_modecode(a->mode)) {
1015 case irm_F: tv->u.F = floor (a->u.F / b->u.F); break; /* @@@ overflow etc */
1016 case irm_D: tv->u.D = floor (a->u.D / b->u.D); break; /* @@@ dto. */
1017 case irm_E: tv->u.E = floor (a->u.E / b->u.E); break; /* @@@ dto. */
1019 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1020 if (!b->u.uInt) goto fail;
1021 tv->u.uInt = a->u.uInt / b->u.uInt;
1024 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1026 || ((b->u.sInt == -1) && (a->u.sInt == tv_val_sInt (get_mode_max(a->mode)) ))) {
1028 obstack_free (&tv_obst, tv);
1031 tv->u.sInt = a->u.sInt / b->u.sInt;
1033 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1037 return tarval_identify (tv);
1041 /* Return `a%b' if computable, else NULL. Modes must be equal. */
1043 tarval_mod (tarval *a, tarval *b)
1047 TARVAL_VRFY (a); TARVAL_VRFY (b);
1048 assert (a->mode == b->mode);
1050 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1054 switch (get_mode_modecode(a->mode)) {
1056 case irm_F: tv->u.F = fmod (a->u.F, b->u.F); break; /* @@@ overflow etc */
1057 case irm_D: tv->u.D = fmod (a->u.D, b->u.D); break; /* @@@ dto */
1058 case irm_E: tv->u.E = fmod (a->u.E, b->u.E); break; /* @@@ dto. */
1060 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1061 if (!b->u.uInt) goto fail;
1062 tv->u.uInt = a->u.uInt % b->u.uInt;
1065 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1068 obstack_free (&tv_obst, tv);
1071 tv->u.sInt = a->u.sInt % b->u.sInt;
1073 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1077 return tarval_identify (tv);
1080 /* Return |a| if computable, else Null. */
1081 /* is -max == min?? */
1083 tarval_abs (tarval *a) {
1085 if (tv_is_negative(a)) return tarval_neg(a);
1090 tv_is_negative(tarval *a) {
1092 switch (get_mode_modecode(a->mode)) {
1094 case irm_F: return (a->u.F<0);
1095 case irm_D: return (a->u.D<0);
1096 case irm_E: return (a->u.E<0);
1098 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1102 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1103 return (a->u.sInt < 0);
1113 /* Return `a&b'. Modes must be equal. */
1115 tarval_and (tarval *a, tarval *b)
1119 TARVAL_VRFY (a); TARVAL_VRFY (b);
1120 assert (a->mode == b->mode);
1122 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1126 switch (get_mode_modecode(a->mode)) {
1128 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1129 tv->u.uInt = a->u.uInt & b->u.uInt; break;
1131 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1132 tv->u.sInt = a->u.sInt & b->u.sInt; break;
1133 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1137 return tarval_identify (tv);
1141 /* Return `a|b'. Modes must be equal. */
1143 tarval_or (tarval *a, tarval *b)
1147 TARVAL_VRFY (a); TARVAL_VRFY (b);
1148 assert (a->mode == b->mode);
1150 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1154 switch (get_mode_modecode(a->mode)) {
1156 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1157 tv->u.uInt = a->u.uInt | b->u.uInt; break;
1159 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1160 tv->u.sInt = a->u.sInt | b->u.sInt; break;
1161 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1165 return tarval_identify (tv);
1169 /* Return `a^b'. Modes must be equal. */
1171 tarval_eor (tarval *a, tarval *b)
1175 TARVAL_VRFY (a); TARVAL_VRFY (b);
1176 assert (a->mode == b->mode);
1178 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1182 switch (get_mode_modecode(a->mode)) {
1184 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1185 tv->u.uInt = a->u.uInt ^ b->u.uInt; break;
1187 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1188 tv->u.sInt = a->u.sInt ^ b->u.sInt; break;
1189 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1193 return tarval_identify (tv);
1197 /* Return `a<<b' if computable, else NULL. */
1199 tarval_shl (tarval *a, tarval *b)
1205 TARVAL_VRFY (a); TARVAL_VRFY (b);
1207 shift = tarval_ord (b, &b_is_huge);
1210 || ((shift >= get_mode_size(mode_Ls)*target_bits))) {
1214 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1217 switch (get_mode_modecode(a->mode)) {
1219 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1220 tv->u.uInt = a->u.uInt << shift;
1223 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1224 tv->u.sInt = a->u.sInt << shift;
1226 default: assert (0);
1229 return tarval_identify (tv);
1233 /* Return `a>>b' if computable, else NULL.
1234 The interpretation of >> (sign extended or not) is implementaion
1235 dependent, i.e. this is neither shr nor shrs!! */
1237 tarval_shr (tarval *a, tarval *b)
1243 TARVAL_VRFY (a); TARVAL_VRFY (b);
1245 shift = tarval_ord (b, &b_is_huge);
1248 || ((shift >= get_mode_size(mode_Ls)*target_bits))) {
1252 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1255 switch (get_mode_modecode(a->mode)) {
1257 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1258 tv->u.uInt = a->u.uInt >> shift;
1261 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1262 tv->u.sInt = a->u.sInt >> shift;
1264 default: assert (0);
1267 return tarval_identify (tv);
1271 /* Classify `tv', which may be NULL.
1272 Return 0 if `tv' is the additive neutral element, 1 if `tv' is the
1273 multiplicative neutral element, and -1 if `tv' is the neutral
1274 element of bitwise and. */
1276 tarval_classify (tarval *tv)
1282 switch (get_mode_modecode(tv->mode)) {
1284 case irm_F: case irm_D: case irm_E:
1288 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Bu))) - 1;
1290 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Hu))) - 1;
1292 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Iu))) - 1;
1294 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Lu))) - 1;
1296 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1305 /* Convert `tv' into type `long', set `fail' if not representable.
1306 If `fail' gets set for an unsigned `tv', the correct result can be
1307 obtained by casting the result to `unsigned long'. */
1309 tarval_ord (tarval *tv, int *fail)
1313 switch (get_mode_modecode(tv->mode)) {
1315 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1316 *fail = tv->u.uInt > tv_val_uInt (get_mode_max(mode_Ls));
1319 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1332 tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
1334 tarval *val = XP_GETARG (tarval *, 0);
1340 switch (get_mode_modecode(val->mode)) {
1342 case irm_T: /* none */
1343 printed = XPSR ("<bad>");
1346 case irm_F: /* float */
1347 sprintf (buf, "%1.9e", (float)(val->u.F));
1348 printed = XPF1R ("%s", buf);
1350 case irm_D: /* double */
1351 printed = XPF1R ("%1.30g", (double)(val->u.D));
1354 case irm_C: /* character */
1355 if ((isprint (val->u.C)) &&
1356 (val->u.C != '\\') && (val->u.C != '\'')) {
1357 printed = XPF1R ("'%c'", val->u.C);
1359 printed = XPF1R ("0x%x", (unsigned long)val->u.C);
1363 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls: /* signed num */
1364 printed = XPF1R ("%ld", (long)val->u.sInt);
1366 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu: /* unsigned num */
1367 printed = XPF1R ("%lu", (unsigned long)val->u.uInt);
1370 case irm_P: /* pointer */
1371 if (val->u.P.xname) {
1372 printed = XPR (val->u.P.xname);
1373 } else if (val->u.P.ent) {
1374 if (get_entity_peculiarity(val->u.P.ent) == existent)
1375 printed = XPF1R ("&(%I)", get_entity_ld_ident(val->u.P.ent));
1377 printed = XPSR ("(NULL)");
1379 assert (val == tarval_P_void);
1380 printed = XPSR ("(void)");
1384 case irm_b: /* boolean */
1385 if (val->u.b) printed = XPSR ("true");
1386 else printed = XPSR ("false");
1389 case irm_M: /* memory */
1390 case irm_BB: /* region */
1400 get_tv_mode (tarval *tv)
1405 /* Returns the entity if the tv is a pointer to an entity, else
1407 entity *get_tv_entity(tarval *tv) {
1410 if (tv->mode == mode_P) {
1411 if (tv->u.P.xname) {
1414 } else if (tv->u.P.ent) {