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