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