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