fixed broken get_tarval_sub_bits() implementation: now correctly clip number of bits
[libfirm] / ir / tv / tv.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Representation of and static computations on target machine
23  *           values.
24  * @date     2003
25  * @author   Mathias Heil
26  * @version  $Id$
27  * @summary
28  *
29  * Values are stored in a format depending upon chosen arithmetic
30  * module. Default uses strcalc and fltcalc.
31  * This implementation assumes:
32  *  - target has IEEE-754 floating-point arithmetic.
33  */
34 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <assert.h>         /* assertions */
39 #include <stdlib.h>         /* atoi() */
40 #ifdef HAVE_STRING_H
41 # include <string.h>         /* nice things for strings */
42 #endif
43 #ifdef HAVE_STRINGS_H
44 #include <strings.h>        /* strings.h also includes bsd only function strcasecmp */
45 #endif
46 #ifdef HAVE_STDLIB_H
47 # include <stdlib.h>
48 #endif
49
50 #include "tv_t.h"
51 #include "set.h"            /* to store tarvals in */
52 #include "entity_t.h"       /* needed to store pointers to entities */
53 #include "irmode_t.h"
54 #include "irnode.h"         /* defines boolean return values (pnc_number)*/
55 #include "strcalc.h"
56 #include "fltcalc.h"
57 #include "irtools.h"
58 #include "xmalloc.h"
59 #include "firm_common.h"
60 #include "error.h"
61
62 /** Size of hash tables.  Should correspond to average number of distinct constant
63     target values */
64 #define N_CONSTANTS 2048
65
66 /* get the integer overflow mode */
67 #define GET_OVERFLOW_MODE() int_overflow_mode
68
69 /* unused, float to int doesn't work yet */
70 enum float_to_int_mode {
71         TRUNCATE,
72         ROUND
73 };
74
75 #define GET_FLOAT_TO_INT_MODE() TRUNCATE
76
77 #define SWITCH_NOINFINITY 0
78 #define SWITCH_NODENORMALS 0
79
80 /****************************************************************************
81  *   local definitions and macros
82  ****************************************************************************/
83 #ifndef NDEBUG
84 #  define TARVAL_VERIFY(a) tarval_verify((a))
85 #else
86 #  define TARVAL_VERIFY(a) ((void)0)
87 #endif
88
89 #define INSERT_TARVAL(tv) ((tarval*)set_insert(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
90 #define FIND_TARVAL(tv) ((tarval*)set_find(tarvals, (tv), sizeof(tarval), hash_tv((tv))))
91
92 #define INSERT_VALUE(val, size) (set_insert(values, (val), size, hash_val((val), size)))
93 #define FIND_VALUE(val, size) (set_find(values, (val), size, hash_val((val), size)))
94
95 #define fail_verify(a) _fail_verify((a), __FILE__, __LINE__)
96
97 /****************************************************************************
98  *   private variables
99  ****************************************************************************/
100 static struct set *tarvals = NULL;   /* container for tarval structs */
101 static struct set *values = NULL;    /* container for values */
102 static tarval_int_overflow_mode_t int_overflow_mode = TV_OVERFLOW_WRAP;
103
104 /** if this is set non-zero, the constant folding for floating point is OFF */
105 static int no_float = 0;
106
107 /****************************************************************************
108  *   private functions
109  ****************************************************************************/
110 #ifndef NDEBUG
111 static int hash_val(const void *value, unsigned int length);
112 static int hash_tv(tarval *tv);
113 static void _fail_verify(tarval *tv, const char* file, int line)
114 {
115         /* print a memory image of the tarval and throw an assertion */
116         if (tv)
117                 printf("%s:%d: Invalid tarval:\n  mode: %s\n value: [%p]\n", file, line, get_mode_name(tv->mode), tv->value);
118         else
119                 printf("%s:%d: Invalid tarval (null)", file, line);
120         assert(0);
121 }
122 #ifdef __GNUC__
123 INLINE static void tarval_verify(tarval *tv) __attribute__ ((unused));
124 #endif
125
126 INLINE static void tarval_verify(tarval *tv)
127 {
128         assert(tv);
129         assert(tv->mode);
130         assert(tv->value);
131
132         if ((tv == tarval_bad) || (tv == tarval_undefined)) return;
133         if ((tv == tarval_b_true) || (tv == tarval_b_false)) return;
134
135         if (!FIND_TARVAL(tv)) fail_verify(tv);
136         if (tv->length > 0 && !FIND_VALUE(tv->value, tv->length)) fail_verify(tv);
137
138         return;
139 }
140 #endif /* NDEBUG */
141
142 /** Hash a tarval. */
143 static int hash_tv(tarval *tv) {
144         return (PTR_TO_INT(tv->value) ^ PTR_TO_INT(tv->mode)) + tv->length;
145 }
146
147 /** Hash a value. Treat it as a byte array. */
148 static int hash_val(const void *value, unsigned int length) {
149         unsigned int i;
150         unsigned int hash = 0;
151
152         /* scramble the byte - array */
153         for (i = 0; i < length; ++i) {
154                 hash += (hash << 5) ^ (hash >> 27) ^ ((char*)value)[i];
155                 hash += (hash << 11) ^ (hash >> 17);
156         }
157
158         return hash;
159 }
160
161 /** finds tarval with value/mode or creates new tarval */
162 static tarval *get_tarval(const void *value, int length, ir_mode *mode) {
163         tarval tv;
164
165         tv.kind   = k_tarval;
166         tv.mode   = mode;
167         tv.length = length;
168         if (length > 0) {
169                 /* if there already is such a value, it is returned, else value
170                  * is copied into the set */
171                 tv.value = INSERT_VALUE(value, length);
172         } else {
173                 tv.value = value;
174         }
175         /* if there is such a tarval, it is returned, else tv is copied
176          * into the set */
177         return (tarval *)INSERT_TARVAL(&tv);
178 }
179
180 /**
181  * handle overflow
182  */
183 static tarval *get_tarval_overflow(const void *value, int length, ir_mode *mode)
184 {
185         switch (get_mode_sort(mode)) {
186         case irms_int_number:
187                 if (sc_comp(value, get_mode_max(mode)->value) == 1) {
188                         switch (GET_OVERFLOW_MODE()) {
189                         case TV_OVERFLOW_SATURATE:
190                                 return get_mode_max(mode);
191                         case TV_OVERFLOW_WRAP:
192                                 {
193                                         char *temp = alloca(sc_get_buffer_length());
194                                         sc_val_from_ulong(-1, temp);
195                                         sc_and(temp, value, temp);
196                                         /* the sc_ module expects that all bits are set ... */
197                                         sign_extend(temp, mode);
198                                         return get_tarval(temp, length, mode);
199                                 }
200                         case TV_OVERFLOW_BAD:
201                                 return tarval_bad;
202                         default:
203                                 return get_tarval(value, length, mode);
204                         }
205                 }
206                 if (sc_comp(value, get_mode_min(mode)->value) == -1) {
207                         switch (GET_OVERFLOW_MODE()) {
208                         case TV_OVERFLOW_SATURATE:
209                                 return get_mode_min(mode);
210                         case TV_OVERFLOW_WRAP: {
211                                 char *temp = alloca(sc_get_buffer_length());
212                                 sc_val_from_ulong(-1, temp);
213                                 sc_and(temp, value, temp);
214                                 return get_tarval(temp, length, mode);
215                         }
216                         case TV_OVERFLOW_BAD:
217                                 return tarval_bad;
218                         default:
219                                 return get_tarval(value, length, mode);
220                         }
221                 }
222                 break;
223
224         case irms_float_number:
225                 if (SWITCH_NOINFINITY && fc_is_inf(value)) {
226                         return fc_is_negative(value)?get_mode_min(mode):get_mode_max(mode);
227                 }
228
229                 if (SWITCH_NODENORMALS && fc_is_subnormal(value)) {
230                         return get_mode_null(mode);
231                 }
232                 break;
233
234         default:
235                 break;
236         }
237         return get_tarval(value, length, mode);
238 }
239
240 /*
241  *   public variables declared in tv.h
242  */
243 static tarval reserved_tv[4];
244
245 tarval *tarval_bad       = &reserved_tv[0];
246 tarval *tarval_undefined = &reserved_tv[1];
247 tarval *tarval_b_false   = &reserved_tv[2];
248 tarval *tarval_b_true    = &reserved_tv[3];
249
250 /*
251  *   public functions declared in tv.h
252  */
253
254 /*
255  * Constructors =============================================================
256  */
257 tarval *new_tarval_from_str(const char *str, size_t len, ir_mode *mode)
258 {
259         assert(str);
260         assert(len);
261         assert(mode);
262
263         switch (get_mode_sort(mode)) {
264         case irms_control_flow:
265         case irms_memory:
266         case irms_auxiliary:
267                 assert(0);
268                 break;
269
270         case irms_internal_boolean:
271                 /* match [tT][rR][uU][eE]|[fF][aA][lL][sS][eE] */
272                 if (strcasecmp(str, "true")) return tarval_b_true;
273                 else if (strcasecmp(str, "false")) return tarval_b_true;
274                 else
275                         /* XXX This is C semantics */
276                         return atoi(str) ? tarval_b_true : tarval_b_false;
277
278         case irms_float_number:
279                 switch (get_mode_size_bits(mode)) {
280                 case 32:
281                         fc_val_from_str(str, len, 8, 23, NULL);
282                         break;
283                 case 64:
284                         fc_val_from_str(str, len, 11, 52, NULL);
285                         break;
286                 case 80:
287                         fc_val_from_str(str, len, 15, 64, NULL);
288                         break;
289                 }
290                 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
291
292         case irms_reference:
293                 /* same as integer modes */
294         case irms_int_number:
295                 sc_val_from_str(str, len, NULL, mode);
296                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
297         }
298
299         assert(0);  /* can't be reached, can it? */
300         return NULL;
301 }
302
303 /*
304  * helper function, create a tarval from long
305  */
306 tarval *new_tarval_from_long(long l, ir_mode *mode) {
307         assert(mode);
308
309         switch(get_mode_sort(mode))   {
310         case irms_internal_boolean:
311                 /* XXX C semantics ! */
312                 return l ? tarval_b_true : tarval_b_false ;
313
314         case irms_reference:
315                 /* same as integer modes */
316         case irms_int_number:
317                 sc_val_from_long(l, NULL);
318                 sign_extend(sc_get_buffer(), mode);
319                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
320
321         case irms_float_number:
322                 return new_tarval_from_double((long double)l, mode);
323
324         default:
325                 assert(0 && "unsupported mode sort");
326         }
327         return NULL;
328 }
329
330 /* returns non-zero if can be converted to long */
331 int tarval_is_long(tarval *tv) {
332         if (!mode_is_int(tv->mode) && !mode_is_reference(tv->mode))
333                 return 0;
334
335         if (get_mode_size_bits(tv->mode) > (int) (sizeof(long) << 3)) {
336                 /* the value might be too big to fit in a long */
337                 sc_max_from_bits(sizeof(long) << 3, 0, NULL);
338                 if (sc_comp(sc_get_buffer(), tv->value) == -1) {
339                         /* really doesn't fit */
340                         return 0;
341                 }
342         }
343         return 1;
344 }
345
346 /* this might overflow the machine's long, so use only with small values */
347 long get_tarval_long(tarval* tv) {
348         assert(tarval_is_long(tv) && "tarval too big to fit in long");
349
350         return sc_val_to_long(tv->value);
351 }
352
353 tarval *new_tarval_from_double(long double d, ir_mode *mode) {
354         assert(mode && (get_mode_sort(mode) == irms_float_number));
355
356         switch (get_mode_size_bits(mode)) {
357         case 32:
358                 fc_val_from_ieee754(d, 8, 23, NULL);
359                 break;
360         case 64:
361                 fc_val_from_ieee754(d, 11, 52, NULL);
362                 break;
363         case 80:
364                 fc_val_from_ieee754(d, 15, 64, NULL);
365                 break;
366         }
367         return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
368 }
369
370 /* returns non-zero if can be converted to double */
371 int tarval_is_double(tarval *tv) {
372         assert(tv);
373
374         return (get_mode_sort(tv->mode) == irms_float_number);
375 }
376
377 long double get_tarval_double(tarval *tv) {
378         assert(tarval_is_double(tv));
379
380         return fc_val_to_ieee754(tv->value);
381 }
382
383
384 /*
385  * Access routines for tarval fields ========================================
386  */
387
388 /* get the mode of the tarval */
389 ir_mode *(get_tarval_mode)(const tarval *tv) {
390         return _get_tarval_mode(tv);
391 }
392
393 /*
394  * Special value query functions ============================================
395  *
396  * These functions calculate and return a tarval representing the requested
397  * value.
398  * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
399  * functions, but these are stored on initialization of the irmode module and
400  * therefore the irmode functions should be preferred to the functions below.
401  */
402
403 tarval *(get_tarval_bad)(void) {
404         return _get_tarval_bad();
405 }
406
407 tarval *(get_tarval_undefined)(void) {
408         return _get_tarval_undefined();
409 }
410
411 tarval *(get_tarval_b_false)(void) {
412         return _get_tarval_b_false();
413 }
414
415 tarval *(get_tarval_b_true)(void) {
416         return _get_tarval_b_true();
417 }
418
419 tarval *get_tarval_max(ir_mode *mode) {
420         assert(mode);
421
422         if (get_mode_n_vector_elems(mode) > 1) {
423                 /* vector arithmetic not implemented yet */
424                 return tarval_bad;
425         }
426
427         switch(get_mode_sort(mode)) {
428         case irms_reference:
429         case irms_control_flow:
430         case irms_memory:
431         case irms_auxiliary:
432                 assert(0);
433                 break;
434
435         case irms_internal_boolean:
436                 return tarval_b_true;
437
438         case irms_float_number:
439                 switch(get_mode_size_bits(mode)) {
440                 case 32:
441                         fc_get_max(8, 23, NULL);
442                         break;
443                 case 64:
444                         fc_get_max(11, 52, NULL);
445                         break;
446                 case 80:
447                         fc_get_max(15, 64, NULL);
448                         break;
449                 }
450                 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
451
452                 case irms_int_number:
453                         sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
454                         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
455         }
456         return tarval_bad;
457 }
458
459 tarval *get_tarval_min(ir_mode *mode) {
460         assert(mode);
461
462         if (get_mode_n_vector_elems(mode) > 1) {
463                 /* vector arithmetic not implemented yet */
464                 return tarval_bad;
465         }
466
467         switch(get_mode_sort(mode)) {
468         case irms_reference:
469         case irms_control_flow:
470         case irms_memory:
471         case irms_auxiliary:
472                 assert(0);
473                 break;
474
475         case irms_internal_boolean:
476                 return tarval_b_false;
477
478         case irms_float_number:
479                 switch(get_mode_size_bits(mode)) {
480                 case 32:
481                         fc_get_min(8, 23, NULL);
482                         break;
483                 case 64:
484                         fc_get_min(11, 52, NULL);
485                         break;
486                 case 80:
487                         fc_get_min(15, 64, NULL);
488                         break;
489                 }
490                 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
491
492                 case irms_int_number:
493                         sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
494                         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
495         }
496         return tarval_bad;
497 }
498
499 /** The bit pattern for the pointer NULL */
500 static long _null_value;
501
502 tarval *get_tarval_null(ir_mode *mode) {
503         assert(mode);
504
505         if (get_mode_n_vector_elems(mode) > 1) {
506                 /* vector arithmetic not implemented yet */
507                 return tarval_bad;
508         }
509
510         switch(get_mode_sort(mode)) {
511         case irms_control_flow:
512         case irms_memory:
513         case irms_auxiliary:
514         case irms_internal_boolean:
515                 assert(0);
516                 break;
517
518         case irms_float_number:
519                 return new_tarval_from_double(0.0, mode);
520
521         case irms_int_number:
522                 return new_tarval_from_long(0l,  mode);
523
524         case irms_reference:
525                 return new_tarval_from_long(_null_value, mode);
526         }
527         return tarval_bad;
528 }
529
530 tarval *get_tarval_one(ir_mode *mode) {
531         assert(mode);
532
533         if (get_mode_n_vector_elems(mode) > 1) {
534                 /* vector arithmetic not implemented yet */
535                 return tarval_bad;
536         }
537
538         switch(get_mode_sort(mode)) {
539         case irms_control_flow:
540         case irms_memory:
541         case irms_auxiliary:
542         case irms_internal_boolean:
543         case irms_reference:
544                 assert(0);
545                 break;
546
547         case irms_float_number:
548                 return new_tarval_from_double(1.0, mode);
549
550         case irms_int_number:
551                 return new_tarval_from_long(1l, mode);
552                 break;
553         }
554         return tarval_bad;
555 }
556
557 tarval *get_tarval_minus_one(ir_mode *mode) {
558         assert(mode);
559
560         if (get_mode_n_vector_elems(mode) > 1) {
561                 /* vector arithmetic not implemented yet */
562                 return tarval_bad;
563         }
564
565         switch(get_mode_sort(mode)) {
566         case irms_control_flow:
567         case irms_memory:
568         case irms_auxiliary:
569         case irms_internal_boolean:
570         case irms_reference:
571                 assert(0);
572                 break;
573
574         case irms_float_number:
575                 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
576
577         case irms_int_number:
578                 return new_tarval_from_long(-1l, mode);
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                 if (no_float)
748                         return pn_Cmp_False;
749                 /*
750                  * BEWARE: we cannot compare a == b here, because
751                  * a NaN is always Unordered to any other value, even to itself!
752                  */
753                 switch (fc_comp(a->value, b->value)) {
754                 case -1: return pn_Cmp_Lt;
755                 case  0: return pn_Cmp_Eq;
756                 case  1: return pn_Cmp_Gt;
757                 case  2: return pn_Cmp_Uo;
758                 default: return pn_Cmp_False;
759                 }
760         case irms_int_number:
761                 if (a == b)
762                         return pn_Cmp_Eq;
763                 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
764
765         case irms_internal_boolean:
766                 if (a == b)
767                         return pn_Cmp_Eq;
768                 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
769         }
770         return pn_Cmp_False;
771 }
772
773 /*
774  * convert to other mode
775  */
776 tarval *tarval_convert_to(tarval *src, ir_mode *m) {
777         char *buffer;
778
779         assert(src);
780         assert(m);
781
782         if (src->mode == m) return src;
783
784         if (get_mode_n_vector_elems(src->mode) > 1) {
785                 /* vector arithmetic not implemented yet */
786                 return tarval_bad;
787         }
788
789         switch (get_mode_sort(src->mode)) {
790         case irms_control_flow:
791         case irms_memory:
792         case irms_auxiliary:
793                 break;
794
795                 /* cast float to something */
796         case irms_float_number:
797                 switch (get_mode_sort(m)) {
798                 case irms_float_number:
799                         switch (get_mode_size_bits(m)) {
800                         case 32:
801                                 fc_cast(src->value, 8, 23, NULL);
802                                 break;
803                         case 64:
804                                 fc_cast(src->value, 11, 52, NULL);
805                                 break;
806                         case 80:
807                                 fc_cast(src->value, 15, 64, NULL);
808                                 break;
809                         default:
810                                 break;
811                         }
812                         return get_tarval(fc_get_buffer(), fc_get_buffer_length(), m);
813
814                 case irms_int_number:
815                         switch (GET_FLOAT_TO_INT_MODE()) {
816                         case TRUNCATE:
817                                 fc_int(src->value, NULL);
818                                 break;
819                         case ROUND:
820                                 fc_rnd(src->value, NULL);
821                                 break;
822                         default:
823                                 assert(0);
824                                 break;
825                         }
826                         /* FIXME: floating point unit can't produce a value in integer
827                          * representation
828                          * an intermediate representation is needed here first. */
829                         /*  return get_tarval(); */
830                         return tarval_bad;
831
832                 default:
833                         /* the rest can't be converted */
834                         return tarval_bad;
835                 }
836                 break;
837
838         /* cast int/characters to something */
839         case irms_int_number:
840                 switch (get_mode_sort(m)) {
841                 case irms_int_number:
842                         buffer = alloca(sc_get_buffer_length());
843                         memcpy(buffer, src->value, sc_get_buffer_length());
844                         sign_extend(buffer, m);
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_reference:
898                 switch(get_mode_sort(m)) {
899                 case irms_int_number:
900                         buffer = alloca(sc_get_buffer_length());
901                         memcpy(buffer, src->value, sc_get_buffer_length());
902                         sign_extend(buffer, src->mode);
903                         return get_tarval_overflow(buffer, src->length, m);
904                 default:
905                         break;
906                 }
907
908                 break;
909         }
910
911         return tarval_bad;
912 }
913
914 /*
915  * bitwise negation
916  */
917 tarval *tarval_not(tarval *a) {
918         char *buffer;
919
920         assert(a);
921
922         /* works for vector mode without changes */
923
924         switch (get_mode_sort(a->mode)) {
925         case irms_int_number:
926                 buffer = alloca(sc_get_buffer_length());
927                 sc_not(a->value, buffer);
928                 return get_tarval(buffer, a->length, a->mode);
929
930         case irms_internal_boolean:
931                 if (a == tarval_b_true)
932                         return tarval_b_false;
933                 if (a == tarval_b_false)
934                         return tarval_b_true;
935                 return tarval_bad;
936
937         default:
938                 assert(0 && "bitwise negation is only allowed for integer and boolean");
939                 return tarval_bad;
940         }
941 }
942
943 /*
944  * arithmetic negation
945  */
946 tarval *tarval_neg(tarval *a) {
947         char *buffer;
948
949         assert(a);
950         assert(mode_is_num(a->mode)); /* negation only for numerical values */
951
952         /* note: negation is allowed even for unsigned modes. */
953
954         if (get_mode_n_vector_elems(a->mode) > 1) {
955                 /* vector arithmetic not implemented yet */
956                 return tarval_bad;
957         }
958
959         switch (get_mode_sort(a->mode)) {
960         case irms_int_number:
961                 buffer = alloca(sc_get_buffer_length());
962                 sc_neg(a->value, buffer);
963                 return get_tarval_overflow(buffer, a->length, a->mode);
964
965         case irms_float_number:
966                 if(no_float)
967                         return tarval_bad;
968
969                 fc_neg(a->value, NULL);
970                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
971
972         default:
973                 return tarval_bad;
974         }
975 }
976
977 /*
978  * addition
979  */
980 tarval *tarval_add(tarval *a, tarval *b) {
981         char *buffer;
982
983         assert(a);
984         assert(b);
985         assert(a->mode == b->mode);
986
987         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
988                 /* vector arithmetic not implemented yet */
989                 return tarval_bad;
990         }
991
992         switch (get_mode_sort(a->mode)) {
993         case irms_int_number:
994                 /* modes of a,b are equal, so result has mode of a as this might be the character */
995                 buffer = alloca(sc_get_buffer_length());
996                 sc_add(a->value, b->value, buffer);
997                 return get_tarval_overflow(buffer, a->length, a->mode);
998
999         case irms_float_number:
1000                 if(no_float)
1001                         return tarval_bad;
1002
1003                 fc_add(a->value, b->value, NULL);
1004                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1005
1006         default:
1007                 return tarval_bad;
1008         }
1009 }
1010
1011 /*
1012  * subtraction
1013  */
1014 tarval *tarval_sub(tarval *a, tarval *b) {
1015         char *buffer;
1016
1017         assert(a);
1018         assert(b);
1019         assert(a->mode == b->mode);
1020
1021         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1022                 /* vector arithmetic not implemented yet */
1023                 return tarval_bad;
1024         }
1025         switch (get_mode_sort(a->mode)) {
1026         case irms_int_number:
1027                 /* modes of a,b are equal, so result has mode of a as this might be the character */
1028                 buffer = alloca(sc_get_buffer_length());
1029                 sc_sub(a->value, b->value, buffer);
1030                 return get_tarval_overflow(buffer, a->length, a->mode);
1031
1032         case irms_float_number:
1033                 if(no_float)
1034                         return tarval_bad;
1035
1036                 fc_sub(a->value, b->value, NULL);
1037                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1038
1039         default:
1040                 return tarval_bad;
1041         }
1042 }
1043
1044 /*
1045  * multiplication
1046  */
1047 tarval *tarval_mul(tarval *a, tarval *b) {
1048         char *buffer;
1049
1050         assert(a);
1051         assert(b);
1052         assert(a->mode == b->mode);
1053
1054         if (get_mode_n_vector_elems(a->mode) > 1) {
1055                 /* vector arithmetic not implemented yet */
1056                 return tarval_bad;
1057         }
1058
1059         switch (get_mode_sort(a->mode)) {
1060         case irms_int_number:
1061                 /* modes of a,b are equal */
1062                 buffer = alloca(sc_get_buffer_length());
1063                 sc_mul(a->value, b->value, buffer);
1064                 return get_tarval_overflow(buffer, a->length, a->mode);
1065
1066         case irms_float_number:
1067                 if(no_float)
1068                         return tarval_bad;
1069
1070                 fc_mul(a->value, b->value, NULL);
1071                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1072
1073         default:
1074                 return tarval_bad;
1075         }
1076 }
1077
1078 /*
1079  * floating point division
1080  */
1081 tarval *tarval_quo(tarval *a, tarval *b) {
1082         assert(a);
1083         assert(b);
1084         assert((a->mode == b->mode) && mode_is_float(a->mode));
1085
1086         if(no_float)
1087                 return tarval_bad;
1088
1089         if (get_mode_n_vector_elems(a->mode) > 1) {
1090                 /* vector arithmetic not implemented yet */
1091                 return tarval_bad;
1092         }
1093
1094         fc_div(a->value, b->value, NULL);
1095         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1096 }
1097
1098 /*
1099  * integer division
1100  * overflow is impossible, but look out for division by zero
1101  */
1102 tarval *tarval_div(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_div(a->value, b->value, NULL);
1116         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1117 }
1118
1119 /*
1120  * remainder
1121  * overflow is impossible, but look out for division by zero
1122  */
1123 tarval *tarval_mod(tarval *a, tarval *b) {
1124         assert(a);
1125         assert(b);
1126         assert((a->mode == b->mode) && mode_is_int(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         /* x/0 error */
1134         if (b == get_mode_null(b->mode)) return tarval_bad;
1135         /* modes of a,b are equal */
1136         sc_mod(a->value, b->value, NULL);
1137         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1138 }
1139
1140 /*
1141  * absolute value
1142  */
1143 tarval *tarval_abs(tarval *a) {
1144         char *buffer;
1145
1146         assert(a);
1147         assert(mode_is_num(a->mode));
1148
1149         if (get_mode_n_vector_elems(a->mode) > 1) {
1150                 /* vector arithmetic not implemented yet */
1151                 return tarval_bad;
1152         }
1153
1154         switch (get_mode_sort(a->mode)) {
1155         case irms_int_number:
1156                 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1157                         buffer = alloca(sc_get_buffer_length());
1158                         sc_neg(a->value, buffer);
1159                         return get_tarval_overflow(buffer, a->length, a->mode);
1160                 }
1161                 return a;
1162
1163         case irms_float_number:
1164                 if(no_float)
1165                         return tarval_bad;
1166
1167                 if (fc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1168                         fc_neg(a->value, NULL);
1169                         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1170                 }
1171                 return a;
1172
1173         default:
1174                 return tarval_bad;
1175         }
1176         return tarval_bad;
1177 }
1178
1179 /*
1180  * bitwise and
1181  */
1182 tarval *tarval_and(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_false) ? a : b;
1192
1193         case irms_int_number:
1194                 sc_and(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 or
1205  */
1206 tarval *tarval_or (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 == tarval_b_true) ? a : b;
1216
1217         case irms_int_number:
1218                 sc_or(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 exclusive or (xor)
1229  */
1230 tarval *tarval_eor(tarval *a, tarval *b) {
1231         assert(a);
1232         assert(b);
1233         assert((a->mode == b->mode));
1234
1235         /* works even for vector modes */
1236
1237         switch (get_mode_sort(a->mode)) {
1238         case irms_internal_boolean:
1239                 return (a == b)? tarval_b_false : tarval_b_true;
1240
1241         case irms_int_number:
1242                 sc_xor(a->value, b->value, NULL);
1243                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1244
1245         default:
1246                 assert(0 && "operation not defined on mode");
1247                 return tarval_bad;;
1248         }
1249 }
1250
1251 /*
1252  * bitwise left shift
1253  */
1254 tarval *tarval_shl(tarval *a, tarval *b) {
1255         char *temp_val = NULL;
1256
1257         assert(a);
1258         assert(b);
1259         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1260
1261         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1262                 /* vector arithmetic not implemented yet */
1263                 return tarval_bad;
1264         }
1265
1266         if (get_mode_modulo_shift(a->mode) != 0) {
1267                 temp_val = alloca(sc_get_buffer_length());
1268
1269                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1270                 sc_mod(b->value, temp_val, temp_val);
1271         } else
1272                 temp_val = (char*)b->value;
1273
1274         sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1275         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1276 }
1277
1278 /*
1279  * bitwise unsigned right shift
1280  */
1281 tarval *tarval_shr(tarval *a, tarval *b) {
1282         char *temp_val = NULL;
1283
1284         assert(a);
1285         assert(b);
1286         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1287
1288         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1289                 /* vector arithmetic not implemented yet */
1290                 return tarval_bad;
1291         }
1292
1293         if (get_mode_modulo_shift(a->mode) != 0) {
1294                 temp_val = alloca(sc_get_buffer_length());
1295
1296                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1297                 sc_mod(b->value, temp_val, temp_val);
1298         } else
1299                 temp_val = (char*)b->value;
1300
1301         sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1302         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1303 }
1304
1305 /*
1306  * bitwise signed right shift
1307  */
1308 tarval *tarval_shrs(tarval *a, tarval *b) {
1309         char *temp_val = NULL;
1310
1311         assert(a);
1312         assert(b);
1313         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1314
1315         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1316                 /* vector arithmetic not implemented yet */
1317                 return tarval_bad;
1318         }
1319
1320         if (get_mode_modulo_shift(a->mode) != 0) {
1321                 temp_val = alloca(sc_get_buffer_length());
1322
1323                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1324                 sc_mod(b->value, temp_val, temp_val);
1325         } else
1326                 temp_val = (char*)b->value;
1327
1328         sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1329         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1330 }
1331
1332 /*
1333  * bitwise rotation
1334  */
1335 tarval *tarval_rot(tarval *a, tarval *b) {
1336         char *temp_val = NULL;
1337
1338         assert(a);
1339         assert(b);
1340         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1341
1342         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1343                 /* vector arithmetic not implemented yet */
1344                 return tarval_bad;
1345         }
1346
1347         if (get_mode_modulo_shift(a->mode) != 0) {
1348                 temp_val = alloca(sc_get_buffer_length());
1349
1350                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1351                 sc_mod(b->value, temp_val, temp_val);
1352         } else
1353                 temp_val = (char*)b->value;
1354
1355         sc_rot(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1356         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1357 }
1358
1359 /*
1360  * carry flag of the last operation
1361  */
1362 int tarval_carry(void) {
1363         return sc_had_carry();
1364 }
1365
1366 /*
1367  * Output of tarvals
1368  */
1369 int tarval_snprintf(char *buf, size_t len, tarval *tv) {
1370         static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1371
1372         const char *str;
1373         char tv_buf[100];
1374         const tarval_mode_info *mode_info;
1375         const char *prefix, *suffix;
1376
1377         mode_info = tv->mode->tv_priv;
1378         if (! mode_info)
1379                 mode_info = &default_info;
1380         prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1381         suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1382
1383         switch (get_mode_sort(tv->mode)) {
1384         case irms_reference:
1385                 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1386                 /* fall through */
1387         case irms_int_number:
1388                 switch (mode_info->mode_output) {
1389
1390                 case TVO_DECIMAL:
1391                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1392                         break;
1393
1394                 case TVO_OCTAL:
1395                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1396                         break;
1397
1398                 case TVO_HEX:
1399                 case TVO_NATIVE:
1400                 default:
1401                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1402                         break;
1403                 }
1404                 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1405
1406         case irms_float_number:
1407                 switch (mode_info->mode_output) {
1408                 case TVO_HEX:
1409                         return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1410
1411                 case TVO_HEXFLOAT:
1412                         return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1413
1414                 case TVO_FLOAT:
1415                 case TVO_NATIVE:
1416                 default:
1417                         return snprintf(buf, len, "%s%s%s", prefix, fc_print(tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1418                 }
1419                 break;
1420
1421         case irms_internal_boolean:
1422                 switch (mode_info->mode_output) {
1423
1424                 case TVO_DECIMAL:
1425                 case TVO_OCTAL:
1426                 case TVO_HEX:
1427                 case TVO_BINARY:
1428                         return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1429
1430                 case TVO_NATIVE:
1431                 default:
1432                         return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1433                 }
1434
1435         case irms_control_flow:
1436         case irms_memory:
1437         case irms_auxiliary:
1438                 return snprintf(buf, len, "<TV_OVERFLOW_BAD>");
1439         }
1440
1441         return 0;
1442 }
1443
1444 /**
1445  * Output of tarvals to stdio.
1446  */
1447 int tarval_printf(tarval *tv) {
1448         char buf[1024];
1449         int res;
1450
1451         res = tarval_snprintf(buf, sizeof(buf), tv);
1452         assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1453         printf(buf);
1454         return res;
1455 }
1456
1457 char *get_tarval_bitpattern(tarval *tv) {
1458         int i, j, pos = 0;
1459         int n = get_mode_size_bits(tv->mode);
1460         int bytes = (n + 7) / 8;
1461         char *res = xmalloc((n + 1) * sizeof(char));
1462         unsigned char byte;
1463
1464         for(i = 0; i < bytes; i++) {
1465                 byte = get_tarval_sub_bits(tv, i);
1466                 for(j = 1; j < 256; j <<= 1)
1467                         if(pos < n)
1468                                 res[pos++] = j & byte ? '1' : '0';
1469         }
1470
1471         res[n] = '\0';
1472
1473         return res;
1474 }
1475
1476 /*
1477  * access to the bitpattern
1478  */
1479 unsigned char get_tarval_sub_bits(tarval *tv, unsigned byte_ofs) {
1480         switch (get_mode_arithmetic(tv->mode)) {
1481         case irma_twos_complement:
1482                 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1483         case irma_ieee754:
1484                 return fc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1485         default:
1486                 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1487         }
1488 }
1489
1490 /*
1491  * Specify the output options of one mode.
1492  *
1493  * This functions stores the modinfo, so DO NOT DESTROY it.
1494  *
1495  * Returns zero on success.
1496  */
1497 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo) {
1498         assert(mode);
1499
1500         mode->tv_priv = modeinfo;
1501         return 0;
1502 }
1503
1504 /*
1505  * Returns the output options of one mode.
1506  *
1507  * This functions returns the mode info of a given mode.
1508  */
1509 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode) {
1510         assert(mode);
1511
1512         return mode->tv_priv;
1513 }
1514
1515 /*
1516  * Identifying tarvals values for algebraic simplifications.
1517  *
1518  * Returns:
1519  *   - TV_CLASSIFY_NULL    for additive neutral or the NULL tarval for reference modes,
1520  *   - TV_CLASSIFY_ONE     for multiplicative neutral,
1521  *   - TV_CLASSIFY_ALL_ONE for bitwise-and neutral
1522  *   - TV_CLASSIFY_OTHER   else
1523  */
1524 tarval_classification_t classify_tarval(tarval *tv) {
1525         if (!tv || tv == tarval_bad) return TV_CLASSIFY_OTHER;
1526
1527         if (tv == get_mode_null(tv->mode))
1528                 return TV_CLASSIFY_NULL;
1529         else if (tv == get_mode_one(tv->mode))
1530                 return TV_CLASSIFY_ONE;
1531         else if ((get_mode_sort(tv->mode) == irms_int_number)
1532                 && (tv == get_mode_minus_one(tv->mode)))
1533                 return TV_CLASSIFY_ALL_ONE;
1534
1535         return TV_CLASSIFY_OTHER;
1536 }
1537
1538 /*
1539  * Returns non-zero if a given (integer) tarval has only one single bit
1540  * set.
1541  */
1542 int tarval_is_single_bit(tarval *tv) {
1543         int i, l;
1544         int bits;
1545
1546         if (!tv || tv == tarval_bad) return 0;
1547         if (! mode_is_int(tv->mode)) return 0;
1548
1549         l = get_mode_size_bytes(tv->mode);
1550         for (bits = 0, i = l - 1; i >= 0; --i) {
1551                 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1552
1553                 /* check for more than one bit in these */
1554                 if (v) {
1555                         if (v & (v-1))
1556                                 return 0;
1557                         if (++bits > 1)
1558                                 return 0;
1559                 }
1560         }
1561         return bits;
1562 }
1563
1564 /*
1565  * Returns non-zero if the mantissa of a floating point IEEE-754
1566  * tarval is zero (i.e. 1.0Exxx)
1567  */
1568 int tarval_ieee754_zero_mantissa(tarval *tv) {
1569         assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1570         return fc_zero_mantissa(tv->value);
1571 }
1572
1573 /* Returns the exponent of a floating point IEEE-754 tarval. */
1574 int tarval_ieee754_get_exponent(tarval *tv) {
1575         assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1576         return fc_get_exponent(tv->value);
1577 }
1578
1579 /* Set the immediate precision for IEEE-754 results. */
1580 unsigned tarval_ieee754_set_immediate_precision(unsigned bits) {
1581         return fc_set_immediate_precision(bits);
1582 }
1583
1584 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1585 unsigned tarval_ieee754_get_exact(void) {
1586         return fc_is_exact();
1587 }
1588
1589 /*
1590  * Sets the overflow mode for integer operations.
1591  */
1592 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode) {
1593         int_overflow_mode = ov_mode;
1594 }
1595
1596 /* Get the overflow mode for integer operations. */
1597 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void) {
1598         return int_overflow_mode;
1599 }
1600
1601 /* Enable/Disable floating point constant folding. */
1602 int tarval_enable_fp_ops(int enable) {
1603         int old = !no_float;
1604
1605         no_float = !enable;
1606         return old;
1607 }
1608
1609 /**
1610  * default mode_info for output as HEX
1611  */
1612 static const tarval_mode_info hex_output = {
1613         TVO_HEX,
1614         "0x",
1615         NULL,
1616 };
1617
1618 /*
1619  * Initialization of the tarval module: called before init_mode()
1620  */
1621 void init_tarval_1(long null_value) {
1622         _null_value = null_value;
1623
1624         /* initialize the sets holding the tarvals with a comparison function and
1625          * an initial size, which is the expected number of constants */
1626         tarvals = new_set(memcmp, N_CONSTANTS);
1627         values  = new_set(memcmp, N_CONSTANTS);
1628         /* init strcalc with precision of 68 to support floating point values with 64
1629          * bit mantissa (needs extra bits for rounding and overflow) */
1630         init_strcalc(68);
1631         init_fltcalc(0);
1632 }
1633
1634 /*
1635  * Initialization of the tarval module: called after init_mode()
1636  */
1637 void init_tarval_2(void) {
1638         tarval_bad->kind        = k_tarval;
1639         tarval_bad->mode        = mode_BAD;
1640         tarval_bad->value       = INT_TO_PTR(resid_tarval_bad);
1641
1642         tarval_undefined->kind  = k_tarval;
1643         tarval_undefined->mode  = mode_ANY;
1644         tarval_undefined->value = INT_TO_PTR(resid_tarval_undefined);
1645
1646         tarval_b_true->kind     = k_tarval;
1647         tarval_b_true->mode     = mode_b;
1648         tarval_b_true->value    = INT_TO_PTR(resid_tarval_b_true);
1649
1650         tarval_b_false->kind    = k_tarval;
1651         tarval_b_false->mode    = mode_b;
1652         tarval_b_false->value   = INT_TO_PTR(resid_tarval_b_false);
1653
1654         /*
1655          * assign output modes that are compatible with the
1656          * old implementation: Hex output
1657          */
1658         set_tarval_mode_output_option(mode_Bs, &hex_output);
1659         set_tarval_mode_output_option(mode_Bu, &hex_output);
1660         set_tarval_mode_output_option(mode_Hs, &hex_output);
1661         set_tarval_mode_output_option(mode_Hu, &hex_output);
1662         set_tarval_mode_output_option(mode_Is, &hex_output);
1663         set_tarval_mode_output_option(mode_Iu, &hex_output);
1664         set_tarval_mode_output_option(mode_Ls, &hex_output);
1665         set_tarval_mode_output_option(mode_Lu, &hex_output);
1666         set_tarval_mode_output_option(mode_P,  &hex_output);
1667 }
1668
1669 /* free all memory occupied by tarval. */
1670 void finish_tarval(void) {
1671         finish_strcalc();
1672         finish_fltcalc();
1673         del_set(tarvals); tarvals = NULL;
1674         del_set(values);  values = NULL;
1675 }
1676
1677 /****************************************************************************
1678  *   end of tv.c
1679  ****************************************************************************/