Lets zeor/sign extend sc_val_to_long()
[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, get_mode_size_bits(tv->mode), mode_is_signed(tv->mode));
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  * Special value query functions ============================================
419  *
420  * These functions calculate and return a tarval representing the requested
421  * value.
422  * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
423  * functions, but these are stored on initialization of the irmode module and
424  * therefore the irmode functions should be prefered to the functions below.
425  */
426
427 tarval *get_tarval_bad(void)
428 {
429   ANNOUNCE();
430   return tarval_bad;
431 }
432 tarval *get_tarval_undefined(void)
433 {
434   ANNOUNCE();
435   return tarval_undefined;
436 }
437 tarval *get_tarval_b_false(void)
438 {
439   ANNOUNCE();
440   return tarval_b_false;
441 }
442 tarval *get_tarval_b_true(void)
443 {
444   ANNOUNCE();
445   return tarval_b_true;
446 }
447 tarval *get_tarval_P_void(void)
448 {
449   ANNOUNCE();
450   return tarval_P_void;
451 }
452
453 tarval *get_tarval_max(ir_mode *mode)
454 {
455   ANNOUNCE();
456   assert(mode);
457
458   if (get_mode_n_vector_elems(mode) > 1) {
459     /* vector arithmetic not implemented yet */
460     return tarval_bad;
461   }
462
463   switch(get_mode_sort(mode))
464   {
465     case irms_reference:
466     case irms_control_flow:
467     case irms_memory:
468     case irms_auxiliary:
469       assert(0);
470       break;
471
472     case irms_internal_boolean:
473       return tarval_b_true;
474
475     case irms_float_number:
476       switch(get_mode_size_bits(mode))
477       {
478         case 32:
479           fc_get_max(8, 23, NULL);
480           break;
481         case 64:
482           fc_get_max(11, 52, NULL);
483           break;
484         case 80:
485           fc_get_max(15, 64, NULL);
486           break;
487       }
488       return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
489
490     case irms_int_number:
491     case irms_character:
492       sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
493       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
494   }
495   return tarval_bad;
496 }
497
498 tarval *get_tarval_min(ir_mode *mode)
499 {
500   ANNOUNCE();
501   assert(mode);
502
503   if (get_mode_n_vector_elems(mode) > 1) {
504     /* vector arithmetic not implemented yet */
505     return tarval_bad;
506   }
507
508   switch(get_mode_sort(mode))
509   {
510     case irms_reference:
511     case irms_control_flow:
512     case irms_memory:
513     case irms_auxiliary:
514       assert(0);
515       break;
516
517     case irms_internal_boolean:
518       return tarval_b_false;
519
520     case irms_float_number:
521       switch(get_mode_size_bits(mode))
522       {
523         case 32:
524           fc_get_min(8, 23, NULL);
525           break;
526         case 64:
527           fc_get_min(11, 52, NULL);
528           break;
529         case 80:
530           fc_get_min(15, 64, NULL);
531           break;
532       }
533       return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
534
535     case irms_int_number:
536     case irms_character:
537       sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
538       return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
539   }
540   return tarval_bad;
541 }
542
543 tarval *get_tarval_null(ir_mode *mode)
544 {
545   ANNOUNCE();
546   assert(mode);
547
548   if (get_mode_n_vector_elems(mode) > 1) {
549     /* vector arithmetic not implemented yet */
550     return tarval_bad;
551   }
552
553   switch(get_mode_sort(mode))
554   {
555     case irms_control_flow:
556     case irms_memory:
557     case irms_auxiliary:
558     case irms_internal_boolean:
559       assert(0);
560       break;
561
562     case irms_float_number:
563       return new_tarval_from_double(0.0, mode);
564
565     case irms_int_number:
566     case irms_character:
567       return new_tarval_from_long(0l,  mode);
568
569     case irms_reference:
570       return tarval_P_void;
571   }
572   return tarval_bad;
573 }
574
575 tarval *get_tarval_one(ir_mode *mode)
576 {
577   ANNOUNCE();
578   assert(mode);
579
580   if (get_mode_n_vector_elems(mode) > 1) {
581     /* vector arithmetic not implemented yet */
582     return tarval_bad;
583   }
584
585   switch(get_mode_sort(mode))
586   {
587     case irms_control_flow:
588     case irms_memory:
589     case irms_auxiliary:
590     case irms_internal_boolean:
591     case irms_reference:
592       assert(0);
593       break;
594
595     case irms_float_number:
596       return new_tarval_from_double(1.0, mode);
597
598     case irms_int_number:
599     case irms_character:
600       return new_tarval_from_long(1l, mode);
601       break;
602   }
603   return tarval_bad;
604 }
605
606 tarval *get_tarval_nan(ir_mode *mode)
607 {
608   ANNOUNCE();
609   assert(mode);
610
611   if (get_mode_n_vector_elems(mode) > 1) {
612     /* vector arithmetic not implemented yet */
613     return tarval_bad;
614   }
615
616   if (get_mode_sort(mode) == irms_float_number) {
617     switch(get_mode_size_bits(mode))
618     {
619       case 32:
620         fc_get_qnan(8, 23, NULL);
621         break;
622       case 64:
623         fc_get_qnan(11, 52, NULL);
624         break;
625       case 80:
626         fc_get_qnan(15, 64, NULL);
627         break;
628     }
629     return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
630   }
631   else {
632     assert(0 && "tarval is not floating point");
633     return tarval_bad;
634   }
635 }
636
637 tarval *get_tarval_inf(ir_mode *mode)
638 {
639   ANNOUNCE();
640   assert(mode);
641
642   if (get_mode_n_vector_elems(mode) > 1) {
643     /* vector arithmetic not implemented yet */
644     return tarval_bad;
645   }
646
647   if (get_mode_sort(mode) == irms_float_number) {
648     switch(get_mode_size_bits(mode))
649     {
650       case 32:
651         fc_get_plusinf(8, 23, NULL);
652         break;
653       case 64:
654         fc_get_plusinf(11, 52, NULL);
655         break;
656       case 80:
657         fc_get_plusinf(15, 64, NULL);
658         break;
659     }
660     return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
661   }
662   else {
663     assert(0 && "tarval is not floating point");
664     return tarval_bad;
665   }
666 }
667
668 /*
669  * Arithmethic operations on tarvals ========================================
670  */
671
672 /*
673  * test if negative number, 1 means 'yes'
674  */
675 int tarval_is_negative(tarval *a)
676 {
677   ANNOUNCE();
678   assert(a);
679
680   if (get_mode_n_vector_elems(a->mode) > 1) {
681     /* vector arithmetic not implemented yet */
682     assert(0 && "tarval_is_negative is not allowed for vector modes");
683     return 0;
684   }
685
686   switch (get_mode_sort(a->mode))
687   {
688     case irms_int_number:
689       if (!mode_is_signed(a->mode)) return 0;
690       else
691     return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
692
693     case irms_float_number:
694       return fc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
695
696     default:
697       assert(0 && "not implemented");
698       return 0;
699   }
700 }
701
702 /*
703  * test if null, 1 means 'yes'
704  */
705 int tarval_is_null(tarval *a)
706 {
707   ir_mode *m = get_tarval_mode(a);
708
709   return a == get_tarval_null(m);
710 }
711
712 /*
713  * test if one, 1 means 'yes'
714  */
715 int tarval_is_one(tarval *a)
716 {
717   ir_mode *m = get_tarval_mode(a);
718
719   return a == get_tarval_one(m);
720 }
721
722 /*
723  * comparison
724  */
725 pnc_number tarval_cmp(tarval *a, tarval *b)
726 {
727   ANNOUNCE();
728   assert(a);
729   assert(b);
730
731   if (a == tarval_bad || b == tarval_bad) assert(0 && "Comparison with tarval_bad");
732   if (a == tarval_undefined || b == tarval_undefined) return False;
733   if (a == b) return Eq;
734   if (a->mode != b->mode) return False;
735
736   if (get_mode_n_vector_elems(a->mode) > 1) {
737     /* vector arithmetic not implemented yet */
738     assert(0 && "cmp not implemented for vector modes");
739   }
740
741   /* Here the two tarvals are unequal and of the same mode */
742   switch (get_mode_sort(a->mode))
743   {
744     case irms_control_flow:
745     case irms_memory:
746     case irms_auxiliary:
747     case irms_reference:
748       return False;
749
750     case irms_float_number:
751       switch (fc_comp(a->value, b->value)) {
752         case -1: return Lt;
753         case  0: assert(0 && "different tarvals compare equal"); return Eq;
754         case  1: return Gt;
755         case  2: return Uo;
756         default: return False;
757       }
758     case irms_int_number:
759     case irms_character:
760       return (sc_comp(a->value, b->value)==1)?(Gt):(Lt);
761
762     case irms_internal_boolean:
763       return (a == tarval_b_true)?(Gt):(Lt);
764   }
765   return False;
766 }
767
768 /*
769  * convert to other mode
770  */
771 tarval *tarval_convert_to(tarval *src, ir_mode *m)
772 {
773   char *buffer;
774
775   ANNOUNCE();
776   assert(src);
777   assert(m);
778
779   if (src->mode == m) return src;
780
781   if (get_mode_n_vector_elems(src->mode) > 1) {
782     /* vector arithmetic not implemented yet */
783     return tarval_bad;
784   }
785
786   switch (get_mode_sort(src->mode))
787   {
788     case irms_control_flow:
789     case irms_memory:
790     case irms_auxiliary:
791       break;
792
793     /* cast float to something */
794     case irms_float_number:
795       switch (get_mode_sort(m)) {
796         case irms_float_number:
797           switch (get_mode_size_bits(m))
798           {
799             case 32:
800               fc_cast(src->value, 8, 23, NULL);
801               break;
802             case 64:
803               fc_cast(src->value, 11, 52, NULL);
804               break;
805             case 80:
806               fc_cast(src->value, 15, 64, NULL);
807               break;
808             default:
809               break;
810           }
811           return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
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
831         default:
832           /* the rest can't be converted */
833           return tarval_bad;
834       }
835       break;
836
837     /* cast int to something */
838     case irms_int_number:
839       switch (get_mode_sort(m)) {
840         case irms_int_number:
841         case irms_character:
842           return get_tarval_overflow(src->value, src->length, m);
843
844         case irms_internal_boolean:
845           /* XXX C semantics */
846           if (src == get_mode_null(src->mode)) return tarval_b_false;
847           else return tarval_b_true;
848
849         case irms_float_number:
850           /* XXX floating point unit does not understand internal integer
851            * representation, convert to string first, then create float from
852            * string */
853           buffer = alloca(100);
854           /* decimal string representation because hexadecimal output is
855            * interpreted unsigned by fc_val_from_str, so this is a HACK */
856           snprintf(buffer, 100, "%s",
857                    sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC));
858           switch (get_mode_size_bits(m))
859           {
860             case 32:
861               fc_val_from_str(buffer, 0, 8, 23, NULL);
862               break;
863             case 64:
864               fc_val_from_str(buffer, 0, 11, 52, NULL);
865               break;
866             case 80:
867               fc_val_from_str(buffer, 0, 15, 64, NULL);
868               break;
869           }
870           return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
871
872         default:
873           break;
874       }
875       break;
876
877     case irms_internal_boolean:
878       switch (get_mode_sort(m))
879       {
880         case irms_int_number:
881           if (src == tarval_b_true) return get_mode_one(m);
882           else return get_mode_null(m);
883
884         default:
885           break;
886       }
887       break;
888
889     case irms_character:
890       break;
891     case irms_reference:
892       break;
893   }
894
895   return tarval_bad;
896 }
897
898 /*
899  * bitwise negation
900  */
901 tarval *tarval_not(tarval *a)
902 {
903   char *buffer;
904
905   ANNOUNCE();
906   assert(a);
907   assert(mode_is_int(a->mode)); /* bitwise negation is only allowed for integer */
908
909   /* works for vector mode without changes */
910
911   switch (get_mode_sort(a->mode))
912   {
913     case irms_int_number:
914       buffer = alloca(sc_get_buffer_length());
915       sc_not(a->value, buffer);
916       return get_tarval(buffer, a->length, a->mode);
917
918     default:
919       return tarval_bad;
920   }
921 }
922
923 /*
924  * arithmetic negation
925  */
926 tarval *tarval_neg(tarval *a)
927 {
928   char *buffer;
929
930   ANNOUNCE();
931   assert(a);
932   assert(mode_is_num(a->mode)); /* negation only for numerical values */
933
934   /* note: negation is allowed even for unsigned modes. */
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);
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);
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);
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  * carry flag of the last operation
1369  */
1370 int tarval_carry(void)
1371 {
1372   return sc_had_carry();
1373 }
1374
1375 /*
1376  * Output of tarvals
1377  */
1378 int tarval_snprintf(char *buf, size_t len, tarval *tv)
1379 {
1380   static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1381
1382   const char *str;
1383   char tv_buf[100];
1384   const tarval_mode_info *mode_info;
1385   const char *prefix, *suffix;
1386
1387   ANNOUNCE();
1388
1389   mode_info = tv->mode->tv_priv;
1390   if (! mode_info)
1391     mode_info = &default_info;
1392   prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1393   suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1394
1395   switch (get_mode_sort(tv->mode))
1396   {
1397     case irms_int_number:
1398     case irms_character:
1399       switch (mode_info->mode_output) {
1400
1401       case TVO_DECIMAL:
1402         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC);
1403     break;
1404
1405       case TVO_OCTAL:
1406         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT);
1407     break;
1408
1409       case TVO_HEX:
1410       case TVO_NATIVE:
1411       default:
1412         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX);
1413     break;
1414       }
1415       return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1416
1417     case irms_float_number:
1418       switch (mode_info->mode_output) {
1419         case TVO_HEX:
1420           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1421
1422         case TVO_HEXFLOAT:
1423           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1424
1425         case TVO_FLOAT:
1426         case TVO_NATIVE:
1427         default:
1428           return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1429       }
1430       break;
1431
1432     case irms_reference:
1433       if (tv == tarval_P_void) return snprintf(buf, len, "NULL");
1434       if (tv->value != NULL){
1435           if (len > tv->length) {
1436             memcpy(buf, tv->value, tv->length);
1437             buf[tv->length] = '\0';
1438           }
1439           else {
1440             /* truncated */
1441             memcpy(buf, tv->value, len-1);
1442             buf[len-1] = '\0';
1443           }
1444           return tv->length;
1445          }
1446       else
1447         return snprintf(buf, len, "void");
1448
1449     case irms_internal_boolean:
1450       switch (mode_info->mode_output) {
1451
1452       case TVO_DECIMAL:
1453       case TVO_OCTAL:
1454       case TVO_HEX:
1455       case TVO_BINARY:
1456         return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1457
1458       case TVO_NATIVE:
1459       default:
1460         return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1461       }
1462
1463     case irms_control_flow:
1464     case irms_memory:
1465     case irms_auxiliary:
1466       return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1467   }
1468
1469   return 0;
1470 }
1471
1472
1473 /**
1474  * Output of tarvals to stdio.
1475  */
1476 int tarval_printf(tarval *tv) {
1477   char buf[1024];
1478   int res;
1479
1480   res = tarval_snprintf(buf, sizeof(buf), tv);
1481   assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1482   printf(buf);
1483   return res;
1484 }
1485
1486
1487 char *get_tarval_bitpattern(tarval *tv)
1488 {
1489   int i, j, pos = 0;
1490   int n = get_mode_size_bits(tv->mode);
1491   int bytes = (n + 7) / 8;
1492   char *res = malloc((n + 1) * sizeof(char));
1493   unsigned char byte;
1494
1495   for(i = 0; i < bytes; i++) {
1496     byte = get_tarval_sub_bits(tv, i);
1497     for(j = 1; j < 256; j <<= 1)
1498       if(pos < n)
1499         res[pos++] = j & byte ? '1' : '0';
1500   }
1501
1502   res[n] = '\0';
1503
1504   return res;
1505 }
1506
1507 /*
1508  * access to the bitpattern
1509  */
1510 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs)
1511 {
1512   switch (get_mode_sort(tv->mode)) {
1513     case irms_int_number:
1514     case irms_character:
1515       return sc_sub_bits(tv->value, tv->length, byte_ofs);
1516
1517     case irms_float_number:
1518       return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1519
1520     default:
1521       return 0;
1522   }
1523 }
1524
1525 /*
1526  * Specify the output options of one mode.
1527  *
1528  * This functions stores the modinfo, so DO NOT DESTROY it.
1529  *
1530  * Returns zero on success.
1531  */
1532 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1533 {
1534   assert(mode);
1535
1536   mode->tv_priv = modeinfo;
1537   return 0;
1538 }
1539
1540 /*
1541  * Returns the output options of one mode.
1542  *
1543  * This functions returns the modinfo of a given mode.
1544  */
1545 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1546 {
1547   assert(mode);
1548
1549   return mode->tv_priv;
1550 }
1551
1552 /*
1553  * Identifying tarvals values for algebraic simplifications.
1554  *
1555  * Returns:
1556  *   - TV_CLASSIFY_NULL    for additive neutral,
1557  *   - TV_CLASSIFY_ONE     for multiplicative neutral,
1558  *   - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1559  *   - TV_CLASSIFY_OTHER   else
1560  */
1561 tarval_classification_t classify_tarval(tarval *tv)
1562 {
1563   ANNOUNCE();
1564   if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1565
1566   if (tv == get_mode_null(tv->mode))
1567     return TV_CLASSIFY_NULL;
1568   else if (tv == get_mode_one(tv->mode))
1569     return TV_CLASSIFY_ONE;
1570   else if ((get_mode_sort(tv->mode) == irms_int_number)
1571            && (tv == new_tarval_from_long(-1, tv->mode)))
1572     return TV_CLASSIFY_ALL_ONE;
1573
1574   return TV_CLASSIFY_OTHER;
1575 }
1576
1577 /**
1578  * Sets the overflow mode for integer operations.
1579  */
1580 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1581   int_overflow_mode = ov_mode;
1582 }
1583
1584 /**
1585  * Get the overflow mode for integer operations.
1586  */
1587 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1588   return int_overflow_mode;
1589 }
1590
1591 /**
1592  * default mode_info for output as HEX
1593  */
1594 static const tarval_mode_info hex_output = {
1595   TVO_HEX,
1596   "0x",
1597   NULL,
1598 };
1599
1600 /**
1601  * default mode_info for output as reference
1602  */
1603 static const tarval_mode_info reference_output = {
1604   TVO_NATIVE,
1605   "&(",
1606   ")",
1607 };
1608
1609
1610 /*
1611  * Initialization of the tarval module: called before init_mode()
1612  */
1613 void init_tarval_1(void)
1614 {
1615   ANNOUNCE();
1616   /* initialize the sets holding the tarvals with a comparison function and
1617    * an initial size, which is the expected number of constants */
1618   tarvals = new_set(memcmp, N_CONSTANTS);
1619   values  = new_set(memcmp, N_CONSTANTS);
1620   /* init strcalc with precision of 68 to support floating point values with 64
1621    * bit mantissa (needs extra bits for rounding and overflow) */
1622   init_strcalc(68);
1623   init_fltcalc(0);
1624 }
1625
1626 /*
1627  * Initialization of the tarval module: called after init_mode()
1628  */
1629 void init_tarval_2(void)
1630 {
1631   ANNOUNCE();
1632
1633   tarval_bad->mode       = mode_BAD;
1634   tarval_undefined->mode = mode_ANY;
1635   tarval_b_true->mode    = mode_b;
1636   tarval_b_false->mode   = mode_b;
1637   tarval_P_void->mode    = mode_P;
1638
1639   /*
1640    * assign output modes that are compatible with the
1641    * old implementation: Hex output
1642    */
1643   set_tarval_mode_output_option(mode_U,  &hex_output);
1644   set_tarval_mode_output_option(mode_C,  &hex_output);
1645   set_tarval_mode_output_option(mode_Bs, &hex_output);
1646   set_tarval_mode_output_option(mode_Bu, &hex_output);
1647   set_tarval_mode_output_option(mode_Hs, &hex_output);
1648   set_tarval_mode_output_option(mode_Hu, &hex_output);
1649   set_tarval_mode_output_option(mode_Is, &hex_output);
1650   set_tarval_mode_output_option(mode_Iu, &hex_output);
1651   set_tarval_mode_output_option(mode_Ls, &hex_output);
1652   set_tarval_mode_output_option(mode_Lu, &hex_output);
1653   set_tarval_mode_output_option(mode_P,  &reference_output);
1654 }
1655
1656 /* free all memory occupied by tarval. */
1657 void finish_tarval(void) {
1658   finish_strcalc ();
1659   finish_fltcalc ();
1660   del_set(tarvals); tarvals = NULL;
1661   del_set(values);  values = NULL;
1662 }
1663
1664 /****************************************************************************
1665  *   end of tv.c
1666  ****************************************************************************/