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