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