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