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