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