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