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