Add an additional PhiM check:
[libfirm] / ir / tv / tv.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/tv/tv.c
4  * Purpose:     Representation of and static computations on target machine
5  *              values.
6  * Author:      Mathias Heil
7  * Modified by:
8  * Created:
9  * CVS-ID:      $Id$
10  * Copyright:   (c) 2003 Universität Karlsruhe
11  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
12  */
13
14 /*
15  *    Values are stored in a format depending upon chosen arithmetic
16  *    module. Default uses strcalc and fltcalc.
17  *
18  */
19
20 /* This implementation assumes:
21  *  - target has IEEE-754 floating-point arithmetic.  */
22
23
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
27
28
29 #include <assert.h>         /* assertions */
30 #include <stdlib.h>         /* atoi() */
31 #ifdef HAVE_STRING_H
32 # include <string.h>         /* nice things for strings */
33 #endif
34 #ifdef HAVE_STRINGS_H
35 #include <strings.h>        /* strings.h also includes bsd only function strcasecmp */
36 #endif
37 #ifdef HAVE_STDLIB_H
38 # include <stdlib.h>
39 #endif
40 #ifdef HAVE_ALLOCA_H
41 # include <alloca.h>
42 #endif
43 #ifdef HAVE_MALLOC_H
44 # include <malloc.h>
45 #endif
46
47 #include "tv_t.h"
48 #include "set.h"            /* to store tarvals in */
49 #include "entity_t.h"       /* needed to store pointers to entities */
50 #include "irmode_t.h"
51 #include "irnode.h"         /* defines boolean return values (pnc_number)*/
52 #include "strcalc.h"
53 #include "fltcalc.h"
54 #include "irtools.h"
55 #include "xmalloc.h"
56 #include "firm_common.h"
57
58 /** Size of hash tables.  Should correspond to average number of distinct constant
59     target values */
60 #define N_CONSTANTS 2048
61
62 /* get the integer overflow mode */
63 #define GET_OVERFLOW_MODE() int_overflow_mode
64
65 /* unused, float to int doesn't work yet */
66 enum float_to_int_mode {
67   TRUNCATE,
68   ROUND
69 };
70
71 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
72
73 #define SWITCH_NOINFINITY 0
74 #define SWITCH_NODENORMALS 0
75
76 /****************************************************************************
77  *   local definitions and macros
78  ****************************************************************************/
79 #ifndef NDEBUG
80 #  define TARVAL_VERIFY(a) tarval_verify((a))
81 #else
82 #  define TARVAL_VERIFY(a) ((void)0)
83 #endif
84
85 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
86 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
87
88 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
89 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
90
91 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
92 #if 0
93 static long long count = 0;
94 #  define ANNOUNCE() printf(__FILE__": call no. %lld (%s)\n", count++, __FUNCTION__);
95 #else
96 #  define ANNOUNCE() ((void)0)
97 #endif
98 /****************************************************************************
99  *   private variables
100  ****************************************************************************/
101 static struct set *tarvals;   /* container for tarval structs */
102 static struct set *values;    /* container for values */
103 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
104
105 /****************************************************************************
106  *   private functions
107  ****************************************************************************/
108 #ifndef NDEBUG
109 static int hash_val(const void *value, unsigned int length);
110 static int hash_tv(tarval *tv);
111 static void _fail_verify(tarval *tv, const char* file, int line)
112 {
113   /* print a memory image of the tarval and throw an assertion */
114   if (tv)
115     printf("%s:%d: Invalid tarval:\n  mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
116   else
117     printf("%s:%d: Invalid tarval (null)", file, line);
118   assert(0);
119 }
120 #ifdef __GNUC__
121 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
122 #endif
123
124 INLINE static void tarval_verify(tarval *tv)
125 {
126   assert(tv);
127   assert(tv->mode);
128   assert(tv->value);
129
130   if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
131   if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
132
133   if (!FIND_TARVAL(tv)) fail_verify(tv);
134   if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
135
136   return;
137 }
138 #endif /* NDEBUG */
139
140 /** Hash a tarval. */
141 static int hash_tv(tarval *tv) {
142   return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
143 }
144
145 /** Hash a value. Treat it as a byte array. */
146 static int hash_val(const void *value, unsigned int length) {
147   unsigned int i;
148   unsigned int hash = 0;
149
150   /* scramble the byte - array */
151   for (i = 0; i < length; i++)
152   {
153     hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
154     hash += (hash << 11) ^ (hash >> 17);
155   }
156
157   return hash;
158 }
159
160 /** finds tarval with value/mode or creates new tarval */
161 static tarval *get_tarval(const void *value, int length, ir_mode *mode)
162 {
163   tarval tv;
164
165   tv.kind   = k_tarval;
166   tv.mode   = mode;
167   tv.length = length;
168   if (length > 0) {
169     /* if there already is such a value, it is returned, else value
170      * is copied into the set */
171     tv.value = INSERT_VALUE(value, length);
172   } else {
173     tv.value = value;
174   }
175   /* if there is such a tarval, it is returned, else tv is copied
176    * into the set */
177   return (tarval *)INSERT_TARVAL(&tv);
178 }
179
180 /**
181  * handle overflow
182  */
183 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
184 {
185   switch (get_mode_sort(mode))
186   {
187     case irms_int_number:
188       if (sc_comp(value, get_mode_max(mode)->value) == 1) {
189         switch (GET_OVERFLOW_MODE()) {
190           case TV_OVERFLOW_SATURATE:
191             return get_mode_max(mode);
192           case TV_OVERFLOW_WRAP:
193             {
194               char *temp = alloca(sc_get_buffer_length());
195               sc_val_from_ulong(-1, temp);
196               sc_and(temp, value, temp);
197               return get_tarval(temp, length, mode);
198             }
199           case TV_OVERFLOW_BAD:
200             return tarval_bad;
201           default:
202             return get_tarval(value, length, mode);
203         }
204       }
205       if (sc_comp(value, get_mode_min(mode)->value) == -1) {
206         switch (GET_OVERFLOW_MODE()) {
207           case TV_OVERFLOW_SATURATE:
208             return get_mode_min(mode);
209           case TV_OVERFLOW_WRAP:
210             {
211               char *temp = alloca(sc_get_buffer_length());
212               sc_val_from_ulong(-1, temp);
213               sc_and(temp, value, temp);
214               return get_tarval(temp, length, mode);
215             }
216           case TV_OVERFLOW_BAD:
217             return tarval_bad;
218           default:
219             return get_tarval(value, length, mode);
220         }
221       }
222       break;
223
224     case irms_float_number:
225       if (SWITCH_NOINFINITY && fc_is_inf(value))
226       {
227         return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
228       }
229
230       if (SWITCH_NODENORMALS && fc_is_subnormal(value))
231       {
232         return get_mode_null(mode);
233       }
234       break;
235
236     default:
237       break;
238   }
239   return get_tarval(value, length, mode);
240 }
241
242 /*
243  *   public variables declared in tv.h
244  */
245 static tarval reserved_tv[4];
246
247 tarval *tarval_bad       = &reserved_tv[0];
248 tarval *tarval_undefined = &reserved_tv[1];
249 tarval *tarval_b_false   = &reserved_tv[2];
250 tarval *tarval_b_true    = &reserved_tv[3];
251
252 /*
253  *   public functions declared in tv.h
254  */
255
256 /*
257  * Constructors =============================================================
258  */
259 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
260 {
261   ANNOUNCE();
262   assert(str);
263   assert(len);
264   assert(mode);
265
266   switch (get_mode_sort(mode))
267   {
268     case irms_control_flow:
269     case irms_memory:
270     case irms_auxiliary:
271       assert(0);
272       break;
273
274     case irms_internal_boolean:
275       /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
276       if (strcasecmp(str, "true")) return tarval_b_true;
277       else if (strcasecmp(str, "false")) return tarval_b_true;
278       else
279         /* XXX This is C semantics */
280     return atoi(str) ? tarval_b_true : tarval_b_false;
281
282     case irms_float_number:
283       switch(get_mode_size_bits(mode)) {
284         case 32:
285           fc_val_from_str(str, len, 8, 23, NULL);
286           break;
287         case 64:
288           fc_val_from_str(str, len, 11, 52, NULL);
289           break;
290         case 80:
291           fc_val_from_str(str, len, 15, 64, NULL);
292           break;
293       }
294       return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
295
296     case irms_reference:
297       /* same as integer modes */
298     case irms_int_number:
299     case irms_character:
300       sc_val_from_str(str, len, NULL, mode);
301       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
302   }
303
304   assert(0);  /* can't be reached, can it? */
305   return NULL;
306 }
307
308 /*
309  * helper function, create a tarval from long
310  */
311 tarval *new_tarval_from_long(long l, ir_mode *mode)
312 {
313   ANNOUNCE();
314   assert(mode);
315
316   switch(get_mode_sort(mode))
317   {
318     case irms_internal_boolean:
319       /* XXX C semantics ! */
320       return l ? tarval_b_true : tarval_b_false ;
321
322     case irms_reference:
323       /* same as integer modes */
324     case irms_int_number:
325     case irms_character:
326       sc_val_from_long(l, NULL);
327       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
328
329     case irms_float_number:
330       return new_tarval_from_double((long double)l, mode);
331
332     default:
333       assert(0 && "unsupported mode sort");
334   }
335   return NULL;
336 }
337
338 /* returns non-zero if can be converted to long */
339 int tarval_is_long(tarval *tv)
340 {
341   mode_sort sort = get_mode_sort(tv->mode);
342
343   ANNOUNCE();
344   if (sort != irms_int_number && sort != irms_character) return 0;
345
346   if (get_mode_size_bits(tv->mode) > sizeof(long)<<3)
347   {
348     /* the value might be too big to fit in a long */
349     sc_max_from_bits(sizeof(long)<<3, 0, NULL);
350     if (sc_comp(sc_get_buffer(), tv->value) == -1)
351     {
352       /* really doesn't fit */
353       return 0;
354     }
355   }
356   return 1;
357 }
358
359 /* this might overflow the machine's long, so use only with small values */
360 long get_tarval_long(tarval* tv)
361 {
362   ANNOUNCE();
363   assert(tarval_is_long(tv) && "tarval too big to fit in long");
364
365   return sc_val_to_long(tv->value);
366 }
367
368 tarval *new_tarval_from_double(long double d, ir_mode *mode)
369 {
370   ANNOUNCE();
371   assert(mode && (get_mode_sort(mode) == irms_float_number));
372
373   switch (get_mode_size_bits(mode)) {
374     case 32:
375       fc_val_from_float(d, 8, 23, NULL);
376       break;
377     case 64:
378       fc_val_from_float(d, 11, 52, NULL);
379       break;
380     case 80:
381       fc_val_from_float(d, 15, 64, NULL);
382       break;
383   }
384   return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
385 }
386
387 /* returns non-zero if can be converted to double */
388 int tarval_is_double(tarval *tv)
389 {
390   ANNOUNCE();
391   assert(tv);
392
393   return (get_mode_sort(tv->mode) == irms_float_number);
394 }
395
396 long double get_tarval_double(tarval *tv)
397 {
398   ANNOUNCE();
399   assert(tarval_is_double(tv));
400
401   return fc_val_to_float(tv->value);
402 }
403
404
405 /*
406  * Access routines for tarval fields ========================================
407  */
408
409 /* get the mode of the tarval */
410 ir_mode *(get_tarval_mode)(const tarval *tv) {
411   return _get_tarval_mode(tv);
412 }
413
414 /*
415  * Special value query functions ============================================
416  *
417  * These functions calculate and return a tarval representing the requested
418  * value.
419  * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
420  * functions, but these are stored on initialization of the irmode module and
421  * therefore the irmode functions should be prefered to the functions below.
422  */
423
424 tarval *(get_tarval_bad)(void) {
425   return _get_tarval_bad();
426 }
427
428 tarval *(get_tarval_undefined)(void) {
429   return _get_tarval_undefined();
430 }
431
432 tarval *(get_tarval_b_false)(void) {
433   return _get_tarval_b_false();
434 }
435
436 tarval *(get_tarval_b_true)(void) {
437   return _get_tarval_b_true();
438 }
439
440 tarval *get_tarval_max(ir_mode *mode)
441 {
442   ANNOUNCE();
443   assert(mode);
444
445   if (get_mode_n_vector_elems(mode) > 1) {
446     /* vector arithmetic not implemented yet */
447     return tarval_bad;
448   }
449
450   switch(get_mode_sort(mode))
451   {
452     case irms_reference:
453     case irms_control_flow:
454     case irms_memory:
455     case irms_auxiliary:
456       assert(0);
457       break;
458
459     case irms_internal_boolean:
460       return tarval_b_true;
461
462     case irms_float_number:
463       switch(get_mode_size_bits(mode))
464       {
465         case 32:
466           fc_get_max(8, 23, NULL);
467           break;
468         case 64:
469           fc_get_max(11, 52, NULL);
470           break;
471         case 80:
472           fc_get_max(15, 64, NULL);
473           break;
474       }
475       return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
476
477     case irms_int_number:
478     case irms_character:
479       sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
480       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
481   }
482   return tarval_bad;
483 }
484
485 tarval *get_tarval_min(ir_mode *mode)
486 {
487   ANNOUNCE();
488   assert(mode);
489
490   if (get_mode_n_vector_elems(mode) > 1) {
491     /* vector arithmetic not implemented yet */
492     return tarval_bad;
493   }
494
495   switch(get_mode_sort(mode))
496   {
497     case irms_reference:
498     case irms_control_flow:
499     case irms_memory:
500     case irms_auxiliary:
501       assert(0);
502       break;
503
504     case irms_internal_boolean:
505       return tarval_b_false;
506
507     case irms_float_number:
508       switch(get_mode_size_bits(mode))
509       {
510         case 32:
511           fc_get_min(8, 23, NULL);
512           break;
513         case 64:
514           fc_get_min(11, 52, NULL);
515           break;
516         case 80:
517           fc_get_min(15, 64, NULL);
518           break;
519       }
520       return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
521
522     case irms_int_number:
523     case irms_character:
524       sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
525       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
526   }
527   return tarval_bad;
528 }
529
530 static long _null_value;
531
532 tarval *get_tarval_null(ir_mode *mode)
533 {
534   ANNOUNCE();
535   assert(mode);
536
537   if (get_mode_n_vector_elems(mode) > 1) {
538     /* vector arithmetic not implemented yet */
539     return tarval_bad;
540   }
541
542   switch(get_mode_sort(mode))
543   {
544     case irms_control_flow:
545     case irms_memory:
546     case irms_auxiliary:
547     case irms_internal_boolean:
548       assert(0);
549       break;
550
551     case irms_float_number:
552       return new_tarval_from_double(0.0, mode);
553
554     case irms_int_number:
555     case irms_character:
556       return new_tarval_from_long(0l,  mode);
557
558     case irms_reference:
559       return new_tarval_from_long(_null_value, mode);
560   }
561   return tarval_bad;
562 }
563
564 tarval *get_tarval_one(ir_mode *mode)
565 {
566   ANNOUNCE();
567   assert(mode);
568
569   if (get_mode_n_vector_elems(mode) > 1) {
570     /* vector arithmetic not implemented yet */
571     return tarval_bad;
572   }
573
574   switch(get_mode_sort(mode))
575   {
576     case irms_control_flow:
577     case irms_memory:
578     case irms_auxiliary:
579     case irms_internal_boolean:
580     case irms_reference:
581       assert(0);
582       break;
583
584     case irms_float_number:
585       return new_tarval_from_double(1.0, mode);
586
587     case irms_int_number:
588     case irms_character:
589       return new_tarval_from_long(1l, mode);
590       break;
591   }
592   return tarval_bad;
593 }
594
595 tarval *get_tarval_minus_one(ir_mode *mode)
596 {
597   ANNOUNCE();
598   assert(mode);
599
600   if (get_mode_n_vector_elems(mode) > 1) {
601     /* vector arithmetic not implemented yet */
602     return tarval_bad;
603   }
604
605   switch(get_mode_sort(mode))
606   {
607     case irms_control_flow:
608     case irms_memory:
609     case irms_auxiliary:
610     case irms_internal_boolean:
611     case irms_reference:
612       assert(0);
613       break;
614
615     case irms_float_number:
616       return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
617
618     case irms_int_number:
619     case irms_character:
620       return mode_is_signed(mode) ? new_tarval_from_long(-1l, mode) : tarval_bad;
621   }
622   return tarval_bad;
623 }
624
625 tarval *get_tarval_nan(ir_mode *mode)
626 {
627   ANNOUNCE();
628   assert(mode);
629
630   if (get_mode_n_vector_elems(mode) > 1) {
631     /* vector arithmetic not implemented yet */
632     return tarval_bad;
633   }
634
635   if (get_mode_sort(mode) == irms_float_number) {
636     switch(get_mode_size_bits(mode))
637     {
638       case 32:
639         fc_get_qnan(8, 23, NULL);
640         break;
641       case 64:
642         fc_get_qnan(11, 52, NULL);
643         break;
644       case 80:
645         fc_get_qnan(15, 64, NULL);
646         break;
647     }
648     return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
649   }
650   else {
651     assert(0 && "tarval is not floating point");
652     return tarval_bad;
653   }
654 }
655
656 tarval *get_tarval_plus_inf(ir_mode *mode)
657 {
658   ANNOUNCE();
659   assert(mode);
660
661   if (get_mode_n_vector_elems(mode) > 1) {
662     /* vector arithmetic not implemented yet */
663     return tarval_bad;
664   }
665
666   if (get_mode_sort(mode) == irms_float_number) {
667     switch(get_mode_size_bits(mode))
668     {
669       case 32:
670         fc_get_plusinf(8, 23, NULL);
671         break;
672       case 64:
673         fc_get_plusinf(11, 52, NULL);
674         break;
675       case 80:
676         fc_get_plusinf(15, 64, NULL);
677         break;
678     }
679     return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
680   }
681   else {
682     assert(0 && "tarval is not floating point");
683     return tarval_bad;
684   }
685 }
686
687 tarval *get_tarval_minus_inf(ir_mode *mode)
688 {
689   ANNOUNCE();
690   assert(mode);
691
692   if (get_mode_n_vector_elems(mode) > 1) {
693     /* vector arithmetic not implemented yet */
694     return tarval_bad;
695   }
696
697   if (get_mode_sort(mode) == irms_float_number) {
698     switch(get_mode_size_bits(mode))
699     {
700     case 32:
701       fc_get_minusinf(8, 23, NULL);
702       break;
703     case 64:
704       fc_get_minusinf(11, 52, NULL);
705       break;
706     case 80:
707       fc_get_minusinf(15, 64, NULL);
708       break;
709     }
710     return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
711   }
712   else {
713     assert(0 && "tarval is not floating point");
714     return tarval_bad;
715   }
716 }
717
718 /*
719  * Arithmethic operations on tarvals ========================================
720  */
721
722 /*
723  * test if negative number, 1 means 'yes'
724  */
725 int tarval_is_negative(tarval *a)
726 {
727   ANNOUNCE();
728   assert(a);
729
730   if (get_mode_n_vector_elems(a->mode) > 1) {
731     /* vector arithmetic not implemented yet */
732     assert(0 && "tarval_is_negative is not allowed for vector modes");
733     return 0;
734   }
735
736   switch (get_mode_sort(a->mode))
737   {
738     case irms_int_number:
739       if (!mode_is_signed(a->mode)) return 0;
740       else
741     return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
742
743     case irms_float_number:
744       return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
745
746     default:
747       assert(0 && "not implemented");
748       return 0;
749   }
750 }
751
752 /*
753  * test if null, 1 means 'yes'
754  */
755 int tarval_is_null(tarval *a)
756 {
757   ir_mode *m = get_tarval_mode(a);
758
759   return a == get_tarval_null(m);
760 }
761
762 /*
763  * test if one, 1 means 'yes'
764  */
765 int tarval_is_one(tarval *a)
766 {
767   ir_mode *m = get_tarval_mode(a);
768
769   return a == get_tarval_one(m);
770 }
771
772 /*
773  * comparison
774  */
775 pn_Cmp tarval_cmp(tarval *a, tarval *b)
776 {
777   ANNOUNCE();
778   assert(a);
779   assert(b);
780
781   if (a == tarval_bad || b == tarval_bad) {
782     assert(0 && "Comparison with tarval_bad");
783     return pn_Cmp_False;
784   }
785
786   if (a == tarval_undefined || b == tarval_undefined)
787     return pn_Cmp_False;
788
789   if (a->mode != b->mode)
790     return pn_Cmp_False;
791
792   if (get_mode_n_vector_elems(a->mode) > 1) {
793     /* vector arithmetic not implemented yet */
794     assert(0 && "cmp not implemented for vector modes");
795   }
796
797   /* Here the two tarvals are unequal and of the same mode */
798   switch (get_mode_sort(a->mode))
799   {
800     case irms_control_flow:
801     case irms_memory:
802     case irms_auxiliary:
803     case irms_reference:
804       if (a == b)
805         return pn_Cmp_Eq;
806       return pn_Cmp_False;
807
808     case irms_float_number:
809       /*
810        * BEWARE: we cannot compare a == b here, because
811        * a NaN is always Unordered to any other value, even to itself!
812        */
813       switch (fc_comp(a->value, b->value)) {
814         case -1: return pn_Cmp_Lt;
815         case  0: return pn_Cmp_Eq;
816         case  1: return pn_Cmp_Gt;
817         case  2: return pn_Cmp_Uo;
818         default: return pn_Cmp_False;
819       }
820     case irms_int_number:
821     case irms_character:
822       if (a == b)
823         return pn_Cmp_Eq;
824       return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
825
826     case irms_internal_boolean:
827       if (a == b)
828         return pn_Cmp_Eq;
829       return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
830   }
831   return pn_Cmp_False;
832 }
833
834 /*
835  * convert to other mode
836  */
837 tarval *tarval_convert_to(tarval *src, ir_mode *m)
838 {
839   char *buffer;
840
841   ANNOUNCE();
842   assert(src);
843   assert(m);
844
845   if (src->mode == m) return src;
846
847   if (get_mode_n_vector_elems(src->mode) > 1) {
848     /* vector arithmetic not implemented yet */
849     return tarval_bad;
850   }
851
852   switch (get_mode_sort(src->mode))
853   {
854     case irms_control_flow:
855     case irms_memory:
856     case irms_auxiliary:
857       break;
858
859     /* cast float to something */
860     case irms_float_number:
861       switch (get_mode_sort(m)) {
862         case irms_float_number:
863           switch (get_mode_size_bits(m))
864           {
865             case 32:
866               fc_cast(src->value, 8, 23, NULL);
867               break;
868             case 64:
869               fc_cast(src->value, 11, 52, NULL);
870               break;
871             case 80:
872               fc_cast(src->value, 15, 64, NULL);
873               break;
874             default:
875               break;
876           }
877           return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
878
879         case irms_int_number:
880           switch (GET_FLOAT_TO_INT_MODE())
881           {
882             case TRUNCATE:
883               fc_int(src->value, NULL);
884               break;
885             case ROUND:
886               fc_rnd(src->value, NULL);
887               break;
888             default:
889               assert(0);
890               break;
891           }
892           /* FIXME: floating point unit can't produce a value in integer
893            * representation
894            * an intermediate representation is needed here first. */
895           /*  return get_tarval(); */
896           return tarval_bad;
897
898         default:
899           /* the rest can't be converted */
900           return tarval_bad;
901       }
902       break;
903
904     /* cast int to something */
905     case irms_int_number:
906       switch (get_mode_sort(m)) {
907         case irms_int_number:
908         case irms_character:
909           return get_tarval_overflow(src->value, src->length, m);
910
911         case irms_internal_boolean:
912           /* XXX C semantics */
913           if (src == get_mode_null(src->mode)) return tarval_b_false;
914           else return tarval_b_true;
915
916         case irms_float_number:
917           /* XXX floating point unit does not understand internal integer
918            * representation, convert to string first, then create float from
919            * string */
920           buffer = alloca(100);
921           /* decimal string representation because hexadecimal output is
922            * interpreted unsigned by fc_val_from_str, so this is a HACK */
923           snprintf(buffer, 100, "%s",
924                    sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
925           buffer[100 - 1] = '\0';
926           switch (get_mode_size_bits(m))
927           {
928             case 32:
929               fc_val_from_str(buffer, 0, 8, 23, NULL);
930               break;
931             case 64:
932               fc_val_from_str(buffer, 0, 11, 52, NULL);
933               break;
934             case 80:
935               fc_val_from_str(buffer, 0, 15, 64, NULL);
936               break;
937           }
938           return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
939
940         case irms_reference:
941           /* allow 0 to be casted */
942           if (src == get_mode_null(src->mode))
943             return get_mode_null(m);
944           break;
945
946         default:
947           break;
948       }
949       break;
950
951     case irms_internal_boolean:
952       switch (get_mode_sort(m))
953       {
954         case irms_int_number:
955           if (src == tarval_b_true) return get_mode_one(m);
956           else return get_mode_null(m);
957
958         default:
959           break;
960       }
961       break;
962
963     case irms_character:
964       break;
965     case irms_reference:
966       break;
967   }
968
969   return tarval_bad;
970 }
971
972 /*
973  * bitwise negation
974  */
975 tarval *tarval_not(tarval *a)
976 {
977   char *buffer;
978
979   ANNOUNCE();
980   assert(a);
981
982   /* works for vector mode without changes */
983
984   switch (get_mode_sort(a->mode))
985   {
986     case irms_int_number:
987       buffer = alloca(sc_get_buffer_length());
988       sc_not(a->value, buffer);
989       return get_tarval(buffer, a->length, a->mode);
990
991     case irms_internal_boolean:
992       if (a == tarval_b_true)
993         return tarval_b_false;
994       if (a == tarval_b_false)
995         return tarval_b_true;
996       return tarval_bad;
997
998     default:
999       assert(0 && "bitwise negation is only allowed for integer and boolean");
1000       return tarval_bad;
1001   }
1002 }
1003
1004 /*
1005  * arithmetic negation
1006  */
1007 tarval *tarval_neg(tarval *a)
1008 {
1009   char *buffer;
1010
1011   ANNOUNCE();
1012   assert(a);
1013   assert(mode_is_num(a->mode)); /* negation only for numerical values */
1014
1015   /* note: negation is allowed even for unsigned modes. */
1016
1017   if (get_mode_n_vector_elems(a->mode) > 1) {
1018     /* vector arithmetic not implemented yet */
1019     return tarval_bad;
1020   }
1021
1022   switch (get_mode_sort(a->mode))
1023   {
1024     case irms_int_number:
1025       buffer = alloca(sc_get_buffer_length());
1026       sc_neg(a->value, buffer);
1027       return get_tarval_overflow(buffer, a->length, a->mode);
1028
1029     case irms_float_number:
1030       fc_neg(a->value, NULL);
1031       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1032
1033     default:
1034       return tarval_bad;
1035   }
1036 }
1037
1038 /*
1039  * addition
1040  */
1041 tarval *tarval_add(tarval *a, tarval *b)
1042 {
1043   char *buffer;
1044
1045   ANNOUNCE();
1046   assert(a);
1047   assert(b);
1048   assert(a->mode == b->mode);
1049
1050   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1051     /* vector arithmetic not implemented yet */
1052     return tarval_bad;
1053   }
1054
1055   switch (get_mode_sort(a->mode))
1056   {
1057     case irms_character:
1058     case irms_int_number:
1059       /* modes of a,b are equal, so result has mode of a as this might be the character */
1060       buffer = alloca(sc_get_buffer_length());
1061       sc_add(a->value, b->value, buffer);
1062       return get_tarval_overflow(buffer, a->length, a->mode);
1063
1064     case irms_float_number:
1065       fc_add(a->value, b->value, NULL);
1066       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1067
1068     default:
1069       return tarval_bad;
1070   }
1071 }
1072
1073 /*
1074  * subtraction
1075  */
1076 tarval *tarval_sub(tarval *a, tarval *b)
1077 {
1078   char *buffer;
1079
1080   ANNOUNCE();
1081   assert(a);
1082   assert(b);
1083   assert(a->mode == b->mode);
1084
1085   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1086     /* vector arithmetic not implemented yet */
1087     return tarval_bad;
1088   }
1089   switch (get_mode_sort(a->mode))
1090   {
1091     case irms_character:
1092     case irms_int_number:
1093       /* modes of a,b are equal, so result has mode of a as this might be the character */
1094       buffer = alloca(sc_get_buffer_length());
1095       sc_sub(a->value, b->value, buffer);
1096       return get_tarval_overflow(buffer, a->length, a->mode);
1097
1098     case irms_float_number:
1099       fc_sub(a->value, b->value, NULL);
1100       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1101
1102     default:
1103       return tarval_bad;
1104   }
1105 }
1106
1107 /*
1108  * multiplication
1109  */
1110 tarval *tarval_mul(tarval *a, tarval *b)
1111 {
1112   char *buffer;
1113
1114   ANNOUNCE();
1115   assert(a);
1116   assert(b);
1117   assert(a->mode == b->mode);
1118
1119   if (get_mode_n_vector_elems(a->mode) > 1) {
1120     /* vector arithmetic not implemented yet */
1121     return tarval_bad;
1122   }
1123
1124   switch (get_mode_sort(a->mode))
1125   {
1126     case irms_int_number:
1127       /* modes of a,b are equal */
1128       buffer = alloca(sc_get_buffer_length());
1129       sc_mul(a->value, b->value, buffer);
1130       return get_tarval_overflow(buffer, a->length, a->mode);
1131
1132     case irms_float_number:
1133       fc_mul(a->value, b->value, NULL);
1134       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1135
1136     default:
1137       return tarval_bad;
1138   }
1139 }
1140
1141 /*
1142  * floating point division
1143  */
1144 tarval *tarval_quo(tarval *a, tarval *b)
1145 {
1146   ANNOUNCE();
1147   assert(a);
1148   assert(b);
1149   assert((a->mode == b->mode) && mode_is_float(a->mode));
1150
1151   if (get_mode_n_vector_elems(a->mode) > 1) {
1152     /* vector arithmetic not implemented yet */
1153     return tarval_bad;
1154   }
1155
1156   fc_div(a->value, b->value, NULL);
1157   return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1158 }
1159
1160 /*
1161  * integer division
1162  * overflow is impossible, but look out for division by zero
1163  */
1164 tarval *tarval_div(tarval *a, tarval *b)
1165 {
1166   ANNOUNCE();
1167   assert(a);
1168   assert(b);
1169   assert((a->mode == b->mode) && mode_is_int(a->mode));
1170
1171   if (get_mode_n_vector_elems(a->mode) > 1) {
1172     /* vector arithmetic not implemented yet */
1173     return tarval_bad;
1174   }
1175
1176   /* x/0 error */
1177   if (b == get_mode_null(b->mode)) return tarval_bad;
1178   /* modes of a,b are equal */
1179   sc_div(a->value, b->value, NULL);
1180   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1181 }
1182
1183 /*
1184  * remainder
1185  * overflow is impossible, but look out for division by zero
1186  */
1187 tarval *tarval_mod(tarval *a, tarval *b)
1188 {
1189   ANNOUNCE();
1190   assert(a);
1191   assert(b);
1192   assert((a->mode == b->mode) && mode_is_int(a->mode));
1193
1194   if (get_mode_n_vector_elems(a->mode) > 1) {
1195     /* vector arithmetic not implemented yet */
1196     return tarval_bad;
1197   }
1198
1199   /* x/0 error */
1200   if (b == get_mode_null(b->mode)) return tarval_bad;
1201   /* modes of a,b are equal */
1202   sc_mod(a->value, b->value, NULL);
1203   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1204 }
1205
1206 /*
1207  * absolute value
1208  */
1209 tarval *tarval_abs(tarval *a)
1210 {
1211   char *buffer;
1212
1213   ANNOUNCE();
1214   assert(a);
1215   assert(mode_is_num(a->mode));
1216
1217   if (get_mode_n_vector_elems(a->mode) > 1) {
1218     /* vector arithmetic not implemented yet */
1219     return tarval_bad;
1220   }
1221
1222   switch (get_mode_sort(a->mode))
1223   {
1224     case irms_int_number:
1225       if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1226       {
1227         buffer = alloca(sc_get_buffer_length());
1228         sc_neg(a->value, buffer);
1229         return get_tarval_overflow(buffer, a->length, a->mode);
1230       }
1231       return a;
1232
1233     case irms_float_number:
1234       if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1235       {
1236         fc_neg(a->value, NULL);
1237         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1238       }
1239       return a;
1240
1241     default:
1242       return tarval_bad;
1243   }
1244   return tarval_bad;
1245 }
1246
1247 /*
1248  * bitwise and
1249  */
1250 tarval *tarval_and(tarval *a, tarval *b)
1251 {
1252   ANNOUNCE();
1253   assert(a);
1254   assert(b);
1255   assert(a->mode == b->mode);
1256
1257   /* works even for vector modes */
1258
1259   switch(get_mode_sort(a->mode))
1260   {
1261     case irms_internal_boolean:
1262       return (a == tarval_b_false) ? a : b;
1263
1264     case irms_int_number:
1265       sc_and(a->value, b->value, NULL);
1266       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1267
1268     default:
1269       assert(0 && "operation not defined on mode");
1270       return tarval_bad;
1271   }
1272 }
1273
1274 /*
1275  * bitwise or
1276  */
1277 tarval *tarval_or (tarval *a, tarval *b)
1278 {
1279   ANNOUNCE();
1280   assert(a);
1281   assert(b);
1282   assert(a->mode == b->mode);
1283
1284   /* works even for vector modes */
1285
1286   switch (get_mode_sort(a->mode))
1287   {
1288     case irms_internal_boolean:
1289       return (a == tarval_b_true) ? a : b;
1290
1291     case irms_int_number:
1292       sc_or(a->value, b->value, NULL);
1293       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1294
1295     default:
1296       assert(0 && "operation not defined on mode");
1297       return tarval_bad;;
1298   }
1299 }
1300
1301 /*
1302  * bitwise exclusive or (xor)
1303  */
1304 tarval *tarval_eor(tarval *a, tarval *b)
1305 {
1306   ANNOUNCE();
1307   assert(a);
1308   assert(b);
1309   assert((a->mode == b->mode));
1310
1311   /* works even for vector modes */
1312
1313   switch (get_mode_sort(a->mode))
1314   {
1315     case irms_internal_boolean:
1316       return (a == b)? tarval_b_false : tarval_b_true;
1317
1318     case irms_int_number:
1319       sc_xor(a->value, b->value, NULL);
1320       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1321
1322     default:
1323       assert(0 && "operation not defined on mode");
1324       return tarval_bad;;
1325   }
1326 }
1327
1328 /*
1329  * bitwise left shift
1330  */
1331 tarval *tarval_shl(tarval *a, tarval *b)
1332 {
1333   char *temp_val = NULL;
1334   ANNOUNCE();
1335   assert(a);
1336   assert(b);
1337   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1338
1339   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1340     /* vector arithmetic not implemented yet */
1341     return tarval_bad;
1342   }
1343
1344   if (get_mode_modulo_shift(a->mode) != 0)
1345   {
1346     temp_val = alloca(sc_get_buffer_length());
1347
1348     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1349     sc_mod(b->value, temp_val, temp_val);
1350   }
1351   else
1352     temp_val = (char*)b->value;
1353
1354   sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1355   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1356 }
1357
1358 /*
1359  * bitwise unsigned right shift
1360  */
1361 tarval *tarval_shr(tarval *a, tarval *b)
1362 {
1363   char *temp_val = NULL;
1364   ANNOUNCE();
1365   assert(a);
1366   assert(b);
1367   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1368
1369   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1370     /* vector arithmetic not implemented yet */
1371     return tarval_bad;
1372   }
1373
1374   if (get_mode_modulo_shift(a->mode) != 0)
1375   {
1376     temp_val = alloca(sc_get_buffer_length());
1377
1378     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1379     sc_mod(b->value, temp_val, temp_val);
1380   }
1381   else
1382     temp_val = (char*)b->value;
1383
1384   sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1385   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1386 }
1387
1388 /*
1389  * bitwise signed right shift
1390  */
1391 tarval *tarval_shrs(tarval *a, tarval *b)
1392 {
1393   char *temp_val = NULL;
1394   ANNOUNCE();
1395   assert(a);
1396   assert(b);
1397   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1398
1399   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1400     /* vector arithmetic not implemented yet */
1401     return tarval_bad;
1402   }
1403
1404   if (get_mode_modulo_shift(a->mode) != 0)
1405   {
1406     temp_val = alloca(sc_get_buffer_length());
1407
1408     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1409     sc_mod(b->value, temp_val, temp_val);
1410   }
1411   else
1412     temp_val = (char*)b->value;
1413
1414   sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1415   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1416 }
1417
1418 /*
1419  * bitwise rotation
1420  */
1421 tarval *tarval_rot(tarval *a, tarval *b)
1422 {
1423   char *temp_val = NULL;
1424   ANNOUNCE();
1425   assert(a);
1426   assert(b);
1427   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1428
1429   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1430     /* vector arithmetic not implemented yet */
1431     return tarval_bad;
1432   }
1433
1434   if (get_mode_modulo_shift(a->mode) != 0)
1435   {
1436     temp_val = alloca(sc_get_buffer_length());
1437
1438     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1439     sc_mod(b->value, temp_val, temp_val);
1440   }
1441   else
1442     temp_val = (char*)b->value;
1443
1444   sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1445   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1446 }
1447
1448 /*
1449  * carry flag of the last operation
1450  */
1451 int tarval_carry(void)
1452 {
1453   return sc_had_carry();
1454 }
1455
1456 /*
1457  * Output of tarvals
1458  */
1459 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1460 {
1461   static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1462
1463   const char *str;
1464   char tv_buf[100];
1465   const tarval_mode_info *mode_info;
1466   const char *prefix, *suffix;
1467
1468   ANNOUNCE();
1469
1470   mode_info = tv->mode->tv_priv;
1471   if (! mode_info)
1472     mode_info = &default_info;
1473   prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1474   suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1475
1476   switch (get_mode_sort(tv->mode))
1477   {
1478     case irms_reference:
1479       if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1480       /* fall through */
1481     case irms_int_number:
1482     case irms_character:
1483       switch (mode_info->mode_output) {
1484
1485       case TVO_DECIMAL:
1486         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1487         break;
1488
1489       case TVO_OCTAL:
1490         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1491         break;
1492
1493       case TVO_HEX:
1494       case TVO_NATIVE:
1495       default:
1496         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1497         break;
1498       }
1499       return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1500
1501     case irms_float_number:
1502       switch (mode_info->mode_output) {
1503         case TVO_HEX:
1504           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1505
1506         case TVO_HEXFLOAT:
1507           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1508
1509         case TVO_FLOAT:
1510         case TVO_NATIVE:
1511         default:
1512           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1513       }
1514       break;
1515
1516     case irms_internal_boolean:
1517       switch (mode_info->mode_output) {
1518
1519       case TVO_DECIMAL:
1520       case TVO_OCTAL:
1521       case TVO_HEX:
1522       case TVO_BINARY:
1523         return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1524
1525       case TVO_NATIVE:
1526       default:
1527         return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1528       }
1529
1530     case irms_control_flow:
1531     case irms_memory:
1532     case irms_auxiliary:
1533       return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1534   }
1535
1536   return 0;
1537 }
1538
1539
1540 /**
1541  * Output of tarvals to stdio.
1542  */
1543 int tarval_printf(tarval *tv) {
1544   char buf[1024];
1545   int res;
1546
1547   res = tarval_snprintf(buf, sizeof(buf), tv);
1548   assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1549   printf(buf);
1550   return res;
1551 }
1552
1553
1554 char *get_tarval_bitpattern(tarval *tv)
1555 {
1556   int i, j, pos = 0;
1557   int n = get_mode_size_bits(tv->mode);
1558   int bytes = (n + 7) / 8;
1559   char *res = xmalloc((n + 1) * sizeof(char));
1560   unsigned char byte;
1561
1562   for(i = 0; i < bytes; i++) {
1563     byte = get_tarval_sub_bits(tv, i);
1564     for(j = 1; j < 256; j <<= 1)
1565       if(pos < n)
1566     res[pos++] = j & byte ? '1' : '0';
1567   }
1568
1569   res[n] = '\0';
1570
1571   return res;
1572 }
1573
1574 /*
1575  * access to the bitpattern
1576  */
1577 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1578 {
1579   switch (get_mode_sort(tv->mode)) {
1580     case irms_int_number:
1581     case irms_character:
1582       return sc_sub_bits(tv->value, tv->length, byte_ofs);
1583
1584     case irms_float_number:
1585       return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1586
1587     default:
1588       return 0;
1589   }
1590 }
1591
1592 /*
1593  * Specify the output options of one mode.
1594  *
1595  * This functions stores the modinfo, so DO NOT DESTROY it.
1596  *
1597  * Returns zero on success.
1598  */
1599 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1600 {
1601   assert(mode);
1602
1603   mode->tv_priv = modeinfo;
1604   return 0;
1605 }
1606
1607 /*
1608  * Returns the output options of one mode.
1609  *
1610  * This functions returns the mode info of a given mode.
1611  */
1612 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1613 {
1614   assert(mode);
1615
1616   return mode->tv_priv;
1617 }
1618
1619 /*
1620  * Identifying tarvals values for algebraic simplifications.
1621  *
1622  * Returns:
1623  *   - TV_CLASSIFY_NULL    for additive neutral or the NULL tarval for reference modes,
1624  *   - TV_CLASSIFY_ONE     for multiplicative neutral,
1625  *   - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1626  *   - TV_CLASSIFY_OTHER   else
1627  */
1628 tarval_classification_t classify_tarval(tarval *tv)
1629 {
1630   ANNOUNCE();
1631   if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1632
1633   if (tv == get_mode_null(tv->mode))
1634     return TV_CLASSIFY_NULL;
1635   else if (tv == get_mode_one(tv->mode))
1636     return TV_CLASSIFY_ONE;
1637   else if ((get_mode_sort(tv->mode) == irms_int_number)
1638            && (tv == new_tarval_from_long(-1, tv->mode)))
1639     return TV_CLASSIFY_ALL_ONE;
1640
1641   return TV_CLASSIFY_OTHER;
1642 }
1643
1644 /*
1645  * Returns non-zero if a given (integer) tarval has only one single bit
1646  * set.
1647  */
1648 int is_single_bit_tarval(tarval *tv) {
1649   int i, l;
1650   int bits;
1651
1652   if (!tv || tv == tarval_bad) return 0;
1653   if (! mode_is_int(tv->mode)) return 0;
1654
1655   l = get_mode_size_bytes(tv->mode);
1656   for (bits = 0, i = l - 1; i >= 0; --i) {
1657     unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1658
1659     /* check for more than one bit in these */
1660     if (v) {
1661       if (v & (v-1))
1662         return 0;
1663       if (++bits > 1)
1664         return 0;
1665     }
1666   }
1667   return bits;
1668 }
1669
1670 /*
1671  * Sets the overflow mode for integer operations.
1672  */
1673 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1674   int_overflow_mode = ov_mode;
1675 }
1676
1677 /**
1678  * Get the overflow mode for integer operations.
1679  */
1680 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1681   return int_overflow_mode;
1682 }
1683
1684 /**
1685  * default mode_info for output as HEX
1686  */
1687 static const tarval_mode_info hex_output = {
1688   TVO_HEX,
1689   "0x",
1690   NULL,
1691 };
1692
1693 /*
1694  * Initialization of the tarval module: called before init_mode()
1695  */
1696 void init_tarval_1(long null_value)
1697 {
1698   ANNOUNCE();
1699   _null_value = null_value;
1700
1701   /* initialize the sets holding the tarvals with a comparison function and
1702    * an initial size, which is the expected number of constants */
1703   tarvals = new_set(memcmp, N_CONSTANTS);
1704   values  = new_set(memcmp, N_CONSTANTS);
1705   /* init strcalc with precision of 68 to support floating point values with 64
1706    * bit mantissa (needs extra bits for rounding and overflow) */
1707   init_strcalc(68);
1708   init_fltcalc(0);
1709 }
1710
1711 /*
1712  * Initialization of the tarval module: called after init_mode()
1713  */
1714 void init_tarval_2(void)
1715 {
1716   ANNOUNCE();
1717
1718   tarval_bad->kind        = k_tarval;
1719   tarval_bad->mode        = mode_BAD;
1720   tarval_bad->value       = INT_TO_PTR(resid_tarval_bad);
1721
1722   tarval_undefined->kind  = k_tarval;
1723   tarval_undefined->mode  = mode_ANY;
1724   tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1725
1726   tarval_b_true->kind     = k_tarval;
1727   tarval_b_true->mode     = mode_b;
1728   tarval_b_true->value    = INT_TO_PTR(resid_tarval_b_true);
1729
1730   tarval_b_false->kind    = k_tarval;
1731   tarval_b_false->mode    = mode_b;
1732   tarval_b_false->value   = INT_TO_PTR(resid_tarval_b_false);
1733
1734   /*
1735    * assign output modes that are compatible with the
1736    * old implementation: Hex output
1737    */
1738   set_tarval_mode_output_option(mode_U,  &hex_output);
1739   set_tarval_mode_output_option(mode_C,  &hex_output);
1740   set_tarval_mode_output_option(mode_Bs, &hex_output);
1741   set_tarval_mode_output_option(mode_Bu, &hex_output);
1742   set_tarval_mode_output_option(mode_Hs, &hex_output);
1743   set_tarval_mode_output_option(mode_Hu, &hex_output);
1744   set_tarval_mode_output_option(mode_Is, &hex_output);
1745   set_tarval_mode_output_option(mode_Iu, &hex_output);
1746   set_tarval_mode_output_option(mode_Ls, &hex_output);
1747   set_tarval_mode_output_option(mode_Lu, &hex_output);
1748   set_tarval_mode_output_option(mode_P,  &hex_output);
1749 }
1750
1751 /* free all memory occupied by tarval. */
1752 void finish_tarval(void) {
1753   finish_strcalc ();
1754   finish_fltcalc ();
1755   del_set(tarvals); tarvals = NULL;
1756   del_set(values);  values = NULL;
1757 }
1758
1759 /****************************************************************************
1760  *   end of tv.c
1761  ****************************************************************************/