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