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