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