Use libFirm's obst.h instead of obstack.h
[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/characters to something */
832         case irms_int_number:
833         case irms_character:
834                 switch (get_mode_sort(m)) {
835                 case irms_int_number:
836                 case irms_character:
837                         buffer = alloca(sc_get_buffer_length());
838                         memcpy(buffer, src->value, sc_get_buffer_length());
839                         sign_extend(buffer, src->mode);
840                         return get_tarval_overflow(buffer, src->length, m);
841
842                 case irms_internal_boolean:
843                         /* XXX C semantics */
844                         if (src == get_mode_null(src->mode)) return tarval_b_false;
845                         else return tarval_b_true;
846
847                 case irms_float_number:
848                         /* XXX floating point unit does not understand internal integer
849                          * representation, convert to string first, then create float from
850                          * string */
851                         buffer = alloca(100);
852                         /* decimal string representation because hexadecimal output is
853                          * interpreted unsigned by fc_val_from_str, so this is a HACK */
854                         snprintf(buffer, 100, "%s",
855                                 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
856                         buffer[100 - 1] = '\0';
857                         switch (get_mode_size_bits(m)) {
858                         case 32:
859                                 fc_val_from_str(buffer, 0, 8, 23, NULL);
860                                 break;
861                         case 64:
862                                 fc_val_from_str(buffer, 0, 11, 52, NULL);
863                                 break;
864                         case 80:
865                                 fc_val_from_str(buffer, 0, 15, 64, NULL);
866                                 break;
867                         }
868                         return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
869
870                 case irms_reference:
871                         /* allow 0 to be casted */
872                         if (src == get_mode_null(src->mode))
873                                 return get_mode_null(m);
874                         break;
875
876                 default:
877                         break;
878                 }
879                 break;
880
881         case irms_internal_boolean:
882                 switch (get_mode_sort(m)) {
883                 case irms_int_number:
884                         if (src == tarval_b_true) return get_mode_one(m);
885                         else return get_mode_null(m);
886
887                 default:
888                         break;
889                 }
890                 break;
891
892         case irms_reference:
893                 break;
894         }
895
896         return tarval_bad;
897 }
898
899 /*
900  * bitwise negation
901  */
902 tarval *tarval_not(tarval *a) {
903         char *buffer;
904
905         assert(a);
906
907         /* works for vector mode without changes */
908
909         switch (get_mode_sort(a->mode)) {
910         case irms_int_number:
911                 buffer = alloca(sc_get_buffer_length());
912                 sc_not(a->value, buffer);
913                 return get_tarval(buffer, a->length, a->mode);
914
915         case irms_internal_boolean:
916                 if (a == tarval_b_true)
917                         return tarval_b_false;
918                 if (a == tarval_b_false)
919                         return tarval_b_true;
920                 return tarval_bad;
921
922         default:
923                 assert(0 && "bitwise negation is only allowed for integer and boolean");
924                 return tarval_bad;
925         }
926 }
927
928 /*
929  * arithmetic negation
930  */
931 tarval *tarval_neg(tarval *a) {
932         char *buffer;
933
934         assert(a);
935         assert(mode_is_num(a->mode)); /* negation only for numerical values */
936
937         /* note: negation is allowed even for unsigned modes. */
938
939         if (get_mode_n_vector_elems(a->mode) > 1) {
940                 /* vector arithmetic not implemented yet */
941                 return tarval_bad;
942         }
943
944         switch (get_mode_sort(a->mode)) {
945         case irms_int_number:
946                 buffer = alloca(sc_get_buffer_length());
947                 sc_neg(a->value, buffer);
948                 return get_tarval_overflow(buffer, a->length, a->mode);
949
950         case irms_float_number:
951                 fc_neg(a->value, NULL);
952                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
953
954         default:
955                 return tarval_bad;
956         }
957 }
958
959 /*
960  * addition
961  */
962 tarval *tarval_add(tarval *a, tarval *b) {
963         char *buffer;
964
965         assert(a);
966         assert(b);
967         assert(a->mode == b->mode);
968
969         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
970                 /* vector arithmetic not implemented yet */
971                 return tarval_bad;
972         }
973
974         switch (get_mode_sort(a->mode)) {
975         case irms_character:
976         case irms_int_number:
977                 /* modes of a,b are equal, so result has mode of a as this might be the character */
978                 buffer = alloca(sc_get_buffer_length());
979                 sc_add(a->value, b->value, buffer);
980                 return get_tarval_overflow(buffer, a->length, a->mode);
981
982         case irms_float_number:
983                 fc_add(a->value, b->value, NULL);
984                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
985
986         default:
987                 return tarval_bad;
988         }
989 }
990
991 /*
992  * subtraction
993  */
994 tarval *tarval_sub(tarval *a, tarval *b) {
995         char *buffer;
996
997         assert(a);
998         assert(b);
999         assert(a->mode == b->mode);
1000
1001         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1002                 /* vector arithmetic not implemented yet */
1003                 return tarval_bad;
1004         }
1005         switch (get_mode_sort(a->mode)) {
1006         case irms_character:
1007         case irms_int_number:
1008                 /* modes of a,b are equal, so result has mode of a as this might be the character */
1009                 buffer = alloca(sc_get_buffer_length());
1010                 sc_sub(a->value, b->value, buffer);
1011                 return get_tarval_overflow(buffer, a->length, a->mode);
1012
1013         case irms_float_number:
1014                 fc_sub(a->value, b->value, NULL);
1015                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1016
1017         default:
1018                 return tarval_bad;
1019         }
1020 }
1021
1022 /*
1023  * multiplication
1024  */
1025 tarval *tarval_mul(tarval *a, tarval *b) {
1026         char *buffer;
1027
1028         assert(a);
1029         assert(b);
1030         assert(a->mode == b->mode);
1031
1032         if (get_mode_n_vector_elems(a->mode) > 1) {
1033                 /* vector arithmetic not implemented yet */
1034                 return tarval_bad;
1035         }
1036
1037         switch (get_mode_sort(a->mode)) {
1038         case irms_int_number:
1039                 /* modes of a,b are equal */
1040                 buffer = alloca(sc_get_buffer_length());
1041                 sc_mul(a->value, b->value, buffer);
1042                 return get_tarval_overflow(buffer, a->length, a->mode);
1043
1044         case irms_float_number:
1045                 fc_mul(a->value, b->value, NULL);
1046                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1047
1048         default:
1049                 return tarval_bad;
1050         }
1051 }
1052
1053 /*
1054  * floating point division
1055  */
1056 tarval *tarval_quo(tarval *a, tarval *b) {
1057         assert(a);
1058         assert(b);
1059         assert((a->mode == b->mode) && mode_is_float(a->mode));
1060
1061         if (get_mode_n_vector_elems(a->mode) > 1) {
1062                 /* vector arithmetic not implemented yet */
1063                 return tarval_bad;
1064         }
1065
1066         fc_div(a->value, b->value, NULL);
1067         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1068 }
1069
1070 /*
1071  * integer division
1072  * overflow is impossible, but look out for division by zero
1073  */
1074 tarval *tarval_div(tarval *a, tarval *b) {
1075         assert(a);
1076         assert(b);
1077         assert((a->mode == b->mode) && mode_is_int(a->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         /* x/0 error */
1085         if (b == get_mode_null(b->mode)) return tarval_bad;
1086         /* modes of a,b are equal */
1087         sc_div(a->value, b->value, NULL);
1088         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1089 }
1090
1091 /*
1092  * remainder
1093  * overflow is impossible, but look out for division by zero
1094  */
1095 tarval *tarval_mod(tarval *a, tarval *b) {
1096         assert(a);
1097         assert(b);
1098         assert((a->mode == b->mode) && mode_is_int(a->mode));
1099
1100         if (get_mode_n_vector_elems(a->mode) > 1) {
1101                 /* vector arithmetic not implemented yet */
1102                 return tarval_bad;
1103         }
1104
1105         /* x/0 error */
1106         if (b == get_mode_null(b->mode)) return tarval_bad;
1107         /* modes of a,b are equal */
1108         sc_mod(a->value, b->value, NULL);
1109         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1110 }
1111
1112 /*
1113  * absolute value
1114  */
1115 tarval *tarval_abs(tarval *a) {
1116         char *buffer;
1117
1118         assert(a);
1119         assert(mode_is_num(a->mode));
1120
1121         if (get_mode_n_vector_elems(a->mode) > 1) {
1122                 /* vector arithmetic not implemented yet */
1123                 return tarval_bad;
1124         }
1125
1126         switch (get_mode_sort(a->mode)) {
1127         case irms_int_number:
1128                 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1129                         buffer = alloca(sc_get_buffer_length());
1130                         sc_neg(a->value, buffer);
1131                         return get_tarval_overflow(buffer, a->length, a->mode);
1132                 }
1133                 return a;
1134
1135         case irms_float_number:
1136                 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1137                         fc_neg(a->value, NULL);
1138                         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1139                 }
1140                 return a;
1141
1142         default:
1143                 return tarval_bad;
1144         }
1145         return tarval_bad;
1146 }
1147
1148 /*
1149  * bitwise and
1150  */
1151 tarval *tarval_and(tarval *a, tarval *b) {
1152         assert(a);
1153         assert(b);
1154         assert(a->mode == b->mode);
1155
1156         /* works even for vector modes */
1157
1158         switch(get_mode_sort(a->mode)) {
1159         case irms_internal_boolean:
1160                 return (a == tarval_b_false) ? a : b;
1161
1162         case irms_int_number:
1163                 sc_and(a->value, b->value, NULL);
1164                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1165
1166         default:
1167                 assert(0 && "operation not defined on mode");
1168                 return tarval_bad;
1169         }
1170 }
1171
1172 /*
1173  * bitwise or
1174  */
1175 tarval *tarval_or (tarval *a, tarval *b) {
1176         assert(a);
1177         assert(b);
1178         assert(a->mode == b->mode);
1179
1180         /* works even for vector modes */
1181
1182         switch (get_mode_sort(a->mode)) {
1183         case irms_internal_boolean:
1184                 return (a == tarval_b_true) ? a : b;
1185
1186         case irms_int_number:
1187                 sc_or(a->value, b->value, NULL);
1188                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1189
1190         default:
1191                 assert(0 && "operation not defined on mode");
1192                 return tarval_bad;
1193         }
1194 }
1195
1196 /*
1197  * bitwise exclusive or (xor)
1198  */
1199 tarval *tarval_eor(tarval *a, tarval *b) {
1200         assert(a);
1201         assert(b);
1202         assert((a->mode == b->mode));
1203
1204         /* works even for vector modes */
1205
1206         switch (get_mode_sort(a->mode)) {
1207         case irms_internal_boolean:
1208                 return (a == b)? tarval_b_false : tarval_b_true;
1209
1210         case irms_int_number:
1211                 sc_xor(a->value, b->value, NULL);
1212                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1213
1214         default:
1215                 assert(0 && "operation not defined on mode");
1216                 return tarval_bad;;
1217         }
1218 }
1219
1220 /*
1221  * bitwise left shift
1222  */
1223 tarval *tarval_shl(tarval *a, tarval *b) {
1224         char *temp_val = NULL;
1225
1226         assert(a);
1227         assert(b);
1228         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1229
1230         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1231                 /* vector arithmetic not implemented yet */
1232                 return tarval_bad;
1233         }
1234
1235         if (get_mode_modulo_shift(a->mode) != 0) {
1236                 temp_val = alloca(sc_get_buffer_length());
1237
1238                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1239                 sc_mod(b->value, temp_val, temp_val);
1240         } else
1241                 temp_val = (char*)b->value;
1242
1243         sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1244         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1245 }
1246
1247 /*
1248  * bitwise unsigned right shift
1249  */
1250 tarval *tarval_shr(tarval *a, tarval *b) {
1251         char *temp_val = NULL;
1252
1253         assert(a);
1254         assert(b);
1255         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1256
1257         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1258                 /* vector arithmetic not implemented yet */
1259                 return tarval_bad;
1260         }
1261
1262         if (get_mode_modulo_shift(a->mode) != 0) {
1263                 temp_val = alloca(sc_get_buffer_length());
1264
1265                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1266                 sc_mod(b->value, temp_val, temp_val);
1267         } else
1268                 temp_val = (char*)b->value;
1269
1270         sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1271         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1272 }
1273
1274 /*
1275  * bitwise signed right shift
1276  */
1277 tarval *tarval_shrs(tarval *a, tarval *b) {
1278         char *temp_val = NULL;
1279
1280         assert(a);
1281         assert(b);
1282         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1283
1284         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1285                 /* vector arithmetic not implemented yet */
1286                 return tarval_bad;
1287         }
1288
1289         if (get_mode_modulo_shift(a->mode) != 0) {
1290                 temp_val = alloca(sc_get_buffer_length());
1291
1292                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1293                 sc_mod(b->value, temp_val, temp_val);
1294         } else
1295                 temp_val = (char*)b->value;
1296
1297         sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1298         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1299 }
1300
1301 /*
1302  * bitwise rotation
1303  */
1304 tarval *tarval_rot(tarval *a, tarval *b) {
1305         char *temp_val = NULL;
1306
1307         assert(a);
1308         assert(b);
1309         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1310
1311         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1312                 /* vector arithmetic not implemented yet */
1313                 return tarval_bad;
1314         }
1315
1316         if (get_mode_modulo_shift(a->mode) != 0) {
1317                 temp_val = alloca(sc_get_buffer_length());
1318
1319                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1320                 sc_mod(b->value, temp_val, temp_val);
1321         } else
1322                 temp_val = (char*)b->value;
1323
1324         sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1325         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1326 }
1327
1328 /*
1329  * carry flag of the last operation
1330  */
1331 int tarval_carry(void) {
1332         return sc_had_carry();
1333 }
1334
1335 /*
1336  * Output of tarvals
1337  */
1338 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1339         static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1340
1341         const char *str;
1342         char tv_buf[100];
1343         const tarval_mode_info *mode_info;
1344         const char *prefix, *suffix;
1345
1346         mode_info = tv->mode->tv_priv;
1347         if (! mode_info)
1348                 mode_info = &default_info;
1349         prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1350         suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1351
1352         switch (get_mode_sort(tv->mode)) {
1353         case irms_reference:
1354                 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1355                 /* fall through */
1356         case irms_int_number:
1357         case irms_character:
1358                 switch (mode_info->mode_output) {
1359
1360                 case TVO_DECIMAL:
1361                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1362                         break;
1363
1364                 case TVO_OCTAL:
1365                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1366                         break;
1367
1368                 case TVO_HEX:
1369                 case TVO_NATIVE:
1370                 default:
1371                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1372                         break;
1373                 }
1374                 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1375
1376         case irms_float_number:
1377                 switch (mode_info->mode_output) {
1378                 case TVO_HEX:
1379                         return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1380
1381                 case TVO_HEXFLOAT:
1382                         return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1383
1384                 case TVO_FLOAT:
1385                 case TVO_NATIVE:
1386                 default:
1387                         return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1388                 }
1389                 break;
1390
1391         case irms_internal_boolean:
1392                 switch (mode_info->mode_output) {
1393
1394                 case TVO_DECIMAL:
1395                 case TVO_OCTAL:
1396                 case TVO_HEX:
1397                 case TVO_BINARY:
1398                         return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1399
1400                 case TVO_NATIVE:
1401                 default:
1402                         return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1403                 }
1404
1405         case irms_control_flow:
1406         case irms_memory:
1407         case irms_auxiliary:
1408                 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1409         }
1410
1411         return 0;
1412 }
1413
1414 /**
1415  * Output of tarvals to stdio.
1416  */
1417 int tarval_printf(tarval *tv) {
1418         char buf[1024];
1419         int res;
1420
1421         res = tarval_snprintf(buf, sizeof(buf), tv);
1422         assert(res < sizeof(buf) && "buffer to small for tarval_snprintf");
1423         printf(buf);
1424         return res;
1425 }
1426
1427 char *get_tarval_bitpattern(tarval *tv) {
1428         int i, j, pos = 0;
1429         int n = get_mode_size_bits(tv->mode);
1430         int bytes = (n + 7) / 8;
1431         char *res = xmalloc((n + 1) * sizeof(char));
1432         unsigned char byte;
1433
1434         for(i = 0; i < bytes; i++) {
1435                 byte = get_tarval_sub_bits(tv, i);
1436                 for(j = 1; j < 256; j <<= 1)
1437                         if(pos < n)
1438                                 res[pos++] = j & byte ? '1' : '0';
1439         }
1440
1441         res[n] = '\0';
1442
1443         return res;
1444 }
1445
1446 /*
1447  * access to the bitpattern
1448  */
1449 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1450         switch (get_mode_sort(tv->mode)) {
1451         case irms_int_number:
1452         case irms_character:
1453                 return sc_sub_bits(tv->value, tv->length, byte_ofs);
1454
1455         case irms_float_number:
1456                 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1457
1458         default:
1459                 return 0;
1460         }
1461 }
1462
1463 /*
1464  * Specify the output options of one mode.
1465  *
1466  * This functions stores the modinfo, so DO NOT DESTROY it.
1467  *
1468  * Returns zero on success.
1469  */
1470 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1471         assert(mode);
1472
1473         mode->tv_priv = modeinfo;
1474         return 0;
1475 }
1476
1477 /*
1478  * Returns the output options of one mode.
1479  *
1480  * This functions returns the mode info of a given mode.
1481  */
1482 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1483         assert(mode);
1484
1485         return mode->tv_priv;
1486 }
1487
1488 /*
1489  * Identifying tarvals values for algebraic simplifications.
1490  *
1491  * Returns:
1492  *   - TV_CLASSIFY_NULL    for additive neutral or the NULL tarval for reference modes,
1493  *   - TV_CLASSIFY_ONE     for multiplicative neutral,
1494  *   - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1495  *   - TV_CLASSIFY_OTHER   else
1496  */
1497 tarval_classification_t classify_tarval(tarval *tv) {
1498         if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1499
1500         if (tv == get_mode_null(tv->mode))
1501                 return TV_CLASSIFY_NULL;
1502         else if (tv == get_mode_one(tv->mode))
1503                 return TV_CLASSIFY_ONE;
1504         else if ((get_mode_sort(tv->mode) == irms_int_number)
1505                 && (tv == new_tarval_from_long(-1, tv->mode)))
1506                 return TV_CLASSIFY_ALL_ONE;
1507
1508         return TV_CLASSIFY_OTHER;
1509 }
1510
1511 /*
1512  * Returns non-zero if a given (integer) tarval has only one single bit
1513  * set.
1514  */
1515 int is_single_bit_tarval(tarval *tv) {
1516         int i, l;
1517         int bits;
1518
1519         if (!tv || tv == tarval_bad) return 0;
1520         if (! mode_is_int(tv->mode)) return 0;
1521
1522         l = get_mode_size_bytes(tv->mode);
1523         for (bits = 0, i = l - 1; i >= 0; --i) {
1524                 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1525
1526                 /* check for more than one bit in these */
1527                 if (v) {
1528                         if (v & (v-1))
1529                                 return 0;
1530                         if (++bits > 1)
1531                                 return 0;
1532                 }
1533         }
1534         return bits;
1535 }
1536
1537 /*
1538  * Sets the overflow mode for integer operations.
1539  */
1540 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1541         int_overflow_mode = ov_mode;
1542 }
1543
1544 /**
1545  * Get the overflow mode for integer operations.
1546  */
1547 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1548         return int_overflow_mode;
1549 }
1550
1551 /**
1552  * default mode_info for output as HEX
1553  */
1554 static const tarval_mode_info hex_output = {
1555         TVO_HEX,
1556         "0x",
1557         NULL,
1558 };
1559
1560 /*
1561  * Initialization of the tarval module: called before init_mode()
1562  */
1563 void init_tarval_1(long null_value) {
1564         _null_value = null_value;
1565
1566         /* initialize the sets holding the tarvals with a comparison function and
1567          * an initial size, which is the expected number of constants */
1568         tarvals = new_set(memcmp, N_CONSTANTS);
1569         values  = new_set(memcmp, N_CONSTANTS);
1570         /* init strcalc with precision of 68 to support floating point values with 64
1571          * bit mantissa (needs extra bits for rounding and overflow) */
1572         init_strcalc(68);
1573         init_fltcalc(0);
1574 }
1575
1576 /*
1577  * Initialization of the tarval module: called after init_mode()
1578  */
1579 void init_tarval_2(void) {
1580         tarval_bad->kind        = k_tarval;
1581         tarval_bad->mode        = mode_BAD;
1582         tarval_bad->value       = INT_TO_PTR(resid_tarval_bad);
1583
1584         tarval_undefined->kind  = k_tarval;
1585         tarval_undefined->mode  = mode_ANY;
1586         tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1587
1588         tarval_b_true->kind     = k_tarval;
1589         tarval_b_true->mode     = mode_b;
1590         tarval_b_true->value    = INT_TO_PTR(resid_tarval_b_true);
1591
1592         tarval_b_false->kind    = k_tarval;
1593         tarval_b_false->mode    = mode_b;
1594         tarval_b_false->value   = INT_TO_PTR(resid_tarval_b_false);
1595
1596         /*
1597          * assign output modes that are compatible with the
1598          * old implementation: Hex output
1599          */
1600         set_tarval_mode_output_option(mode_U,  &hex_output);
1601         set_tarval_mode_output_option(mode_C,  &hex_output);
1602         set_tarval_mode_output_option(mode_Bs, &hex_output);
1603         set_tarval_mode_output_option(mode_Bu, &hex_output);
1604         set_tarval_mode_output_option(mode_Hs, &hex_output);
1605         set_tarval_mode_output_option(mode_Hu, &hex_output);
1606         set_tarval_mode_output_option(mode_Is, &hex_output);
1607         set_tarval_mode_output_option(mode_Iu, &hex_output);
1608         set_tarval_mode_output_option(mode_Ls, &hex_output);
1609         set_tarval_mode_output_option(mode_Lu, &hex_output);
1610         set_tarval_mode_output_option(mode_P,  &hex_output);
1611         }
1612
1613 /* free all memory occupied by tarval. */
1614 void finish_tarval(void) {
1615         finish_strcalc ();
1616         finish_fltcalc ();
1617         del_set(tarvals); tarvals = NULL;
1618         del_set(values);  values = NULL;
1619 }
1620
1621 /****************************************************************************
1622  *   end of tv.c
1623  ****************************************************************************/