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