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