sign_extend() did NOT expect all upper bits cleared but clear it
[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 = NULL;   /* container for tarval structs */
102 static struct set *values = NULL;    /* 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           buffer = alloca(sc_get_buffer_length());
912           memcpy(buffer, src->value, sc_get_buffer_length());
913           sign_extend(buffer, src->mode);
914           return get_tarval_overflow(buffer, src->length, m);
915
916         case irms_internal_boolean:
917           /* XXX C semantics */
918           if (src == get_mode_null(src->mode)) return tarval_b_false;
919           else return tarval_b_true;
920
921         case irms_float_number:
922           /* XXX floating point unit does not understand internal integer
923            * representation, convert to string first, then create float from
924            * string */
925           buffer = alloca(100);
926           /* decimal string representation because hexadecimal output is
927            * interpreted unsigned by fc_val_from_str, so this is a HACK */
928           snprintf(buffer, 100, "%s",
929                    sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
930           buffer[100 - 1] = '\0';
931           switch (get_mode_size_bits(m))
932           {
933             case 32:
934               fc_val_from_str(buffer, 0, 8, 23, NULL);
935               break;
936             case 64:
937               fc_val_from_str(buffer, 0, 11, 52, NULL);
938               break;
939             case 80:
940               fc_val_from_str(buffer, 0, 15, 64, NULL);
941               break;
942           }
943           return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
944
945         case irms_reference:
946           /* allow 0 to be casted */
947           if (src == get_mode_null(src->mode))
948             return get_mode_null(m);
949           break;
950
951         default:
952           break;
953       }
954       break;
955
956     case irms_internal_boolean:
957       switch (get_mode_sort(m))
958       {
959         case irms_int_number:
960           if (src == tarval_b_true) return get_mode_one(m);
961           else return get_mode_null(m);
962
963         default:
964           break;
965       }
966       break;
967
968     case irms_character:
969       break;
970     case irms_reference:
971       break;
972   }
973
974   return tarval_bad;
975 }
976
977 /*
978  * bitwise negation
979  */
980 tarval *tarval_not(tarval *a)
981 {
982   char *buffer;
983
984   ANNOUNCE();
985   assert(a);
986
987   /* works for vector mode without changes */
988
989   switch (get_mode_sort(a->mode))
990   {
991     case irms_int_number:
992       buffer = alloca(sc_get_buffer_length());
993       sc_not(a->value, buffer);
994       return get_tarval(buffer, a->length, a->mode);
995
996     case irms_internal_boolean:
997       if (a == tarval_b_true)
998         return tarval_b_false;
999       if (a == tarval_b_false)
1000         return tarval_b_true;
1001       return tarval_bad;
1002
1003     default:
1004       assert(0 && "bitwise negation is only allowed for integer and boolean");
1005       return tarval_bad;
1006   }
1007 }
1008
1009 /*
1010  * arithmetic negation
1011  */
1012 tarval *tarval_neg(tarval *a)
1013 {
1014   char *buffer;
1015
1016   ANNOUNCE();
1017   assert(a);
1018   assert(mode_is_num(a->mode)); /* negation only for numerical values */
1019
1020   /* note: negation is allowed even for unsigned modes. */
1021
1022   if (get_mode_n_vector_elems(a->mode) > 1) {
1023     /* vector arithmetic not implemented yet */
1024     return tarval_bad;
1025   }
1026
1027   switch (get_mode_sort(a->mode))
1028   {
1029     case irms_int_number:
1030       buffer = alloca(sc_get_buffer_length());
1031       sc_neg(a->value, buffer);
1032       return get_tarval_overflow(buffer, a->length, a->mode);
1033
1034     case irms_float_number:
1035       fc_neg(a->value, NULL);
1036       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1037
1038     default:
1039       return tarval_bad;
1040   }
1041 }
1042
1043 /*
1044  * addition
1045  */
1046 tarval *tarval_add(tarval *a, tarval *b)
1047 {
1048   char *buffer;
1049
1050   ANNOUNCE();
1051   assert(a);
1052   assert(b);
1053   assert(a->mode == b->mode);
1054
1055   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1056     /* vector arithmetic not implemented yet */
1057     return tarval_bad;
1058   }
1059
1060   switch (get_mode_sort(a->mode))
1061   {
1062     case irms_character:
1063     case irms_int_number:
1064       /* modes of a,b are equal, so result has mode of a as this might be the character */
1065       buffer = alloca(sc_get_buffer_length());
1066       sc_add(a->value, b->value, buffer);
1067       return get_tarval_overflow(buffer, a->length, a->mode);
1068
1069     case irms_float_number:
1070       fc_add(a->value, b->value, NULL);
1071       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1072
1073     default:
1074       return tarval_bad;
1075   }
1076 }
1077
1078 /*
1079  * subtraction
1080  */
1081 tarval *tarval_sub(tarval *a, tarval *b)
1082 {
1083   char *buffer;
1084
1085   ANNOUNCE();
1086   assert(a);
1087   assert(b);
1088   assert(a->mode == b->mode);
1089
1090   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1091     /* vector arithmetic not implemented yet */
1092     return tarval_bad;
1093   }
1094   switch (get_mode_sort(a->mode))
1095   {
1096     case irms_character:
1097     case irms_int_number:
1098       /* modes of a,b are equal, so result has mode of a as this might be the character */
1099       buffer = alloca(sc_get_buffer_length());
1100       sc_sub(a->value, b->value, buffer);
1101       return get_tarval_overflow(buffer, a->length, a->mode);
1102
1103     case irms_float_number:
1104       fc_sub(a->value, b->value, NULL);
1105       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1106
1107     default:
1108       return tarval_bad;
1109   }
1110 }
1111
1112 /*
1113  * multiplication
1114  */
1115 tarval *tarval_mul(tarval *a, tarval *b)
1116 {
1117   char *buffer;
1118
1119   ANNOUNCE();
1120   assert(a);
1121   assert(b);
1122   assert(a->mode == b->mode);
1123
1124   if (get_mode_n_vector_elems(a->mode) > 1) {
1125     /* vector arithmetic not implemented yet */
1126     return tarval_bad;
1127   }
1128
1129   switch (get_mode_sort(a->mode))
1130   {
1131     case irms_int_number:
1132       /* modes of a,b are equal */
1133       buffer = alloca(sc_get_buffer_length());
1134       sc_mul(a->value, b->value, buffer);
1135       return get_tarval_overflow(buffer, a->length, a->mode);
1136
1137     case irms_float_number:
1138       fc_mul(a->value, b->value, NULL);
1139       return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1140
1141     default:
1142       return tarval_bad;
1143   }
1144 }
1145
1146 /*
1147  * floating point division
1148  */
1149 tarval *tarval_quo(tarval *a, tarval *b)
1150 {
1151   ANNOUNCE();
1152   assert(a);
1153   assert(b);
1154   assert((a->mode == b->mode) && mode_is_float(a->mode));
1155
1156   if (get_mode_n_vector_elems(a->mode) > 1) {
1157     /* vector arithmetic not implemented yet */
1158     return tarval_bad;
1159   }
1160
1161   fc_div(a->value, b->value, NULL);
1162   return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1163 }
1164
1165 /*
1166  * integer division
1167  * overflow is impossible, but look out for division by zero
1168  */
1169 tarval *tarval_div(tarval *a, tarval *b)
1170 {
1171   ANNOUNCE();
1172   assert(a);
1173   assert(b);
1174   assert((a->mode == b->mode) && mode_is_int(a->mode));
1175
1176   if (get_mode_n_vector_elems(a->mode) > 1) {
1177     /* vector arithmetic not implemented yet */
1178     return tarval_bad;
1179   }
1180
1181   /* x/0 error */
1182   if (b == get_mode_null(b->mode)) return tarval_bad;
1183   /* modes of a,b are equal */
1184   sc_div(a->value, b->value, NULL);
1185   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1186 }
1187
1188 /*
1189  * remainder
1190  * overflow is impossible, but look out for division by zero
1191  */
1192 tarval *tarval_mod(tarval *a, tarval *b)
1193 {
1194   ANNOUNCE();
1195   assert(a);
1196   assert(b);
1197   assert((a->mode == b->mode) && mode_is_int(a->mode));
1198
1199   if (get_mode_n_vector_elems(a->mode) > 1) {
1200     /* vector arithmetic not implemented yet */
1201     return tarval_bad;
1202   }
1203
1204   /* x/0 error */
1205   if (b == get_mode_null(b->mode)) return tarval_bad;
1206   /* modes of a,b are equal */
1207   sc_mod(a->value, b->value, NULL);
1208   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1209 }
1210
1211 /*
1212  * absolute value
1213  */
1214 tarval *tarval_abs(tarval *a)
1215 {
1216   char *buffer;
1217
1218   ANNOUNCE();
1219   assert(a);
1220   assert(mode_is_num(a->mode));
1221
1222   if (get_mode_n_vector_elems(a->mode) > 1) {
1223     /* vector arithmetic not implemented yet */
1224     return tarval_bad;
1225   }
1226
1227   switch (get_mode_sort(a->mode))
1228   {
1229     case irms_int_number:
1230       if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1231       {
1232         buffer = alloca(sc_get_buffer_length());
1233         sc_neg(a->value, buffer);
1234         return get_tarval_overflow(buffer, a->length, a->mode);
1235       }
1236       return a;
1237
1238     case irms_float_number:
1239       if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1)
1240       {
1241         fc_neg(a->value, NULL);
1242         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1243       }
1244       return a;
1245
1246     default:
1247       return tarval_bad;
1248   }
1249   return tarval_bad;
1250 }
1251
1252 /*
1253  * bitwise and
1254  */
1255 tarval *tarval_and(tarval *a, tarval *b)
1256 {
1257   ANNOUNCE();
1258   assert(a);
1259   assert(b);
1260   assert(a->mode == b->mode);
1261
1262   /* works even for vector modes */
1263
1264   switch(get_mode_sort(a->mode))
1265   {
1266     case irms_internal_boolean:
1267       return (a == tarval_b_false) ? a : b;
1268
1269     case irms_int_number:
1270       sc_and(a->value, b->value, NULL);
1271       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1272
1273     default:
1274       assert(0 && "operation not defined on mode");
1275       return tarval_bad;
1276   }
1277 }
1278
1279 /*
1280  * bitwise or
1281  */
1282 tarval *tarval_or (tarval *a, tarval *b)
1283 {
1284   ANNOUNCE();
1285   assert(a);
1286   assert(b);
1287   assert(a->mode == b->mode);
1288
1289   /* works even for vector modes */
1290
1291   switch (get_mode_sort(a->mode))
1292   {
1293     case irms_internal_boolean:
1294       return (a == tarval_b_true) ? a : b;
1295
1296     case irms_int_number:
1297       sc_or(a->value, b->value, NULL);
1298       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1299
1300     default:
1301       assert(0 && "operation not defined on mode");
1302       return tarval_bad;;
1303   }
1304 }
1305
1306 /*
1307  * bitwise exclusive or (xor)
1308  */
1309 tarval *tarval_eor(tarval *a, tarval *b)
1310 {
1311   ANNOUNCE();
1312   assert(a);
1313   assert(b);
1314   assert((a->mode == b->mode));
1315
1316   /* works even for vector modes */
1317
1318   switch (get_mode_sort(a->mode))
1319   {
1320     case irms_internal_boolean:
1321       return (a == b)? tarval_b_false : tarval_b_true;
1322
1323     case irms_int_number:
1324       sc_xor(a->value, b->value, NULL);
1325       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1326
1327     default:
1328       assert(0 && "operation not defined on mode");
1329       return tarval_bad;;
1330   }
1331 }
1332
1333 /*
1334  * bitwise left shift
1335  */
1336 tarval *tarval_shl(tarval *a, tarval *b)
1337 {
1338   char *temp_val = NULL;
1339   ANNOUNCE();
1340   assert(a);
1341   assert(b);
1342   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1343
1344   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1345     /* vector arithmetic not implemented yet */
1346     return tarval_bad;
1347   }
1348
1349   if (get_mode_modulo_shift(a->mode) != 0)
1350   {
1351     temp_val = alloca(sc_get_buffer_length());
1352
1353     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1354     sc_mod(b->value, temp_val, temp_val);
1355   }
1356   else
1357     temp_val = (char*)b->value;
1358
1359   sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1360   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1361 }
1362
1363 /*
1364  * bitwise unsigned right shift
1365  */
1366 tarval *tarval_shr(tarval *a, tarval *b)
1367 {
1368   char *temp_val = NULL;
1369   ANNOUNCE();
1370   assert(a);
1371   assert(b);
1372   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1373
1374   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1375     /* vector arithmetic not implemented yet */
1376     return tarval_bad;
1377   }
1378
1379   if (get_mode_modulo_shift(a->mode) != 0)
1380   {
1381     temp_val = alloca(sc_get_buffer_length());
1382
1383     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1384     sc_mod(b->value, temp_val, temp_val);
1385   }
1386   else
1387     temp_val = (char*)b->value;
1388
1389   sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1390   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1391 }
1392
1393 /*
1394  * bitwise signed right shift
1395  */
1396 tarval *tarval_shrs(tarval *a, tarval *b)
1397 {
1398   char *temp_val = NULL;
1399   ANNOUNCE();
1400   assert(a);
1401   assert(b);
1402   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1403
1404   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1405     /* vector arithmetic not implemented yet */
1406     return tarval_bad;
1407   }
1408
1409   if (get_mode_modulo_shift(a->mode) != 0)
1410   {
1411     temp_val = alloca(sc_get_buffer_length());
1412
1413     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1414     sc_mod(b->value, temp_val, temp_val);
1415   }
1416   else
1417     temp_val = (char*)b->value;
1418
1419   sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1420   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1421 }
1422
1423 /*
1424  * bitwise rotation
1425  */
1426 tarval *tarval_rot(tarval *a, tarval *b)
1427 {
1428   char *temp_val = NULL;
1429   ANNOUNCE();
1430   assert(a);
1431   assert(b);
1432   assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1433
1434   if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1435     /* vector arithmetic not implemented yet */
1436     return tarval_bad;
1437   }
1438
1439   if (get_mode_modulo_shift(a->mode) != 0)
1440   {
1441     temp_val = alloca(sc_get_buffer_length());
1442
1443     sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1444     sc_mod(b->value, temp_val, temp_val);
1445   }
1446   else
1447     temp_val = (char*)b->value;
1448
1449   sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1450   return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1451 }
1452
1453 /*
1454  * carry flag of the last operation
1455  */
1456 int tarval_carry(void)
1457 {
1458   return sc_had_carry();
1459 }
1460
1461 /*
1462  * Output of tarvals
1463  */
1464 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1465 {
1466   static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1467
1468   const char *str;
1469   char tv_buf[100];
1470   const tarval_mode_info *mode_info;
1471   const char *prefix, *suffix;
1472
1473   ANNOUNCE();
1474
1475   mode_info = tv->mode->tv_priv;
1476   if (! mode_info)
1477     mode_info = &default_info;
1478   prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1479   suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1480
1481   switch (get_mode_sort(tv->mode))
1482   {
1483     case irms_reference:
1484       if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1485       /* fall through */
1486     case irms_int_number:
1487     case irms_character:
1488       switch (mode_info->mode_output) {
1489
1490       case TVO_DECIMAL:
1491         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1492         break;
1493
1494       case TVO_OCTAL:
1495         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1496         break;
1497
1498       case TVO_HEX:
1499       case TVO_NATIVE:
1500       default:
1501         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1502         break;
1503       }
1504       return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1505
1506     case irms_float_number:
1507       switch (mode_info->mode_output) {
1508         case TVO_HEX:
1509           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1510
1511         case TVO_HEXFLOAT:
1512           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1513
1514         case TVO_FLOAT:
1515         case TVO_NATIVE:
1516         default:
1517           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1518       }
1519       break;
1520
1521     case irms_internal_boolean:
1522       switch (mode_info->mode_output) {
1523
1524       case TVO_DECIMAL:
1525       case TVO_OCTAL:
1526       case TVO_HEX:
1527       case TVO_BINARY:
1528         return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1529
1530       case TVO_NATIVE:
1531       default:
1532         return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1533       }
1534
1535     case irms_control_flow:
1536     case irms_memory:
1537     case irms_auxiliary:
1538       return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1539   }
1540
1541   return 0;
1542 }
1543
1544
1545 /**
1546  * Output of tarvals to stdio.
1547  */
1548 int tarval_printf(tarval *tv) {
1549   char buf[1024];
1550   int res;
1551
1552   res = tarval_snprintf(buf, sizeof(buf), tv);
1553   assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1554   printf(buf);
1555   return res;
1556 }
1557
1558
1559 char *get_tarval_bitpattern(tarval *tv)
1560 {
1561   int i, j, pos = 0;
1562   int n = get_mode_size_bits(tv->mode);
1563   int bytes = (n + 7) / 8;
1564   char *res = xmalloc((n + 1) * sizeof(char));
1565   unsigned char byte;
1566
1567   for(i = 0; i < bytes; i++) {
1568     byte = get_tarval_sub_bits(tv, i);
1569     for(j = 1; j < 256; j <<= 1)
1570       if(pos < n)
1571     res[pos++] = j & byte ? '1' : '0';
1572   }
1573
1574   res[n] = '\0';
1575
1576   return res;
1577 }
1578
1579 /*
1580  * access to the bitpattern
1581  */
1582 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1583 {
1584   switch (get_mode_sort(tv->mode)) {
1585     case irms_int_number:
1586     case irms_character:
1587       return sc_sub_bits(tv->value, tv->length, byte_ofs);
1588
1589     case irms_float_number:
1590       return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1591
1592     default:
1593       return 0;
1594   }
1595 }
1596
1597 /*
1598  * Specify the output options of one mode.
1599  *
1600  * This functions stores the modinfo, so DO NOT DESTROY it.
1601  *
1602  * Returns zero on success.
1603  */
1604 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1605 {
1606   assert(mode);
1607
1608   mode->tv_priv = modeinfo;
1609   return 0;
1610 }
1611
1612 /*
1613  * Returns the output options of one mode.
1614  *
1615  * This functions returns the mode info of a given mode.
1616  */
1617 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1618 {
1619   assert(mode);
1620
1621   return mode->tv_priv;
1622 }
1623
1624 /*
1625  * Identifying tarvals values for algebraic simplifications.
1626  *
1627  * Returns:
1628  *   - TV_CLASSIFY_NULL    for additive neutral or the NULL tarval for reference modes,
1629  *   - TV_CLASSIFY_ONE     for multiplicative neutral,
1630  *   - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1631  *   - TV_CLASSIFY_OTHER   else
1632  */
1633 tarval_classification_t classify_tarval(tarval *tv)
1634 {
1635   ANNOUNCE();
1636   if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1637
1638   if (tv == get_mode_null(tv->mode))
1639     return TV_CLASSIFY_NULL;
1640   else if (tv == get_mode_one(tv->mode))
1641     return TV_CLASSIFY_ONE;
1642   else if ((get_mode_sort(tv->mode) == irms_int_number)
1643            && (tv == new_tarval_from_long(-1, tv->mode)))
1644     return TV_CLASSIFY_ALL_ONE;
1645
1646   return TV_CLASSIFY_OTHER;
1647 }
1648
1649 /*
1650  * Returns non-zero if a given (integer) tarval has only one single bit
1651  * set.
1652  */
1653 int is_single_bit_tarval(tarval *tv) {
1654   int i, l;
1655   int bits;
1656
1657   if (!tv || tv == tarval_bad) return 0;
1658   if (! mode_is_int(tv->mode)) return 0;
1659
1660   l = get_mode_size_bytes(tv->mode);
1661   for (bits = 0, i = l - 1; i >= 0; --i) {
1662     unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1663
1664     /* check for more than one bit in these */
1665     if (v) {
1666       if (v & (v-1))
1667         return 0;
1668       if (++bits > 1)
1669         return 0;
1670     }
1671   }
1672   return bits;
1673 }
1674
1675 /*
1676  * Sets the overflow mode for integer operations.
1677  */
1678 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1679   int_overflow_mode = ov_mode;
1680 }
1681
1682 /**
1683  * Get the overflow mode for integer operations.
1684  */
1685 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1686   return int_overflow_mode;
1687 }
1688
1689 /**
1690  * default mode_info for output as HEX
1691  */
1692 static const tarval_mode_info hex_output = {
1693   TVO_HEX,
1694   "0x",
1695   NULL,
1696 };
1697
1698 /*
1699  * Initialization of the tarval module: called before init_mode()
1700  */
1701 void init_tarval_1(long null_value)
1702 {
1703   ANNOUNCE();
1704   _null_value = null_value;
1705
1706   /* initialize the sets holding the tarvals with a comparison function and
1707    * an initial size, which is the expected number of constants */
1708   tarvals = new_set(memcmp, N_CONSTANTS);
1709   values  = new_set(memcmp, N_CONSTANTS);
1710   /* init strcalc with precision of 68 to support floating point values with 64
1711    * bit mantissa (needs extra bits for rounding and overflow) */
1712   init_strcalc(68);
1713   init_fltcalc(0);
1714 }
1715
1716 /*
1717  * Initialization of the tarval module: called after init_mode()
1718  */
1719 void init_tarval_2(void)
1720 {
1721   ANNOUNCE();
1722
1723   tarval_bad->kind        = k_tarval;
1724   tarval_bad->mode        = mode_BAD;
1725   tarval_bad->value       = INT_TO_PTR(resid_tarval_bad);
1726
1727   tarval_undefined->kind  = k_tarval;
1728   tarval_undefined->mode  = mode_ANY;
1729   tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1730
1731   tarval_b_true->kind     = k_tarval;
1732   tarval_b_true->mode     = mode_b;
1733   tarval_b_true->value    = INT_TO_PTR(resid_tarval_b_true);
1734
1735   tarval_b_false->kind    = k_tarval;
1736   tarval_b_false->mode    = mode_b;
1737   tarval_b_false->value   = INT_TO_PTR(resid_tarval_b_false);
1738
1739   /*
1740    * assign output modes that are compatible with the
1741    * old implementation: Hex output
1742    */
1743   set_tarval_mode_output_option(mode_U,  &hex_output);
1744   set_tarval_mode_output_option(mode_C,  &hex_output);
1745   set_tarval_mode_output_option(mode_Bs, &hex_output);
1746   set_tarval_mode_output_option(mode_Bu, &hex_output);
1747   set_tarval_mode_output_option(mode_Hs, &hex_output);
1748   set_tarval_mode_output_option(mode_Hu, &hex_output);
1749   set_tarval_mode_output_option(mode_Is, &hex_output);
1750   set_tarval_mode_output_option(mode_Iu, &hex_output);
1751   set_tarval_mode_output_option(mode_Ls, &hex_output);
1752   set_tarval_mode_output_option(mode_Lu, &hex_output);
1753   set_tarval_mode_output_option(mode_P,  &hex_output);
1754 }
1755
1756 /* free all memory occupied by tarval. */
1757 void finish_tarval(void) {
1758   finish_strcalc ();
1759   finish_fltcalc ();
1760   del_set(tarvals); tarvals = NULL;
1761   del_set(values);  values = NULL;
1762 }
1763
1764 /****************************************************************************
1765  *   end of tv.c
1766  ****************************************************************************/