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