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