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 // printf("Tarval is %lu\n", val->u.uInt);
185 assert (!uInt_overflow (val->u.uInt, val->mode));
187 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
188 assert (!sInt_overflow (val->u.sInt, val->mode)); break;
194 assert (val->u.P.ent->kind == k_entity);
195 assert ( val->u.P.xname || val->u.P.ent
196 || !tarval_P_void || (val == tarval_P_void));
199 assert ((unsigned)val->u.b <= 1); break;
201 assert (val->mode == mode_T);
212 pset_stats (tarvals);
218 /* Return the canonical tarval * for tv.
219 May destroy everything allocated on tv_obst after tv! */
221 tarval_identify (tarval *tv)
225 o = pset_insert (tarvals, tv, tarval_hash (tv));
228 obstack_free (&tv_obst, (void *)tv);
236 /* Return 0 iff a equals b. Bitwise identical NaNs compare equal. */
238 tarval_cmp (const void *p, const void *q)
246 if (a == b) return 0;
247 if ((void *)a->mode - (void *)b->mode)
248 return (void *)a->mode - (void *)b->mode;
250 switch (get_mode_modecode(a->mode)) {
253 return memcmp (&a->u.F, &b->u.F, sizeof (a->u.F));
255 return memcmp (&a->u.D, &b->u.D, sizeof (a->u.D));
257 return memcmp (&a->u.E, &b->u.E, sizeof (a->u.E));
259 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
260 if (sizeof (int) == sizeof (tarval_uInt)) {
261 return a->u.uInt - b->u.uInt;
263 return a->u.uInt != b->u.uInt;
265 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
266 if (sizeof (int) == sizeof (tarval_sInt)) {
267 return a->u.sInt - b->u.sInt;
269 return a->u.sInt != b->u.sInt;
271 return a->u.C - b->u.C;
273 return a->u.U - b->u.U;
275 if (a->u.P.ent || b->u.P.ent)
276 return (char *)a->u.P.ent - (char *)b->u.P.ent;
277 if (a->u.P.xname && b->u.P.xname)
278 return strcmp (a->u.P.xname, b->u.P.xname);
279 return a->u.P.xname - b->u.P.xname;
281 return a->u.b - b->u.b;
288 tarval_hash (tarval *tv)
292 h = get_mode_modecode(tv->mode) * 0x421u;
293 switch (get_mode_modecode(tv->mode)) {
295 h = 0x94b527ce; break;
298 { union { float f; unsigned u; } u;
299 assert (sizeof (float) <= sizeof (unsigned));
300 u.u = 0; u.f = tv->u.F;
306 { union { double d; unsigned u[2]; } u;
307 assert (sizeof (double) <= 2*sizeof (unsigned));
308 u.u[0] = u.u[1] = 0; u.d = tv->u.D;
309 h ^= u.u[0] ^ u.u[1];
313 { union { long double e; unsigned u[3]; } u;
314 assert (sizeof (long double) <= 3*sizeof (unsigned));
315 u.u[0] = u.u[1] = u.u[2] = 0; u.e = tv->u.E;
316 h ^= u.u[0] ^ u.u[1] ^ u.u[2];
319 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
320 h ^= tv->u.uInt; break;
321 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
322 h ^= tv->u.sInt; break;
329 /* @@@ lower bits not random, watch for collisions; perhaps
330 replace by tv->u.p.ent - (entity *)0 */
331 h ^= ((char *)tv->u.P.ent - (char *)0) / 64;
332 } else if (tv->u.P.xname) {
333 /* Of course, strlen() in a hash function is a mistake, but this
334 case should be really rare. */
335 h ^= ID_HASH (tv->u.P.xname, strlen (tv->u.P.xname));
349 /*** ***************** Initialization ************************************* ***/
354 obstack_init (&tv_obst);
355 obstack_alignment_mask (&tv_obst) = ALIGNOF (tarval) - 1;
356 assert (IS_POW2 (ALIGNOF (tarval)));
358 /* initialize the target value table */
359 tarvals = new_pset (tarval_cmp, TUNE_NCONSTANTS);
366 union ieee754_double x;
368 /* assumed by tarval_hash(): */
369 assert (sizeof (float) * CHAR_BIT == 32);
370 assert (sizeof (double) * CHAR_BIT == 64);
373 /* assumed by tarval_sInt & friends: */
374 assert ( (irm_C == irm_c+1) && (irm_h == irm_C+1)
375 && (irm_H == irm_h+1) && (irm_i == irm_H+1)
376 && (irm_I == irm_i+1) && (irm_l == irm_I+1)
377 && (irm_L == irm_l+1));
379 /* assumed everywhere: */
380 for (i = 0; i <= irm_L-irm_c; i += 2) {
381 assert ( IS_POW2 (max_sInt[i+1]+1)
382 && (min_sInt[i] == -max_sInt[i]-1)
383 && ((tarval_uInt)max_sInt[i+1] == (tarval_uInt)max_sInt[i]-min_sInt[i]));
388 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
390 tarval_bad = tarval_identify (tv);
392 tarval_b_false = tarval_from_long (mode_b, 0);
393 tarval_b_true = tarval_from_long (mode_b, 1);
395 /* IsInf <-> exponent == 0x7ff && ! (bits | fraction_low) */
396 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
399 x.ieee.exponent = 0x7ff;
400 x.ieee.mantissa0 = 0;
401 x.ieee.mantissa1 = 0;
403 tarval_D_Inf = tarval_identify (tv);
405 /* IsNaN <-> exponent==0x7ff && (qnan_bit | bits | fraction_low) */
406 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
408 x.ieee_nan.negative = 0;
409 x.ieee_nan.exponent = 0x7ff;
410 x.ieee_nan.quiet_nan = 1; /* @@@ quiet or signalling? */
411 x.ieee_nan.mantissa0 = 42;
412 x.ieee_nan.mantissa1 = 0;
413 assert(x.d != x.d /* x.d is NaN */);
415 tarval_D_NaN = tarval_identify (tv);
417 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
419 tv->u.P.xname = NULL;
422 tarval_P_void = tarval_identify (tv);
424 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
427 tarval_mode_null [irm_F] = tarval_from_long (mode_F, 0);
428 tarval_mode_null [irm_D] = tarval_from_long (mode_D, 0);
429 tarval_mode_null [irm_E] = tarval_from_long (mode_E, 0);
430 tarval_mode_null [irm_Bs] = tarval_from_long (mode_Bs, 0);
431 tarval_mode_null [irm_Bu] = tarval_from_long (mode_Bu, 0);
432 tarval_mode_null [irm_Hs] = tarval_from_long (mode_Hs, 0);
433 tarval_mode_null [irm_Hu] = tarval_from_long (mode_Hu, 0);
434 tarval_mode_null [irm_Is] = tarval_from_long (mode_Is, 0);
435 tarval_mode_null [irm_Iu] = tarval_from_long (mode_Iu, 0);
436 tarval_mode_null [irm_Ls] = tarval_from_long (mode_Ls, 0);
437 tarval_mode_null [irm_Lu] = tarval_from_long (mode_Lu, 0);
438 tarval_mode_null [irm_C] = tarval_from_long (mode_C, 0);
439 tarval_mode_null [irm_U] = tarval_from_long (mode_U, 0);
440 tarval_mode_null [irm_b] = tarval_b_false;
441 tarval_mode_null [irm_P] = tarval_P_void;
445 /*** ********************** Constructors for tarvals ********************** ***/
447 /* copy from src to dst len chars omitting '_'. */
449 stripcpy (char *dst, const char *src, size_t len)
454 if (*src == '_') src++;
457 *d = 0; /* make it 0-terminated. */
463 tarval_F_from_str (const char *s, size_t len)
471 buf = alloca (len+1);
472 stripcpy (buf, s, len);
474 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
476 tv->u.F = (float)strtod (buf, &eptr);
477 assert (eptr == buf+strlen(buf));
479 return tarval_identify (tv);
484 tarval_D_from_str (const char *s, size_t len)
492 buf = alloca (len+1);
493 stripcpy (buf, s, len);
495 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
497 tv->u.D = strtod (buf, &eptr);
498 assert (eptr == buf+strlen(buf));
500 return tarval_identify (tv);
504 tarval_int_from_str (const char *s, size_t len, int base, ir_mode *m)
510 assert (mode_is_int(m));
513 buf = alloca (len+1);
514 stripcpy (buf, s, len);
517 val = strtol(buf, &eptr, base); /* strtoll */
518 assert (eptr == buf+strlen(buf));
519 if ((errno == ERANGE) &&
520 ((m == mode_Ls) || (m == mode_Lu)) ) {
521 printf("WARNING: Constant %s not representable. Continuing with %ld.\n",
525 return tarval_from_long(m, val);
528 /* Create a tarval with mode `m' and value `val' casted to the type that
529 represents such tarvals on host. The resulting value must be legal
532 tarval_from_long (ir_mode *m, long val)
538 if (m == mode_T) return tarval_bad;
540 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
543 switch (get_mode_modecode(m)) {
546 tv->u.F = val; break;
548 tv->u.D = val; break;
550 /* @@@ not yet implemented */
553 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
554 tv->u.uInt = val; break;
556 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
557 tv->u.sInt = val; break;
560 obstack_free (&tv_obst, tv);
561 return tarval_P_void;
563 tv->u.C = val; break;
565 tv->u.U = val; break;
567 tv->u.b = !!val; /* u.b must be 0 or 1 */
573 return tarval_identify (tv);
578 tarval_P_from_str (const char *xname)
584 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
587 tv->u.P.xname = obstack_copy0 (&tv_obst, xname, strlen (xname));
590 return tarval_identify (tv);
595 tarval_P_from_entity (entity *ent)
601 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
604 tv->u.P.xname = NULL;
607 return tarval_identify (tv);
611 /* Routines for building a tarval step by step follow.
612 Legal calling sequences:
614 No constructors except tarval_append() and tarval_append1 ()
615 tarval_finish_as() or tarval_cancel() */
617 /* Begin building a tarval. */
622 obstack_blank (&tv_obst, sizeof (tarval));
626 /* Append `n' chars from `p' to the tarval currently under construction. */
628 tarval_append (const char *p, size_t n)
631 obstack_grow (&tv_obst, p, n);
635 /* Append `ch' to the tarval currently under construction. */
637 tarval_append1 (char ch)
640 obstack_1grow (&tv_obst, ch);
644 /* Finish the tarval currently under construction and give id mode `m'.
646 Return NULL if the value does not make sense for this mode, this
647 can only happen in mode C. */
649 tarval_finish_as (ir_mode *m)
651 int size = obstack_object_size (&tv_obst) - sizeof (tarval);
654 char ch = 0; /* initialized to shut up gcc */
656 assert (BUILDING && (size >= 0));
658 if (size != 1) return tarval_cancel();
659 p = (unsigned char *)obstack_base (&tv_obst) + sizeof (tarval);
661 obstack_blank (&tv_obst, -size);
663 tv = obstack_finish (&tv_obst);
664 p = (unsigned char *)tv + sizeof (tarval);
667 switch (get_mode_modecode(m)) {
678 return tarval_identify (tv);
682 /* Cancel tarval building and return tarval_bad. */
687 obstack_free (&tv_obst, obstack_finish (&tv_obst));
692 /*** ****************** Arithmethic operations on tarvals ***************** ***/
694 /* Return `src' converted to mode `m' if representable, else NULL.
695 @@@ lots of conversions missing */
697 tarval_convert_to (tarval *src, ir_mode *m)
701 if (m == src->mode) return src;
703 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
706 switch (get_mode_modecode(src->mode)) {
709 if (m != mode_F) goto fail;
713 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
714 switch (get_mode_modecode(m)) {
715 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
716 tv->u.sInt = src->u.sInt;
717 if (sInt_overflow (tv->u.sInt, m)) goto fail;
720 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
721 tv->u.uInt = src->u.sInt;
722 if (uInt_overflow (tv->u.uInt, m)) goto fail;
726 tv->u.b = !!src->u.sInt;
732 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
733 switch (get_mode_modecode(m)) {
734 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
735 tv->u.sInt = src->u.uInt;
736 if (sInt_overflow (tv->u.sInt, m)) goto fail;
739 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
740 tv->u.uInt = src->u.uInt;
741 if (uInt_overflow (tv->u.uInt, m)) goto fail;
745 tv->u.b = !!src->u.uInt;
753 switch (get_mode_modecode(m)) {
754 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
755 tv->u.sInt = src->u.b;
758 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
759 tv->u.uInt = src->u.b;
767 obstack_free (&tv_obst, tv);
771 return tarval_identify (tv);
775 /* GL Why are there no ArmRoq comments, why is this not used? */
777 tarval_neg (tarval *a)
783 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
787 switch (get_mode_modecode(a->mode)) {
789 case irm_F: tv->u.F = -a->u.F; break;
790 case irm_D: tv->u.D = -a->u.D; break;
792 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
793 tv->u.uInt = -a->u.uInt & tv_val_uInt (get_mode_max(a->mode));
796 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
797 tv->u.sInt = -a->u.sInt;
798 if ( sInt_overflow (tv->u.sInt, a->mode)
799 || ((tv->u.sInt >= 0) == (a->u.sInt >= 0))) {
800 obstack_free (&tv_obst, tv);
804 case irm_b: tv->u.b = !a->u.b; break;
808 return tarval_identify (tv);
812 /* Compare `a' with `b'.
813 Return one of irpn_Lt, irpn_Eq, irpn_Gt, irpn_Uo, or irpn_False if
814 result is unknown. */
816 tarval_comp (tarval *a, tarval *b)
822 assert (a->mode == b->mode);
824 switch (get_mode_modecode(a->mode)) {
826 case irm_F: return ( a->u.F == b->u.F ? irpn_Eq
827 : a->u.F > b->u.F ? irpn_Gt
828 : a->u.F < b->u.F ? irpn_Lt
830 case irm_D: return ( a->u.D == b->u.D ? irpn_Eq
831 : a->u.D > b->u.D ? irpn_Gt
832 : a->u.D < b->u.D ? irpn_Lt
834 case irm_E: return ( a->u.E == b-> u.E ? irpn_Eq
835 : a->u.E > b->u.E ? irpn_Gt
836 : a->u.E < b->u.E ? irpn_Lt
839 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
840 return ( a->u.uInt == b->u.uInt ? irpn_Eq
841 : a->u.uInt > b->u.uInt ? irpn_Gt
844 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
845 return ( a->u.sInt == b->u.sInt ? irpn_Eq
846 : a->u.sInt > b->u.sInt ? irpn_Gt
848 case irm_b: return ( a->u.b == b->u.b ? irpn_Eq
849 : a->u.b > b->u.b ? irpn_Gt
851 /* The following assumes that pointers are unsigned, which is valid
852 for all sane CPUs (transputers are insane). */
853 case irm_P: return ( a == b ? irpn_Eq
854 : a == tarval_P_void ? irpn_Lt
855 : b == tarval_P_void ? irpn_Gt
856 : irpn_False); /* unknown */
862 /* Return `a+b' if computable, else NULL. Modes must be equal. */
864 tarval_add (tarval *a, tarval *b)
868 TARVAL_VRFY (a); TARVAL_VRFY (b);
869 assert (a->mode == b->mode);
871 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
875 switch (get_mode_modecode(a->mode)) {
877 case irm_F: tv->u.F = a->u.F + b->u.F; break; /* @@@ overflow etc */
878 case irm_D: tv->u.D = a->u.D + b->u.D; break; /* @@@ dto. */
879 case irm_E: tv->u.E = a->u.E + b->u.E; break; /* @@@ dto. */
881 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
882 tv->u.uInt = (a->u.uInt + b->u.uInt) & tv_val_uInt (get_mode_max(a->mode));
885 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
886 tv->u.sInt = a->u.sInt + b->u.sInt;
887 if ( sInt_overflow (tv->u.sInt, a->mode)
888 || ((tv->u.sInt > a->u.sInt) ^ (b->u.sInt > 0))) {
889 obstack_free (&tv_obst, tv);
893 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
897 return tarval_identify (tv);
901 /* Return `a-b' if computable, else NULL. Modes must be equal. */
903 tarval_sub (tarval *a, tarval *b)
907 TARVAL_VRFY (a); TARVAL_VRFY (b);
908 assert (a->mode == b->mode);
910 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
914 switch (get_mode_modecode(a->mode)) {
916 case irm_F: tv->u.F = a->u.F - b->u.F; break; /* @@@ overflow etc */
917 case irm_D: tv->u.D = a->u.D - b->u.D; break; /* @@@ dto. */
918 case irm_E: tv->u.E = a->u.E - b->u.E; break; /* @@@ dto. */
920 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
921 tv->u.uInt = (a->u.uInt - b->u.uInt) & tv_val_uInt (get_mode_max(a->mode));
924 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
925 tv->u.sInt = a->u.sInt - b->u.sInt;
926 if ( sInt_overflow (tv->u.sInt, a->mode)
927 || ((tv->u.sInt > a->u.sInt) ^ (b->u.sInt < 0))) {
928 obstack_free (&tv_obst, tv);
932 case irm_b: tv->u.b = a->u.b & ~b->u.b; break; /* u.b is in canonical form */
936 return tarval_identify (tv);
939 /* Return `a*b' if computable, else NULL. Modes must be equal. */
941 tarval_mul (tarval *a, tarval *b)
945 TARVAL_VRFY (a); TARVAL_VRFY (b);
946 assert (a->mode == b->mode);
948 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
952 switch (get_mode_modecode(a->mode)) {
954 case irm_F: tv->u.F = a->u.F * b->u.F; break; /* @@@ overflow etc */
955 case irm_D: tv->u.D = a->u.D * b->u.D; break; /* @@@ dto. */
956 case irm_E: tv->u.E = a->u.E * b->u.E; break; /* @@@ dto. */
958 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
959 tv->u.uInt = (a->u.uInt * b->u.uInt) & tv_val_uInt (get_mode_max(a->mode));
962 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
963 tv->u.sInt = a->u.sInt * b->u.sInt;
964 if ( sInt_overflow (tv->u.sInt, a->mode)
965 || (b->u.sInt && (tv->u.sInt / b->u.sInt != a->u.sInt))) {
966 obstack_free (&tv_obst, tv);
970 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
974 return tarval_identify (tv);
978 /* Return floating-point `a/b' if computable, else NULL.
979 Modes must be equal, non-floating-point operands are converted to irm_D. */
981 tarval_quo (tarval *a, tarval *b)
985 TARVAL_VRFY (a); TARVAL_VRFY (b);
986 assert (a->mode == b->mode);
988 switch (get_mode_modecode(a->mode)) {
991 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
993 tv->u.F = a->u.F / b->u.F; /* @@@ overflow etc */
996 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
998 tv->u.D = a->u.D / b->u.D; /* @@@ overflow etc */
1001 a = tarval_convert_to (a, mode_D);
1002 b = tarval_convert_to (b, mode_D);
1003 return a && b ? tarval_quo (a, b) : NULL;
1006 return tarval_identify (tv);
1010 /* Return `a/b' if computable, else NULL. Modes must be equal. */
1012 tarval_div (tarval *a, tarval *b)
1016 TARVAL_VRFY (a); TARVAL_VRFY (b);
1017 assert (a->mode == b->mode);
1019 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1023 switch (get_mode_modecode(a->mode)) {
1025 case irm_F: tv->u.F = floor (a->u.F / b->u.F); break; /* @@@ overflow etc */
1026 case irm_D: tv->u.D = floor (a->u.D / b->u.D); break; /* @@@ dto. */
1027 case irm_E: tv->u.E = floor (a->u.E / b->u.E); break; /* @@@ dto. */
1029 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1030 if (!b->u.uInt) goto fail;
1031 tv->u.uInt = a->u.uInt / b->u.uInt;
1034 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1036 || ((b->u.sInt == -1) && (a->u.sInt == tv_val_sInt (get_mode_max(a->mode)) ))) {
1038 obstack_free (&tv_obst, tv);
1041 tv->u.sInt = a->u.sInt / b->u.sInt;
1043 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1047 return tarval_identify (tv);
1051 /* Return `a%b' if computable, else NULL. Modes must be equal. */
1053 tarval_mod (tarval *a, tarval *b)
1057 TARVAL_VRFY (a); TARVAL_VRFY (b);
1058 assert (a->mode == b->mode);
1060 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1064 switch (get_mode_modecode(a->mode)) {
1066 case irm_F: tv->u.F = fmod (a->u.F, b->u.F); break; /* @@@ overflow etc */
1067 case irm_D: tv->u.D = fmod (a->u.D, b->u.D); break; /* @@@ dto */
1068 case irm_E: tv->u.E = fmod (a->u.E, b->u.E); break; /* @@@ dto. */
1070 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1071 if (!b->u.uInt) goto fail;
1072 tv->u.uInt = a->u.uInt % b->u.uInt;
1075 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1078 obstack_free (&tv_obst, tv);
1081 tv->u.sInt = a->u.sInt % b->u.sInt;
1083 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1087 return tarval_identify (tv);
1090 /* Return |a| if computable, else Null. */
1091 /* is -max == min?? */
1093 tarval_abs (tarval *a) {
1095 if (tv_is_negative(a)) return tarval_neg(a);
1100 tv_is_negative(tarval *a) {
1102 switch (get_mode_modecode(a->mode)) {
1104 case irm_F: return (a->u.F<0);
1105 case irm_D: return (a->u.D<0);
1106 case irm_E: return (a->u.E<0);
1108 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1112 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1113 return (a->u.sInt < 0);
1123 /* Return `a&b'. Modes must be equal. */
1125 tarval_and (tarval *a, tarval *b)
1129 TARVAL_VRFY (a); TARVAL_VRFY (b);
1130 assert (a->mode == b->mode);
1132 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1136 switch (get_mode_modecode(a->mode)) {
1138 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1139 tv->u.uInt = a->u.uInt & b->u.uInt; break;
1141 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1142 tv->u.sInt = a->u.sInt & b->u.sInt; break;
1143 case irm_b: tv->u.b = a->u.b & b->u.b; break; /* u.b is in canonical form */
1147 return tarval_identify (tv);
1151 /* Return `a|b'. Modes must be equal. */
1153 tarval_or (tarval *a, tarval *b)
1157 TARVAL_VRFY (a); TARVAL_VRFY (b);
1158 assert (a->mode == b->mode);
1160 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1164 switch (get_mode_modecode(a->mode)) {
1166 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1167 tv->u.uInt = a->u.uInt | b->u.uInt; break;
1169 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1170 tv->u.sInt = a->u.sInt | b->u.sInt; break;
1171 case irm_b: tv->u.b = a->u.b | b->u.b; break; /* u.b is in canonical form */
1175 return tarval_identify (tv);
1179 /* Return `a^b'. Modes must be equal. */
1181 tarval_eor (tarval *a, tarval *b)
1185 TARVAL_VRFY (a); TARVAL_VRFY (b);
1186 assert (a->mode == b->mode);
1188 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1192 switch (get_mode_modecode(a->mode)) {
1194 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1195 tv->u.uInt = a->u.uInt ^ b->u.uInt; break;
1197 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1198 tv->u.sInt = a->u.sInt ^ b->u.sInt; break;
1199 case irm_b: tv->u.b = a->u.b ^ b->u.b; break; /* u.b is in canonical form */
1203 return tarval_identify (tv);
1207 /* Return `a<<b' if computable, else NULL. */
1209 tarval_shl (tarval *a, tarval *b)
1215 TARVAL_VRFY (a); TARVAL_VRFY (b);
1217 shift = tarval_ord (b, &b_is_huge);
1220 || ((shift >= get_mode_size(mode_Ls)*target_bits))) {
1224 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1227 switch (get_mode_modecode(a->mode)) {
1229 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1230 tv->u.uInt = a->u.uInt << shift;
1233 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1234 tv->u.sInt = a->u.sInt << shift;
1236 default: assert (0);
1239 return tarval_identify (tv);
1243 /* Return `a>>b' if computable, else NULL.
1244 The interpretation of >> (sign extended or not) is implementaion
1245 dependent, i.e. this is neither shr nor shrs!! */
1247 tarval_shr (tarval *a, tarval *b)
1253 TARVAL_VRFY (a); TARVAL_VRFY (b);
1255 shift = tarval_ord (b, &b_is_huge);
1258 || ((shift >= get_mode_size(mode_Ls)*target_bits))) {
1262 tv = (tarval *)obstack_alloc (&tv_obst, sizeof (tarval));
1265 switch (get_mode_modecode(a->mode)) {
1267 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1268 tv->u.uInt = a->u.uInt >> shift;
1271 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1272 tv->u.sInt = a->u.sInt >> shift;
1274 default: assert (0);
1277 return tarval_identify (tv);
1281 /* Classify `tv', which may be NULL.
1282 Return 0 if `tv' is the additive neutral element, 1 if `tv' is the
1283 multiplicative neutral element, and -1 if `tv' is the neutral
1284 element of bitwise and. */
1286 tarval_classify (tarval *tv)
1292 switch (get_mode_modecode(tv->mode)) {
1294 case irm_F: case irm_D: case irm_E:
1298 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Bu))) - 1;
1300 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Hu))) - 1;
1302 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Iu))) - 1;
1304 return (long)((tv->u.uInt+1) & tv_val_uInt (get_mode_max(mode_Lu))) - 1;
1306 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1315 /* Convert `tv' into type `long', set `fail' if not representable.
1316 If `fail' gets set for an unsigned `tv', the correct result can be
1317 obtained by casting the result to `unsigned long'. */
1319 tarval_ord (tarval *tv, int *fail)
1323 switch (get_mode_modecode(tv->mode)) {
1325 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu:
1326 *fail = tv->u.uInt > tv_val_uInt (get_mode_max(mode_Ls));
1329 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls:
1342 tarval_print (XP_PAR1, const xprintf_info *info ATTRIBUTE((unused)), XP_PARN)
1344 tarval *val = XP_GETARG (tarval *, 0);
1350 switch (get_mode_modecode(val->mode)) {
1352 case irm_T: /* none */
1353 printed = XPSR ("<bad>");
1356 case irm_F: /* float */
1357 sprintf (buf, "%1.9e", (float)(val->u.F));
1358 printed = XPF1R ("%s", buf);
1360 case irm_D: /* double */
1361 printed = XPF1R ("%1.30g", (double)(val->u.D));
1364 case irm_C: /* character */
1365 if ((isprint (val->u.C)) &&
1366 (val->u.C != '\\') && (val->u.C != '\'')) {
1367 printed = XPF1R ("'%c'", val->u.C);
1369 printed = XPF1R ("0x%x", (unsigned long)val->u.C);
1372 case irm_U: /* unicode character */
1373 printed = XPF1R ("0x%x", (unsigned long)val->u.U);
1376 case irm_Bs: case irm_Hs: case irm_Is: case irm_Ls: /* signed num */
1377 printed = XPF1R ("%ld", (long)val->u.sInt);
1379 case irm_Bu: case irm_Hu: case irm_Iu: case irm_Lu: /* unsigned num */
1380 printed = XPF1R ("%lu", (unsigned long)val->u.uInt);
1383 case irm_P: /* pointer */
1384 if (val->u.P.xname) {
1385 printed = XPR (val->u.P.xname);
1386 } else if (val->u.P.ent) {
1387 if (get_entity_peculiarity(val->u.P.ent) == existent)
1388 printed = XPF1R ("&(%I)", get_entity_ld_ident(val->u.P.ent));
1390 printed = XPSR ("(NULL)");
1392 assert (val == tarval_P_void);
1393 printed = XPSR ("(void)");
1397 case irm_b: /* boolean */
1398 if (val->u.b) printed = XPSR ("true");
1399 else printed = XPSR ("false");
1402 case irm_M: /* memory */
1403 case irm_BB: /* region */
1413 get_tv_mode (tarval *tv)
1418 /* Returns the entity if the tv is a pointer to an entity, else
1420 entity *get_tv_entity(tarval *tv) {
1423 if (tv->mode == mode_P) {
1424 if (tv->u.P.xname) {
1427 } else if (tv->u.P.ent) {