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