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