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