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