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