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