tv: Let sc_comp() return an ir_relation instead of magic numbers.
[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) == ir_relation_greater) {
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) == ir_relation_less) {
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) == ir_relation_less) {
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) == ir_relation_less ? 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                 return fc_comp((fp_value const*)a->value, (fp_value const*)b->value);
716
717         case irms_reference:
718         case irms_int_number:
719                 if (a == b)
720                         return ir_relation_equal;
721                 return sc_comp(a->value, b->value);
722
723         case irms_internal_boolean:
724                 if (a == b)
725                         return ir_relation_equal;
726                 return a == tarval_b_true ? ir_relation_greater : ir_relation_less;
727
728         default:
729                 panic("can't compare values of mode %F", a->mode);
730         }
731 }
732
733 ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *dst_mode)
734 {
735         char                    *buffer;
736         fp_value                *res = NULL;
737         const float_descriptor_t *desc;
738         int                      len;
739
740         assert(src);
741         assert(dst_mode);
742
743         if (src->mode == dst_mode)
744                 return src;
745
746         switch (get_mode_sort(src->mode)) {
747         /* cast float to something */
748         case irms_float_number:
749                 switch (get_mode_sort(dst_mode)) {
750                 case irms_float_number:
751                         desc = get_descriptor(dst_mode);
752                         fc_cast((const fp_value*) src->value, desc, NULL);
753                         return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
754
755                 case irms_int_number:
756                         res     = fc_int((const fp_value*) src->value, NULL);
757                         buffer = (char*) alloca(sc_get_buffer_length());
758                         if (! fc_flt2int(res, buffer, dst_mode))
759                                 return tarval_bad;
760                         return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
761
762                 default:
763                         break;
764                 }
765                 /* the rest can't be converted */
766                 return tarval_bad;
767
768         /* cast int/characters to something */
769         case irms_int_number:
770                 switch (get_mode_sort(dst_mode)) {
771
772                 case irms_reference:
773                 case irms_int_number:
774                         buffer = (char*) alloca(sc_get_buffer_length());
775                         memcpy(buffer, src->value, sc_get_buffer_length());
776                         return get_tarval_overflow(buffer, src->length, dst_mode);
777
778                 case irms_internal_boolean:
779                         /* XXX C semantics */
780                         if (src == get_mode_null(src->mode)) return tarval_b_false;
781                         else return tarval_b_true;
782
783                 case irms_float_number:
784                         /* XXX floating point unit does not understand internal integer
785                          * representation, convert to string first, then create float from
786                          * string */
787                         buffer = (char*) alloca(100);
788                         /* decimal string representation because hexadecimal output is
789                          * interpreted unsigned by fc_val_from_str, so this is a HACK */
790                         len = snprintf(buffer, 100, "%s",
791                                 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
792                         buffer[100 - 1] = '\0';
793                         desc = get_descriptor(dst_mode);
794                         fc_val_from_str(buffer, len, desc, NULL);
795                         return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
796
797                 default:
798                         break;
799                 }
800                 break;
801
802         case irms_internal_boolean:
803                 /* beware: this is C semantic for the INTERNAL boolean mode */
804                 if (get_mode_sort(dst_mode) == irms_int_number)
805                         return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
806                 break;
807
808         case irms_reference:
809                 if (get_mode_sort(dst_mode) == irms_int_number) {
810                         buffer = (char*) alloca(sc_get_buffer_length());
811                         memcpy(buffer, src->value, sc_get_buffer_length());
812                         sign_extend(buffer, src->mode);
813                         return get_tarval_overflow(buffer, src->length, dst_mode);
814                 }
815                 break;
816         default:
817                 return tarval_bad;
818         }
819
820         return tarval_bad;
821 }
822
823 ir_tarval *tarval_not(ir_tarval *a)
824 {
825         char *buffer;
826
827         switch (get_mode_sort(a->mode)) {
828         case irms_reference:
829         case irms_int_number:
830                 buffer = (char*) alloca(sc_get_buffer_length());
831                 sc_not(a->value, buffer);
832                 return get_tarval(buffer, a->length, a->mode);
833
834         case irms_internal_boolean:
835                 if (a == tarval_b_true)
836                         return tarval_b_false;
837                 if (a == tarval_b_false)
838                         return tarval_b_true;
839                 return tarval_bad;
840
841         default:
842                 panic("bitwise negation is only allowed for integer and boolean");
843         }
844 }
845
846 ir_tarval *tarval_neg(ir_tarval *a)
847 {
848         char *buffer;
849
850         assert(mode_is_num(a->mode)); /* negation only for numerical values */
851
852         /* note: negation is allowed even for unsigned modes. */
853
854         switch (get_mode_sort(a->mode)) {
855         case irms_int_number:
856                 buffer = (char*) alloca(sc_get_buffer_length());
857                 sc_neg(a->value, buffer);
858                 return get_tarval_overflow(buffer, a->length, a->mode);
859
860         case irms_float_number:
861                 fc_neg((const fp_value*) a->value, NULL);
862                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
863
864         default:
865                 return tarval_bad;
866         }
867 }
868
869 ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b)
870 {
871         char *buffer;
872
873         if (mode_is_reference(a->mode) && a->mode != b->mode) {
874                 b = tarval_convert_to(b, a->mode);
875         } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
876                 a = tarval_convert_to(a, b->mode);
877         }
878
879         assert(a->mode == b->mode);
880
881         switch (get_mode_sort(a->mode)) {
882         case irms_reference:
883         case irms_int_number:
884                 /* modes of a,b are equal, so result has mode of a as this might be the character */
885                 buffer = (char*) alloca(sc_get_buffer_length());
886                 sc_add(a->value, b->value, buffer);
887                 return get_tarval_overflow(buffer, a->length, a->mode);
888
889         case irms_float_number:
890                 fc_add((const fp_value*) a->value, (const fp_value*) b->value, NULL);
891                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
892
893         default:
894                 return tarval_bad;
895         }
896 }
897
898 ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode)
899 {
900         char    *buffer;
901
902         if (dst_mode != NULL) {
903                 if (a->mode != dst_mode)
904                         a = tarval_convert_to(a, dst_mode);
905                 if (b->mode != dst_mode)
906                         b = tarval_convert_to(b, dst_mode);
907         }
908         assert(a->mode == b->mode);
909
910         switch (get_mode_sort(a->mode)) {
911         case irms_reference:
912         case irms_int_number:
913                 /* modes of a,b are equal, so result has mode of a as this might be the character */
914                 buffer = (char*) alloca(sc_get_buffer_length());
915                 sc_sub(a->value, b->value, buffer);
916                 return get_tarval_overflow(buffer, a->length, a->mode);
917
918         case irms_float_number:
919                 fc_sub((const fp_value*) a->value, (const fp_value*) b->value, NULL);
920                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
921
922         default:
923                 return tarval_bad;
924         }
925 }
926
927 ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b)
928 {
929         char *buffer;
930
931         assert(a->mode == b->mode);
932
933         switch (get_mode_sort(a->mode)) {
934         case irms_int_number:
935                 /* modes of a,b are equal */
936                 buffer = (char*) alloca(sc_get_buffer_length());
937                 sc_mul(a->value, b->value, buffer);
938                 return get_tarval_overflow(buffer, a->length, a->mode);
939
940         case irms_float_number:
941                 fc_mul((const fp_value*) a->value, (const fp_value*) b->value, NULL);
942                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
943
944         default:
945                 return tarval_bad;
946         }
947 }
948
949 ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b)
950 {
951         ir_mode *mode = a->mode;
952         assert(mode == b->mode);
953
954         if (mode_is_int(mode)) {
955                 /* x/0 error */
956                 if (b == get_mode_null(mode))
957                         return tarval_bad;
958
959                 /* modes of a,b are equal */
960                 sc_div(a->value, b->value, NULL);
961                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
962         } else {
963                 assert(mode_is_float(mode));
964                 fc_div((const fp_value*) a->value, (const fp_value*) b->value, NULL);
965                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), mode);
966         }
967 }
968
969 ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b)
970 {
971         assert((a->mode == b->mode) && mode_is_int(a->mode));
972
973         /* x/0 error */
974         if (b == get_mode_null(b->mode)) return tarval_bad;
975         /* modes of a,b are equal */
976         sc_mod(a->value, b->value, NULL);
977         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
978 }
979
980 ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod)
981 {
982         int len = sc_get_buffer_length();
983         char *div_res = (char*) alloca(len);
984         char *mod_res = (char*) alloca(len);
985
986         assert((a->mode == b->mode) && mode_is_int(a->mode));
987
988         /* x/0 error */
989         if (b == get_mode_null(b->mode)) return tarval_bad;
990         /* modes of a,b are equal */
991         sc_divmod(a->value, b->value, div_res, mod_res);
992         *mod = get_tarval(mod_res, len, a->mode);
993         return get_tarval(div_res, len, a->mode);
994 }
995
996 ir_tarval *tarval_abs(ir_tarval *a)
997 {
998         char *buffer;
999
1000         assert(mode_is_num(a->mode));
1001
1002         switch (get_mode_sort(a->mode)) {
1003         case irms_int_number:
1004                 if (sc_comp(a->value, get_mode_null(a->mode)->value) == ir_relation_less) {
1005                         buffer = (char*) alloca(sc_get_buffer_length());
1006                         sc_neg(a->value, buffer);
1007                         return get_tarval_overflow(buffer, a->length, a->mode);
1008                 }
1009                 return a;
1010
1011         case irms_float_number:
1012                 if (fc_is_negative((fp_value const*)a)) {
1013                         fc_neg((const fp_value*) a->value, NULL);
1014                         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1015                 }
1016                 return a;
1017
1018         default:
1019                 break;
1020         }
1021         return tarval_bad;
1022 }
1023
1024 ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b)
1025 {
1026         assert(a->mode == b->mode);
1027
1028         switch (get_mode_sort(a->mode)) {
1029         case irms_internal_boolean:
1030                 return (a == tarval_b_false) ? a : b;
1031
1032         case irms_reference:
1033         case irms_int_number:
1034                 sc_and(a->value, b->value, NULL);
1035                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1036
1037         default:
1038                 panic("operation not defined on mode");
1039         }
1040 }
1041
1042 ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b)
1043 {
1044         assert(a->mode == b->mode);
1045
1046         switch (get_mode_sort(a->mode)) {
1047         case irms_internal_boolean:
1048                 return a == tarval_b_true && b == tarval_b_false ? tarval_b_true : tarval_b_false;
1049
1050         case irms_reference:
1051         case irms_int_number:
1052                 sc_andnot(a->value, b->value, NULL);
1053                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1054
1055         default:
1056                 panic("operation not defined on mode");
1057         }
1058 }
1059
1060 ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b)
1061 {
1062         assert(a->mode == b->mode);
1063
1064         switch (get_mode_sort(a->mode)) {
1065         case irms_internal_boolean:
1066                 return (a == tarval_b_true) ? a : b;
1067
1068         case irms_reference:
1069         case irms_int_number:
1070                 sc_or(a->value, b->value, NULL);
1071                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1072
1073         default:
1074                 panic("operation not defined on mode");
1075         }
1076 }
1077
1078 ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b)
1079 {
1080         assert((a->mode == b->mode));
1081
1082         switch (get_mode_sort(a->mode)) {
1083         case irms_internal_boolean:
1084                 return (a == b)? tarval_b_false : tarval_b_true;
1085
1086         case irms_reference:
1087         case irms_int_number:
1088                 sc_xor(a->value, b->value, NULL);
1089                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1090
1091         default:
1092                 panic("operation not defined on mode");
1093         }
1094 }
1095
1096 ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b)
1097 {
1098         char *temp_val = NULL;
1099
1100         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1101
1102         if (get_mode_modulo_shift(a->mode) != 0) {
1103                 temp_val = (char*) alloca(sc_get_buffer_length());
1104
1105                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1106                 sc_mod(b->value, temp_val, temp_val);
1107         } else
1108                 temp_val = (char*)b->value;
1109
1110         sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1111         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1112 }
1113
1114 ir_tarval *tarval_shl_unsigned(ir_tarval *a, unsigned b)
1115 {
1116         ir_mode *mode   = a->mode;
1117         unsigned modulo = get_mode_modulo_shift(mode);
1118         if (modulo != 0)
1119                 b %= modulo;
1120         assert((unsigned)(long)b==b);
1121         sc_shlI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1122         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1123 }
1124
1125 ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b)
1126 {
1127         char *temp_val = NULL;
1128
1129         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1130
1131         if (get_mode_modulo_shift(a->mode) != 0) {
1132                 temp_val = (char*) alloca(sc_get_buffer_length());
1133
1134                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1135                 sc_mod(b->value, temp_val, temp_val);
1136         } else
1137                 temp_val = (char*)b->value;
1138
1139         sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1140         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1141 }
1142
1143 ir_tarval *tarval_shr_unsigned(ir_tarval *a, unsigned b)
1144 {
1145         ir_mode *mode   = a->mode;
1146         unsigned modulo = get_mode_modulo_shift(mode);
1147         if (modulo != 0)
1148                 b %= modulo;
1149         assert((unsigned)(long)b==b);
1150         sc_shrI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1151         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1152 }
1153
1154 ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b)
1155 {
1156         char *temp_val = NULL;
1157
1158         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1159
1160         if (get_mode_modulo_shift(a->mode) != 0) {
1161                 temp_val = (char*) alloca(sc_get_buffer_length());
1162
1163                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1164                 sc_mod(b->value, temp_val, temp_val);
1165         } else
1166                 temp_val = (char*)b->value;
1167
1168         sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1169         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1170 }
1171
1172 ir_tarval *tarval_shrs_unsigned(ir_tarval *a, unsigned b)
1173 {
1174         ir_mode *mode   = a->mode;
1175         unsigned modulo = get_mode_modulo_shift(mode);
1176         if (modulo != 0)
1177                 b %= modulo;
1178         assert((unsigned)(long)b==b);
1179         sc_shrsI(a->value, (long)b, get_mode_size_bits(mode), mode_is_signed(mode), NULL);
1180         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
1181 }
1182
1183 ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b)
1184 {
1185         char *temp_val = NULL;
1186
1187         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1188
1189         if (get_mode_modulo_shift(a->mode) != 0) {
1190                 temp_val = (char*) alloca(sc_get_buffer_length());
1191
1192                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1193                 sc_mod(b->value, temp_val, temp_val);
1194         } else
1195                 temp_val = (char*)b->value;
1196
1197         sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1198         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1199 }
1200
1201 int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
1202 {
1203         static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1204
1205         const char *str;
1206         char tv_buf[100];
1207         const tarval_mode_info *mode_info;
1208         const char *prefix, *suffix;
1209
1210         mode_info = (const tarval_mode_info*) tv->mode->tv_priv;
1211         if (! mode_info)
1212                 mode_info = &default_info;
1213         prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1214         suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1215
1216         switch (get_mode_sort(tv->mode)) {
1217         case irms_reference:
1218                 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1219                 /* FALLTHROUGH */
1220         case irms_int_number:
1221                 switch (mode_info->mode_output) {
1222
1223                 case TVO_DECIMAL:
1224                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1225                         break;
1226
1227                 case TVO_OCTAL:
1228                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1229                         break;
1230
1231                 case TVO_NATIVE:
1232                         prefix = "0x";
1233                 case TVO_HEX:
1234                 default:
1235                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1236                         break;
1237                 }
1238                 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1239
1240         case irms_float_number:
1241                 switch (mode_info->mode_output) {
1242                 case TVO_HEX:
1243                         return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1244
1245                 case TVO_HEXFLOAT:
1246                         return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1247
1248                 case TVO_FLOAT:
1249                 case TVO_NATIVE:
1250                 default:
1251                         return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1252                 }
1253
1254         case irms_internal_boolean:
1255                 switch (mode_info->mode_output) {
1256
1257                 case TVO_DECIMAL:
1258                 case TVO_OCTAL:
1259                 case TVO_HEX:
1260                 case TVO_BINARY:
1261                         return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1262
1263                 case TVO_NATIVE:
1264                 default:
1265                         return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1266                 }
1267
1268         default:
1269                 if (tv == tarval_bad)
1270                         return snprintf(buf, len, "<TV_BAD>");
1271                 else if (tv == tarval_undefined)
1272                         return snprintf(buf, len, "<TV_UNDEFINED>");
1273                 else if (tv == tarval_reachable)
1274                         return snprintf(buf, len, "<TV_REACHABLE>");
1275                 else if (tv == tarval_unreachable)
1276                         return snprintf(buf, len, "<TV_UNREACHABLE>");
1277                 else
1278                         return snprintf(buf, len, "<TV_??""?>");
1279         }
1280 }
1281
1282 int tarval_printf(ir_tarval *tv)
1283 {
1284         char buf[1024];
1285         int res;
1286
1287         res = tarval_snprintf(buf, sizeof(buf), tv);
1288         assert(res < (int) sizeof(buf) && "buffer to small for tarval_snprintf");
1289         printf("%s", buf);
1290         return res;
1291 }
1292
1293 char *get_tarval_bitpattern(ir_tarval *tv)
1294 {
1295         int i, j, pos = 0;
1296         int n = get_mode_size_bits(tv->mode);
1297         int bytes = (n + 7) / 8;
1298         char *res = XMALLOCN(char, n + 1);
1299         unsigned char byte;
1300
1301         for (i = 0; i < bytes; i++) {
1302                 byte = get_tarval_sub_bits(tv, i);
1303                 for (j = 1; j < 256; j <<= 1)
1304                         if (pos < n)
1305                                 res[pos++] = j & byte ? '1' : '0';
1306         }
1307
1308         res[n] = '\0';
1309
1310         return res;
1311 }
1312
1313 unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs)
1314 {
1315         switch (get_mode_arithmetic(tv->mode)) {
1316         case irma_twos_complement:
1317                 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1318         case irma_ieee754:
1319         case irma_x86_extended_float:
1320                 return fc_sub_bits((const fp_value*) tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1321         default:
1322                 panic("arithmetic mode not supported");
1323         }
1324 }
1325
1326 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1327 {
1328         assert(mode);
1329
1330         mode->tv_priv = modeinfo;
1331         return 0;
1332 }
1333
1334 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1335 {
1336         assert(mode);
1337
1338         return (const tarval_mode_info*) mode->tv_priv;
1339 }
1340
1341 int tarval_is_single_bit(ir_tarval *tv)
1342 {
1343         int i, l;
1344         int bits;
1345
1346         if (!tv || tv == tarval_bad) return 0;
1347         if (! mode_is_int(tv->mode)) return 0;
1348
1349         l = get_mode_size_bytes(tv->mode);
1350         for (bits = 0, i = l - 1; i >= 0; --i) {
1351                 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1352
1353                 /* check for more than one bit in these */
1354                 if (v) {
1355                         if (v & (v-1))
1356                                 return 0;
1357                         if (++bits > 1)
1358                                 return 0;
1359                 }
1360         }
1361         return bits;
1362 }
1363
1364 int get_tarval_popcount(ir_tarval *tv)
1365 {
1366         int i, l;
1367         int bits;
1368
1369         if (!tv || tv == tarval_bad) return -1;
1370         if (! mode_is_int(tv->mode)) return -1;
1371
1372         l = get_mode_size_bytes(tv->mode);
1373         for (bits = 0, i = l - 1; i >= 0; --i) {
1374                 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1375
1376                 bits += popcount(v);
1377         }
1378         return bits;
1379 }
1380
1381 int get_tarval_lowest_bit(ir_tarval *tv)
1382 {
1383         int i, l;
1384
1385         if (!tv || tv == tarval_bad) return -1;
1386         if (! mode_is_int(tv->mode)) return -1;
1387
1388         l = get_mode_size_bytes(tv->mode);
1389         for (i = 0; i < l; ++i) {
1390                 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1391
1392                 if (v)
1393                         return ntz(v) + i * 8;
1394         }
1395         return -1;
1396 }
1397
1398 int tarval_zero_mantissa(ir_tarval *tv)
1399 {
1400         assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1401                || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1402         return fc_zero_mantissa((const fp_value*) tv->value);
1403 }
1404
1405 int tarval_get_exponent(ir_tarval *tv)
1406 {
1407         assert(get_mode_arithmetic(tv->mode) == irma_ieee754
1408                || get_mode_arithmetic(tv->mode) == irma_x86_extended_float);
1409         return fc_get_exponent((const fp_value*) tv->value);
1410 }
1411
1412 int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode)
1413 {
1414         const float_descriptor_t *desc = get_descriptor(mode);
1415         return fc_can_lossless_conv_to((const fp_value*) tv->value, desc);
1416 }
1417
1418 unsigned tarval_ieee754_get_exact(void)
1419 {
1420         return fc_is_exact();
1421 }
1422
1423 int tarval_is_NaN(ir_tarval *tv)
1424 {
1425         if (! mode_is_float(tv->mode))
1426                 return 0;
1427         return fc_is_nan((const fp_value*) tv->value);
1428 }
1429
1430 int tarval_is_plus_inf(ir_tarval *tv)
1431 {
1432         if (! mode_is_float(tv->mode))
1433                 return 0;
1434         return fc_is_inf((const fp_value*) tv->value)
1435                 && !fc_is_negative((const fp_value*) tv->value);
1436 }
1437
1438 int tarval_is_minus_inf(ir_tarval *tv)
1439 {
1440         if (! mode_is_float(tv->mode))
1441                 return 0;
1442         return fc_is_inf((const fp_value*) tv->value)
1443                 && fc_is_negative((const fp_value*) tv->value);
1444 }
1445
1446 int tarval_is_finite(ir_tarval *tv)
1447 {
1448         if (mode_is_float(tv->mode))
1449                 return !fc_is_nan((const fp_value*) tv->value)
1450                         && !fc_is_inf((const fp_value*) tv->value);
1451         return 1;
1452 }
1453
1454 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)
1455 {
1456         int_overflow_mode = ov_mode;
1457 }
1458
1459 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void)
1460 {
1461         return int_overflow_mode;
1462 }
1463
1464 /**
1465  * default mode_info for output as HEX
1466  */
1467 static const tarval_mode_info hex_output = {
1468         TVO_HEX,
1469         "0x",
1470         NULL,
1471 };
1472
1473 void init_tarval_1(long null_value, int support_quad_precision)
1474 {
1475         _null_value = null_value;
1476
1477         /* initialize the sets holding the tarvals with a comparison function and
1478          * an initial size, which is the expected number of constants */
1479         tarvals = new_set(cmp_tv, N_CONSTANTS);
1480         values  = new_set(memcmp, N_CONSTANTS);
1481         /* calls init_strcalc() with needed size */
1482         init_fltcalc(support_quad_precision ? 112 : 64);
1483 }
1484
1485 void init_tarval_2(void)
1486 {
1487         tarval_bad->kind          = k_tarval;
1488         tarval_bad->mode          = mode_BAD;
1489
1490         tarval_undefined->kind    = k_tarval;
1491         tarval_undefined->mode    = mode_ANY;
1492
1493         tarval_b_true->kind       = k_tarval;
1494         tarval_b_true->mode       = mode_b;
1495
1496         tarval_b_false->kind      = k_tarval;
1497         tarval_b_false->mode      = mode_b;
1498
1499         tarval_unreachable->kind  = k_tarval;
1500         tarval_unreachable->mode  = mode_X;
1501
1502         tarval_reachable->kind    = k_tarval;
1503         tarval_reachable->mode    = mode_X;
1504
1505         /*
1506          * assign output modes that are compatible with the
1507          * old implementation: Hex output
1508          */
1509         set_tarval_mode_output_option(mode_Bs, &hex_output);
1510         set_tarval_mode_output_option(mode_Bu, &hex_output);
1511         set_tarval_mode_output_option(mode_Hs, &hex_output);
1512         set_tarval_mode_output_option(mode_Hu, &hex_output);
1513         set_tarval_mode_output_option(mode_Is, &hex_output);
1514         set_tarval_mode_output_option(mode_Iu, &hex_output);
1515         set_tarval_mode_output_option(mode_Ls, &hex_output);
1516         set_tarval_mode_output_option(mode_Lu, &hex_output);
1517         set_tarval_mode_output_option(mode_P,  &hex_output);
1518 }
1519
1520 void finish_tarval(void)
1521 {
1522         finish_strcalc();
1523         finish_fltcalc();
1524         del_set(tarvals); tarvals = NULL;
1525         del_set(values);  values = NULL;
1526 }
1527
1528 int (is_tarval)(const void *thing)
1529 {
1530         return _is_tarval(thing);
1531 }