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