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