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