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