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