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"
51 static struct obstack tv_obst; /* obstack for all the target values */
52 static pset *tarvals; /* pset containing pointers to _all_ tarvals */
54 /* currently building an object with tarval_start() & friends ? */
55 #define BUILDING obstack_object_size (&tv_obst)
57 /* bcopy is not ISO C */
58 #define bcopy(X, Y, Z) memcpy((Y), (X), (Z))
61 /* special tarvals: */
63 tarval *tarval_b_false;
64 tarval *tarval_b_true;
67 tarval *tarval_P_void;
68 tarval *tarval_mode_null[irm_max];
69 tarval *tarval_mode_min[irm_max];
70 tarval *tarval_mode_max[irm_max];
72 tarval *get_tarval_bad () { return tarval_bad; }
73 tarval *get_tarval_b_false () { return tarval_b_false; }
74 tarval *get_tarval_b_true () { return tarval_b_true; }
75 tarval *get_tarval_D_NaN () { return tarval_D_NaN; }
76 tarval *get_tarval_D_Inf () { return tarval_D_Inf; }
77 tarval *get_tarval_P_void () { return tarval_P_void; }
78 tarval *get_tarval_mode_null(ir_mode *mode)
79 { return tarval_mode_null[get_mode_modecode(mode)]; }
80 tarval *get_tarval_mode_min (ir_mode *mode)
81 { return tarval_mode_min[get_mode_modecode(mode)]; }
82 tarval *get_tarval_mode_max (ir_mode *mode)
83 { return tarval_mode_max[get_mode_modecode(mode)]; }
86 /* @@@ depends on order of ir_mode */
87 static tarval_sInt min_sInt[8] = {
93 static tarval_sInt max_sInt[8] = {
94 TARGET_SIMAX (c), TARGET_UIMAX (C),
95 TARGET_SIMAX (h), TARGET_UIMAX (H),
96 TARGET_SIMAX (i), TARGET_UIMAX (I),
97 TARGET_SIMAX (l), TARGET_UIMAX (L)
101 /* Used to be in irmode.h, replaced now. */
102 # define is_Int(m) ((m) <= irm_Lu && (m) >= irm_Bs) /* old */
104 /* return a mode-specific value */
107 tv_val_F (tarval *tv)
113 tv_val_D (tarval *tv)
119 tv_val_E (tarval *tv)
125 tv_val_sInt (tarval *tv)
131 tv_val_uInt (tarval *tv)
137 tv_val_P (tarval *tv)
143 tv_val_b (tarval *tv)
149 /* Overflows `sInt' signed integral `mode'? */
151 sInt_overflow (tarval_sInt sInt, ir_mode *mode)
153 assert (is_Int(get_mode_modecode(mode)));
154 return (get_mode_min(mode) && get_mode_max(mode) /* only valid after firm initialization */
155 && (sInt < tv_val_sInt (get_mode_min(mode))
156 || tv_val_sInt (get_mode_max(mode)) < sInt));
160 /* Overflows `uInt' unsigned integral `mode'? */
162 uInt_overflow (tarval_uInt uInt, ir_mode *mode)
164 assert (is_Int(get_mode_modecode(mode)));
165 return (get_mode_max(mode) /* only valid after firm initialization */
166 && tv_val_uInt (get_mode_max(mode)) < uInt);
172 _tarval_vrfy (const tarval *val)
175 switch (get_mode_modecode(val->mode)) {
182 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
183 assert (!uInt_overflow (val->u.uInt, val->mode)); break;
184 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
185 assert (!sInt_overflow (val->u.sInt, 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));
195 assert ((unsigned)val->u.b <= 1); break;
197 assert (val->mode == mode_T);
208 pset_stats (tarvals);
214 /* Return the canonical tarval * for tv.
215 May destroy everything allocated on tv_obst after tv! */
217 tarval_identify (tarval *tv)
221 o = pset_insert (tarvals, tv, tarval_hash (tv));
224 obstack_free (&tv_obst, (void *)tv);
232 /* Return 0 iff a equals b. Bitwise identical NaNs compare equal. */
234 tarval_cmp (const void *p, const void *q)
242 if (a == b) return 0;
243 if ((void *)a->mode - (void *)b->mode)
244 return (void *)a->mode - (void *)b->mode;
246 switch (get_mode_modecode(a->mode)) {
249 return memcmp (&a->u.F, &b->u.F, sizeof (a->u.F));
251 return memcmp (&a->u.D, &b->u.D, sizeof (a->u.D));
253 return memcmp (&a->u.E, &b->u.E, sizeof (a->u.E));
255 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
256 if (sizeof (int) == sizeof (tarval_uInt)) {
257 return a->u.uInt - b->u.uInt;
259 return a->u.uInt != b->u.uInt;
261 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
262 if (sizeof (int) == sizeof (tarval_sInt)) {
263 return a->u.sInt - b->u.sInt;
265 return a->u.sInt != b->u.sInt;
267 return a->u.C - b->u.C;
269 if (a->u.P.ent || b->u.P.ent)
270 return (char *)a->u.P.ent - (char *)b->u.P.ent;
271 if (a->u.P.xname && b->u.P.xname)
272 return strcmp (a->u.P.xname, b->u.P.xname);
273 return a->u.P.xname - b->u.P.xname;
275 return a->u.b - b->u.b;
282 tarval_hash (tarval *tv)
286 h = get_mode_modecode(tv->mode) * 0x421u;
287 switch (get_mode_modecode(tv->mode)) {
289 h = 0x94b527ce; break;
292 { union { float f; unsigned u; } u;
293 assert (sizeof (float) <= sizeof (unsigned));
294 u.u = 0; u.f = tv->u.F;
300 { union { double d; unsigned u[2]; } u;
301 assert (sizeof (double) <= 2*sizeof (unsigned));
302 u.u[0] = u.u[1] = 0; u.d = tv->u.D;
303 h ^= u.u[0] ^ u.u[1];
307 { union { long double e; unsigned u[3]; } u;
308 assert (sizeof (long double) <= 3*sizeof (unsigned));
309 u.u[0] = u.u[1] = u.u[2] = 0; u.e = tv->u.E;
310 h ^= u.u[0] ^ u.u[1] ^ u.u[2];
313 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
314 h ^= tv->u.uInt; break;
315 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
316 h ^= tv->u.sInt; break;
321 /* @@@ lower bits not random, watch for collisions; perhaps
322 replace by tv->u.p.ent - (entity *)0 */
323 h ^= ((char *)tv->u.P.ent - (char *)0) / 64;
324 } else if (tv->u.P.xname) {
325 /* Of course, strlen() in a hash function is a mistake, but this
326 case should be really rare. */
327 h ^= ID_HASH (tv->u.P.xname, strlen (tv->u.P.xname));
341 /*** ***************** Initialization ************************************* ***/
346 obstack_init (&tv_obst);
347 obstack_alignment_mask (&tv_obst) = ALIGNOF (tarval) - 1;
348 assert (IS_POW2 (ALIGNOF (tarval)));
350 /* initialize the target value table */
351 tarvals = new_pset (tarval_cmp, TUNE_NCONSTANTS);
358 union ieee754_double x;
360 /* assumed by tarval_hash(): */
361 assert (sizeof (float) * CHAR_BIT == 32);
362 assert (sizeof (double) * CHAR_BIT == 64);
365 /* assumed by tarval_sInt & friends: */
366 assert ( (irm_C == irm_c+1) && (irm_h == irm_C+1)
367 && (irm_H == irm_h+1) && (irm_i == irm_H+1)
368 && (irm_I == irm_i+1) && (irm_l == irm_I+1)
369 && (irm_L == irm_l+1));
371 /* assumed everywhere: */
372 for (i = 0; i <= irm_L-irm_c; i += 2) {
373 assert ( IS_POW2 (max_sInt[i+1]+1)
374 && (min_sInt[i] == -max_sInt[i]-1)
375 && ((tarval_uInt)max_sInt[i+1] == (tarval_uInt)max_sInt[i]-min_sInt[i]));
380 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
382 tarval_bad = tarval_identify (tv);
384 tarval_b_false = tarval_from_long (mode_b, 0);
385 tarval_b_true = tarval_from_long (mode_b, 1);
387 /* IsInf <-> exponent == 0x7ff && ! (bits | fraction_low) */
388 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
391 x.ieee.exponent = 0x7ff;
392 x.ieee.mantissa0 = 0;
393 x.ieee.mantissa1 = 0;
395 tarval_D_Inf = tarval_identify (tv);
397 /* IsNaN <-> exponent==0x7ff && (qnan_bit | bits | fraction_low) */
398 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
400 x.ieee_nan.negative = 0;
401 x.ieee_nan.exponent = 0x7ff;
402 x.ieee_nan.quiet_nan = 1; /* @@@ quiet or signalling? */
403 x.ieee_nan.mantissa0 = 42;
404 x.ieee_nan.mantissa1 = 0;
405 assert(x.d != x.d /* x.d is NaN */);
407 tarval_D_NaN = tarval_identify (tv);
409 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
411 tv->u.P.xname = NULL;
414 tarval_P_void = tarval_identify (tv);
416 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
419 tarval_mode_null [irm_F] = tarval_from_long (mode_F, 0);
420 tarval_mode_null [irm_D] = tarval_from_long (mode_D, 0);
421 tarval_mode_null [irm_E] = tarval_from_long (mode_E, 0);
422 tarval_mode_null [irm_Bs] = tarval_from_long (mode_Bs, 0);
423 tarval_mode_null [irm_Bu] = tarval_from_long (mode_Bu, 0);
424 tarval_mode_null [irm_Hs] = tarval_from_long (mode_Hs, 0);
425 tarval_mode_null [irm_Hu] = tarval_from_long (mode_Hu, 0);
426 tarval_mode_null [irm_Is] = tarval_from_long (mode_Is, 0);
427 tarval_mode_null [irm_Iu] = tarval_from_long (mode_Iu, 0);
428 tarval_mode_null [irm_Ls] = tarval_from_long (mode_Ls, 0);
429 tarval_mode_null [irm_Lu] = tarval_from_long (mode_Lu, 0);
430 tarval_mode_null [irm_C] = tarval_from_long (mode_C, 0);
431 tarval_mode_null [irm_b] = tarval_b_false;
432 tarval_mode_null [irm_P] = tarval_P_void;
436 /*** ********************** Constructors for tarvals ********************** ***/
438 /* copy from src to dst len chars omitting '_'. */
440 stripcpy (char *dst, const char *src, size_t len)
445 if (*src == '_') src++;
448 *d = 0; /* make it 0-terminated. */
454 tarval_F_from_str (const char *s, size_t len)
462 buf = alloca (len+1);
463 stripcpy (buf, s, len);
465 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
467 tv->u.F = (float)strtod (buf, &eptr);
468 assert (eptr == buf+strlen(buf));
470 return tarval_identify (tv);
475 tarval_D_from_str (const char *s, size_t len)
483 buf = alloca (len+1);
484 stripcpy (buf, s, len);
486 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
488 tv->u.D = strtod (buf, &eptr);
489 assert (eptr == buf+strlen(buf));
491 return tarval_identify (tv);
495 tarval_int_from_str (const char *s, size_t len, int base, ir_mode *m)
501 assert (mode_is_int(m));
504 buf = alloca (len+1);
505 stripcpy (buf, s, len);
508 val = strtol(buf, &eptr, base); /* strtoll */
509 assert (eptr == buf+strlen(buf));
510 if ((errno == ERANGE) &&
511 ((m == mode_Ls) || (m == mode_Lu)) ) {
512 printf("WARNING: Constant %s not representable. Continuing with %ld.\n",
516 return tarval_from_long(m, val);
519 /* Create a tarval with mode `m' and value `val' casted to the type that
520 represents such tarvals on host. The resulting value must be legal
523 tarval_from_long (ir_mode *m, long val)
529 if (m == mode_T) return tarval_bad;
531 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
534 switch (get_mode_modecode(m)) {
537 tv->u.F = val; break;
539 tv->u.D = val; break;
541 /* @@@ not yet implemented */
544 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
545 tv->u.uInt = val; break;
547 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
548 tv->u.sInt = val; break;
551 obstack_free (&tv_obst, tv);
552 return tarval_P_void;
554 tv->u.C = val; break;
556 tv->u.b = !!val; /* u.b must be 0 or 1 */
562 return tarval_identify (tv);
567 tarval_P_from_str (const char *xname)
573 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
576 tv->u.P.xname = obstack_copy0 (&tv_obst, xname, strlen (xname));
579 return tarval_identify (tv);
584 tarval_P_from_entity (entity *ent)
590 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
593 tv->u.P.xname = NULL;
596 return tarval_identify (tv);
600 /* Routines for building a tarval step by step follow.
601 Legal calling sequences:
603 No constructors except tarval_append() and tarval_append1 ()
604 tarval_finish_as() or tarval_cancel() */
606 /* Begin building a tarval. */
611 obstack_blank (&tv_obst, sizeof (tarval));
615 /* Append `n' chars from `p' to the tarval currently under construction. */
617 tarval_append (const char *p, size_t n)
620 obstack_grow (&tv_obst, p, n);
624 /* Append `ch' to the tarval currently under construction. */
626 tarval_append1 (char ch)
629 obstack_1grow (&tv_obst, ch);
633 /* Finish the tarval currently under construction and give id mode `m'.
635 Return NULL if the value does not make sense for this mode, this
636 can only happen in mode C. */
638 tarval_finish_as (ir_mode *m)
640 int size = obstack_object_size (&tv_obst) - sizeof (tarval);
643 char ch = 0; /* initialized to shut up gcc */
645 assert (BUILDING && (size >= 0));
647 if (size != 1) return tarval_cancel();
648 p = (unsigned char *)obstack_base (&tv_obst) + sizeof (tarval);
650 obstack_blank (&tv_obst, -size);
652 tv = obstack_finish (&tv_obst);
653 p = (unsigned char *)tv + sizeof (tarval);
656 switch (get_mode_modecode(m)) {
667 return tarval_identify (tv);
671 /* Cancel tarval building and return tarval_bad. */
676 obstack_free (&tv_obst, obstack_finish (&tv_obst));
681 /*** ****************** Arithmethic operations on tarvals ***************** ***/
683 /* Return `src' converted to mode `m' if representable, else NULL.
684 @@@ lots of conversions missing */
686 tarval_convert_to (tarval *src, ir_mode *m)
690 if (m == src->mode) return src;
692 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
695 switch (get_mode_modecode(src->mode)) {
698 if (m != mode_F) goto fail;
702 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
703 switch (get_mode_modecode(m)) {
704 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
705 tv->u.sInt = src->u.sInt;
706 if (sInt_overflow (tv->u.sInt, m)) goto fail;
709 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
710 tv->u.uInt = src->u.sInt;
711 if (uInt_overflow (tv->u.uInt, m)) goto fail;
715 tv->u.b = !!src->u.sInt;
721 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
722 switch (get_mode_modecode(m)) {
723 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
724 tv->u.sInt = src->u.uInt;
725 if (sInt_overflow (tv->u.sInt, m)) goto fail;
728 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
729 tv->u.uInt = src->u.uInt;
730 if (uInt_overflow (tv->u.uInt, m)) goto fail;
734 tv->u.b = !!src->u.uInt;
742 switch (get_mode_modecode(m)) {
743 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
744 tv->u.sInt = src->u.b;
747 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
748 tv->u.uInt = src->u.b;
756 obstack_free (&tv_obst, tv);
760 return tarval_identify (tv);
764 /* GL Why are there no ArmRoq comments, why is this not used? */
766 tarval_neg (tarval *a)
772 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
776 switch (get_mode_modecode(a->mode)) {
778 case irm_F: tv->u.F = -a->u.F; break;
779 case irm_D: tv->u.D = -a->u.D; break;
781 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
782 tv->u.uInt = -a->u.uInt & tv_val_uInt (get_mode_max(a->mode));
785 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
786 tv->u.sInt = -a->u.sInt;
787 if ( sInt_overflow (tv->u.sInt, a->mode)
788 || ((tv->u.sInt >= 0) == (a->u.sInt >= 0))) {
789 obstack_free (&tv_obst, tv);
793 case irm_b: tv->u.b = !a->u.b; break;
797 return tarval_identify (tv);
801 /* Compare `a' with `b'.
802 Return one of irpn_Lt, irpn_Eq, irpn_Gt, irpn_Uo, or irpn_False if
803 result is unknown. */
805 tarval_comp (tarval *a, tarval *b)
811 assert (a->mode == b->mode);
813 switch (get_mode_modecode(a->mode)) {
815 case irm_F: return ( a->u.F == b->u.F ? irpn_Eq
816 : a->u.F > b->u.F ? irpn_Gt
817 : a->u.F < b->u.F ? irpn_Lt
819 case irm_D: return ( a->u.D == b->u.D ? irpn_Eq
820 : a->u.D > b->u.D ? irpn_Gt
821 : a->u.D < b->u.D ? irpn_Lt
823 case irm_E: return ( a->u.E == b-> u.E ? irpn_Eq
824 : a->u.E > b->u.E ? irpn_Gt
825 : a->u.E < b->u.E ? irpn_Lt
828 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
829 return ( a->u.uInt == b->u.uInt ? irpn_Eq
830 : a->u.uInt > b->u.uInt ? irpn_Gt
833 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
834 return ( a->u.sInt == b->u.sInt ? irpn_Eq
835 : a->u.sInt > b->u.sInt ? irpn_Gt
837 case irm_b: return ( a->u.b == b->u.b ? irpn_Eq
838 : a->u.b > b->u.b ? irpn_Gt
840 /* The following assumes that pointers are unsigned, which is valid
841 for all sane CPUs (transputers are insane). */
842 case irm_P: return ( a == b ? irpn_Eq
843 : a == tarval_P_void ? irpn_Lt
844 : b == tarval_P_void ? irpn_Gt
845 : irpn_False); /* unknown */
851 /* Return `a+b' if computable, else NULL. Modes must be equal. */
853 tarval_add (tarval *a, tarval *b)
857 TARVAL_VRFY (a); TARVAL_VRFY (b);
858 assert (a->mode == b->mode);
860 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
864 switch (get_mode_modecode(a->mode)) {
866 case irm_F: tv->u.F = a->u.F + b->u.F; break; /* @@@ overflow etc */
867 case irm_D: tv->u.D = a->u.D + b->u.D; break; /* @@@ dto. */
868 case irm_E: tv->u.E = a->u.E + b->u.E; break; /* @@@ dto. */
870 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
871 tv->u.uInt = (a->u.uInt + b->u.uInt) & tv_val_uInt (get_mode_max(a->mode));
874 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
875 tv->u.sInt = a->u.sInt + b->u.sInt;
876 if ( sInt_overflow (tv->u.sInt, a->mode)
877 || ((tv->u.sInt > a->u.sInt) ^ (b->u.sInt > 0))) {
878 obstack_free (&tv_obst, tv);
882 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
886 return tarval_identify (tv);
890 /* Return `a-b' if computable, else NULL. Modes must be equal. */
892 tarval_sub (tarval *a, tarval *b)
896 TARVAL_VRFY (a); TARVAL_VRFY (b);
897 assert (a->mode == b->mode);
899 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
903 switch (get_mode_modecode(a->mode)) {
905 case irm_F: tv->u.F = a->u.F - b->u.F; break; /* @@@ overflow etc */
906 case irm_D: tv->u.D = a->u.D - b->u.D; break; /* @@@ dto. */
907 case irm_E: tv->u.E = a->u.E - b->u.E; break; /* @@@ dto. */
909 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
910 tv->u.uInt = (a->u.uInt - b->u.uInt) & tv_val_uInt (get_mode_max(a->mode));
913 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
914 tv->u.sInt = a->u.sInt - b->u.sInt;
915 if ( sInt_overflow (tv->u.sInt, a->mode)
916 || ((tv->u.sInt > a->u.sInt) ^ (b->u.sInt < 0))) {
917 obstack_free (&tv_obst, tv);
921 case irm_b: tv->u.b = a->u.b & ~b->u.b; break; /* u.b is in canonical form */
925 return tarval_identify (tv);
928 /* Return `a*b' if computable, else NULL. Modes must be equal. */
930 tarval_mul (tarval *a, tarval *b)
934 TARVAL_VRFY (a); TARVAL_VRFY (b);
935 assert (a->mode == b->mode);
937 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
941 switch (get_mode_modecode(a->mode)) {
943 case irm_F: tv->u.F = a->u.F * b->u.F; break; /* @@@ overflow etc */
944 case irm_D: tv->u.D = a->u.D * b->u.D; break; /* @@@ dto. */
945 case irm_E: tv->u.E = a->u.E * b->u.E; break; /* @@@ dto. */
947 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
948 tv->u.uInt = (a->u.uInt * b->u.uInt) & tv_val_uInt (get_mode_max(a->mode));
951 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
952 tv->u.sInt = a->u.sInt * b->u.sInt;
953 if ( sInt_overflow (tv->u.sInt, a->mode)
954 || (b->u.sInt && (tv->u.sInt / b->u.sInt != a->u.sInt))) {
955 obstack_free (&tv_obst, tv);
959 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
963 return tarval_identify (tv);
967 /* Return floating-point `a/b' if computable, else NULL.
968 Modes must be equal, non-floating-point operands are converted to irm_D. */
970 tarval_quo (tarval *a, tarval *b)
974 TARVAL_VRFY (a); TARVAL_VRFY (b);
975 assert (a->mode == b->mode);
977 switch (get_mode_modecode(a->mode)) {
980 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
982 tv->u.F = a->u.F / b->u.F; /* @@@ overflow etc */
985 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
987 tv->u.D = a->u.D / b->u.D; /* @@@ overflow etc */
990 a = tarval_convert_to (a, mode_D);
991 b = tarval_convert_to (b, mode_D);
992 return a && b ? tarval_quo (a, b) : NULL;
995 return tarval_identify (tv);
999 /* Return `a/b' if computable, else NULL. Modes must be equal. */
1001 tarval_div (tarval *a, tarval *b)
1005 TARVAL_VRFY (a); TARVAL_VRFY (b);
1006 assert (a->mode == b->mode);
1008 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1012 switch (get_mode_modecode(a->mode)) {
1014 case irm_F: tv->u.F = floor (a->u.F / b->u.F); break; /* @@@ overflow etc */
1015 case irm_D: tv->u.D = floor (a->u.D / b->u.D); break; /* @@@ dto. */
1016 case irm_E: tv->u.E = floor (a->u.E / b->u.E); break; /* @@@ dto. */
1018 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1019 if (!b->u.uInt) goto fail;
1020 tv->u.uInt = a->u.uInt / b->u.uInt;
1023 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1025 || ((b->u.sInt == -1) && (a->u.sInt == tv_val_sInt (get_mode_max(a->mode)) ))) {
1027 obstack_free (&tv_obst, tv);
1030 tv->u.sInt = a->u.sInt / b->u.sInt;
1032 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1036 return tarval_identify (tv);
1040 /* Return `a%b' if computable, else NULL. Modes must be equal. */
1042 tarval_mod (tarval *a, tarval *b)
1046 TARVAL_VRFY (a); TARVAL_VRFY (b);
1047 assert (a->mode == b->mode);
1049 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1053 switch (get_mode_modecode(a->mode)) {
1055 case irm_F: tv->u.F = fmod (a->u.F, b->u.F); break; /* @@@ overflow etc */
1056 case irm_D: tv->u.D = fmod (a->u.D, b->u.D); break; /* @@@ dto */
1057 case irm_E: tv->u.E = fmod (a->u.E, b->u.E); break; /* @@@ dto. */
1059 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1060 if (!b->u.uInt) goto fail;
1061 tv->u.uInt = a->u.uInt % b->u.uInt;
1064 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1067 obstack_free (&tv_obst, tv);
1070 tv->u.sInt = a->u.sInt % b->u.sInt;
1072 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1076 return tarval_identify (tv);
1079 /* Return |a| if computable, else Null. */
1080 /* is -max == min?? */
1082 tarval_abs (tarval *a) {
1084 if (tv_is_negative(a)) return tarval_neg(a);
1089 tv_is_negative(tarval *a) {
1091 switch (get_mode_modecode(a->mode)) {
1093 case irm_F: return (a->u.F<0);
1094 case irm_D: return (a->u.D<0);
1095 case irm_E: return (a->u.E<0);
1097 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1101 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1102 return (a->u.sInt < 0);
1112 /* Return `a&b'. Modes must be equal. */
1114 tarval_and (tarval *a, tarval *b)
1118 TARVAL_VRFY (a); TARVAL_VRFY (b);
1119 assert (a->mode == b->mode);
1121 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1125 switch (get_mode_modecode(a->mode)) {
1127 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1128 tv->u.uInt = a->u.uInt & b->u.uInt; break;
1130 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1131 tv->u.sInt = a->u.sInt & b->u.sInt; break;
1132 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1136 return tarval_identify (tv);
1140 /* Return `a|b'. Modes must be equal. */
1142 tarval_or (tarval *a, tarval *b)
1146 TARVAL_VRFY (a); TARVAL_VRFY (b);
1147 assert (a->mode == b->mode);
1149 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1153 switch (get_mode_modecode(a->mode)) {
1155 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1156 tv->u.uInt = a->u.uInt | b->u.uInt; break;
1158 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1159 tv->u.sInt = a->u.sInt | b->u.sInt; break;
1160 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1164 return tarval_identify (tv);
1168 /* Return `a^b'. Modes must be equal. */
1170 tarval_eor (tarval *a, tarval *b)
1174 TARVAL_VRFY (a); TARVAL_VRFY (b);
1175 assert (a->mode == b->mode);
1177 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1181 switch (get_mode_modecode(a->mode)) {
1183 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1184 tv->u.uInt = a->u.uInt ^ b->u.uInt; break;
1186 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1187 tv->u.sInt = a->u.sInt ^ b->u.sInt; break;
1188 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1192 return tarval_identify (tv);
1196 /* Return `a<<b' if computable, else NULL. */
1198 tarval_shl (tarval *a, tarval *b)
1204 TARVAL_VRFY (a); TARVAL_VRFY (b);
1206 shift = tarval_ord (b, &b_is_huge);
1209 || ((shift >= get_mode_size(mode_Ls)*target_bits))) {
1213 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1216 switch (get_mode_modecode(a->mode)) {
1218 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1219 tv->u.uInt = a->u.uInt << shift;
1222 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1223 tv->u.sInt = a->u.sInt << shift;
1225 default: assert (0);
1228 return tarval_identify (tv);
1232 /* Return `a>>b' if computable, else NULL.
1233 The interpretation of >> (sign extended or not) is implementaion
1234 dependent, i.e. this is neither shr nor shrs!! */
1236 tarval_shr (tarval *a, tarval *b)
1242 TARVAL_VRFY (a); TARVAL_VRFY (b);
1244 shift = tarval_ord (b, &b_is_huge);
1247 || ((shift >= get_mode_size(mode_Ls)*target_bits))) {
1251 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1254 switch (get_mode_modecode(a->mode)) {
1256 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1257 tv->u.uInt = a->u.uInt >> shift;
1260 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1261 tv->u.sInt = a->u.sInt >> shift;
1263 default: assert (0);
1266 return tarval_identify (tv);
1270 /* Classify `tv', which may be NULL.
1271 Return 0 if `tv' is the additive neutral element, 1 if `tv' is the
1272 multiplicative neutral element, and -1 if `tv' is the neutral
1273 element of bitwise and. */
1275 tarval_classify (tarval *tv)
1281 switch (get_mode_modecode(tv->mode)) {
1283 case irm_F: case irm_D: case irm_E:
1287 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Bu))) - 1;
1289 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Hu))) - 1;
1291 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Iu))) - 1;
1293 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Lu))) - 1;
1295 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1304 /* Convert `tv' into type `long', set `fail' if not representable.
1305 If `fail' gets set for an unsigned `tv', the correct result can be
1306 obtained by casting the result to `unsigned long'. */
1308 tarval_ord (tarval *tv, int *fail)
1312 switch (get_mode_modecode(tv->mode)) {
1314 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1315 *fail = tv->u.uInt > tv_val_uInt (get_mode_max(mode_Ls));
1318 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1331 tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
1333 tarval *val = XP_GETARG (tarval *, 0);
1339 switch (get_mode_modecode(val->mode)) {
1341 case irm_T: /* none */
1342 printed = XPSR ("<bad>");
1345 case irm_F: /* float */
1346 sprintf (buf, "%1.9e", (float)(val->u.F));
1347 printed = XPF1R ("%s", buf);
1349 case irm_D: /* double */
1350 printed = XPF1R ("%1.30g", (double)(val->u.D));
1353 case irm_C: /* character */
1354 if ((isprint (val->u.C)) &&
1355 (val->u.C != '\\') && (val->u.C != '\'')) {
1356 printed = XPF1R ("'%c'", val->u.C);
1358 printed = XPF1R ("0x%x", (unsigned long)val->u.C);
1362 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls: /* signed num */
1363 printed = XPF1R ("%ld", (long)val->u.sInt);
1365 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu: /* unsigned num */
1366 printed = XPF1R ("%lu", (unsigned long)val->u.uInt);
1369 case irm_P: /* pointer */
1370 if (val->u.P.xname) {
1371 printed = XPR (val->u.P.xname);
1372 } else if (val->u.P.ent) {
1373 if (get_entity_peculiarity(val->u.P.ent) == existent)
1374 printed = XPF1R ("&(%I)", get_entity_ld_ident(val->u.P.ent));
1376 printed = XPSR ("(NULL)");
1378 assert (val == tarval_P_void);
1379 printed = XPSR ("(void)");
1383 case irm_b: /* boolean */
1384 if (val->u.b) printed = XPSR ("true");
1385 else printed = XPSR ("false");
1388 case irm_M: /* memory */
1389 case irm_BB: /* region */
1399 get_tv_mode (tarval *tv)
1404 /* Returns the entity if the tv is a pointer to an entity, else
1406 entity *get_tv_entity(tarval *tv) {
1409 if (tv->mode == mode_P) {
1410 if (tv->u.P.xname) {
1413 } else if (tv->u.P.ent) {