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