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