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