9c821badb0927e1bedae6aec6e5402aa7b3d0657
[libfirm] / tv.c
1 /*
2  * Copyright (C) 1995-2011 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) ((ir_tarval*)set_insert(tarvals, (tv), sizeof(ir_tarval), hash_tv((tv))))
82 #define FIND_TARVAL(tv) ((ir_tarval*)set_find(tarvals, (tv), sizeof(ir_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 unsigned hash_val(const void *value, size_t length);
120 static unsigned hash_tv(ir_tarval *tv);
121 static void _fail_verify(ir_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(ir_tarval *tv) __attribute__ ((unused));
131 #endif
132
133 inline static void tarval_verify(ir_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 unsigned hash_tv(ir_tarval *tv)
149 {
150         return (unsigned)((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 unsigned hash_val(const void *value, size_t length)
155 {
156         size_t i;
157         unsigned 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 ir_tarval *tv1 = (const ir_tarval*) p1;
171         const ir_tarval *tv2 = (const ir_tarval*) 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 ir_tarval *get_tarval(const void *value, size_t length, ir_mode *mode)
194 {
195         ir_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 = (char*) 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 (ir_tarval *)INSERT_TARVAL(&tv);
215 }
216
217 /**
218  * handle overflow
219  */
220 static ir_tarval *get_tarval_overflow(const void *value, size_t 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 = (char*) 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 = (char*) 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 = (char*) 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((const fp_value*) value)) {
273                         /* clip infinity to maximum value */
274                         return fc_is_negative((const fp_value*) value) ? get_mode_min(mode) : get_mode_max(mode);
275                 }
276 #endif
277
278 #ifdef SWITCH_NODENORMALS
279                 if (fc_is_subnormal((const fp_value*) 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 ir_tarval reserved_tv[6];
296
297 ir_tarval *tarval_b_false     = &reserved_tv[0];
298 ir_tarval *tarval_b_true      = &reserved_tv[1];
299 ir_tarval *tarval_bad         = &reserved_tv[2];
300 ir_tarval *tarval_undefined   = &reserved_tv[3];
301 ir_tarval *tarval_reachable   = &reserved_tv[4];
302 ir_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 ir_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 ir_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 ir_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 ir_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(ir_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(ir_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 ir_tarval *new_tarval_from_long_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 ir_tarval *new_tarval_from_double(double d, ir_mode *mode)
494 {
495         return new_tarval_from_long_double(d, mode);
496 }
497
498 /* returns non-zero if can be converted to double */
499 int tarval_is_double(ir_tarval *tv)
500 {
501         assert(tv);
502
503         return (get_mode_sort(tv->mode) == irms_float_number);
504 }
505
506 long double get_tarval_long_double(ir_tarval *tv)
507 {
508         assert(tarval_is_double(tv));
509
510         return fc_val_to_ieee754((const fp_value*) tv->value);
511 }
512
513 double get_tarval_double(ir_tarval *tv)
514 {
515         return get_tarval_long_double(tv);
516 }
517
518
519 /*
520  * Access routines for tarval fields ========================================
521  */
522
523 /* get the mode of the tarval */
524 ir_mode *(get_tarval_mode)(const ir_tarval *tv)
525 {
526         return _get_tarval_mode(tv);
527 }
528
529 /*
530  * Special value query functions ============================================
531  *
532  * These functions calculate and return a tarval representing the requested
533  * value.
534  * The functions get_mode_{Max,Min,...} return tarvals retrieved from these
535  * functions, but these are stored on initialization of the irmode module and
536  * therefore the irmode functions should be preferred to the functions below.
537  */
538
539 ir_tarval *(get_tarval_bad)(void)
540 {
541         return _get_tarval_bad();
542 }
543
544 ir_tarval *(get_tarval_undefined)(void)
545 {
546         return _get_tarval_undefined();
547 }
548
549 ir_tarval *(get_tarval_b_false)(void)
550 {
551         return _get_tarval_b_false();
552 }
553
554 ir_tarval *(get_tarval_b_true)(void)
555 {
556         return _get_tarval_b_true();
557 }
558
559 ir_tarval *(get_tarval_reachable)(void)
560 {
561         return _get_tarval_reachable();
562 }
563
564 ir_tarval *(get_tarval_unreachable)(void)
565 {
566         return _get_tarval_unreachable();
567 }
568
569 ir_tarval *get_tarval_max(ir_mode *mode)
570 {
571         const ieee_descriptor_t *desc;
572
573         assert(mode);
574         if (get_mode_n_vector_elems(mode) > 1) {
575                 /* vector arithmetic not implemented yet */
576                 return tarval_bad;
577         }
578
579         switch (get_mode_sort(mode)) {
580         case irms_control_flow:
581         case irms_memory:
582         case irms_auxiliary:
583                 panic("mode %F does not support maximum value", mode);
584
585         case irms_internal_boolean:
586                 return tarval_b_true;
587
588         case irms_float_number:
589                 desc = get_descriptor(mode);
590                 fc_get_max(desc, NULL);
591                 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
592
593         case irms_reference:
594         case irms_int_number:
595                 sc_max_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
596                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
597         }
598         return tarval_bad;
599 }
600
601 ir_tarval *get_tarval_min(ir_mode *mode)
602 {
603         const ieee_descriptor_t *desc;
604
605         assert(mode);
606         if (get_mode_n_vector_elems(mode) > 1) {
607                 /* vector arithmetic not implemented yet */
608                 return tarval_bad;
609         }
610
611         switch (get_mode_sort(mode)) {
612         case irms_control_flow:
613         case irms_memory:
614         case irms_auxiliary:
615                 panic("mode %F does not support minimum value", mode);
616
617         case irms_internal_boolean:
618                 return tarval_b_false;
619
620         case irms_float_number:
621                 desc = get_descriptor(mode);
622                 fc_get_min(desc, NULL);
623                 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
624
625         case irms_reference:
626         case irms_int_number:
627                 sc_min_from_bits(get_mode_size_bits(mode), mode_is_signed(mode), NULL);
628                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), mode);
629         }
630         return tarval_bad;
631 }
632
633 /** The bit pattern for the pointer NULL */
634 static long _null_value = 0;
635
636 ir_tarval *get_tarval_null(ir_mode *mode)
637 {
638         assert(mode);
639
640         if (get_mode_n_vector_elems(mode) > 1) {
641                 /* vector arithmetic not implemented yet */
642                 return tarval_bad;
643         }
644
645         switch (get_mode_sort(mode)) {
646         case irms_control_flow:
647         case irms_memory:
648         case irms_auxiliary:
649                 panic("mode %F does not support null value", mode);
650
651         case irms_float_number:
652                 return new_tarval_from_double(0.0, mode);
653
654         case irms_internal_boolean:
655         case irms_int_number:
656                 return new_tarval_from_long(0l,  mode);
657
658         case irms_reference:
659                 return new_tarval_from_long(_null_value, mode);
660         }
661         return tarval_bad;
662 }
663
664 ir_tarval *get_tarval_one(ir_mode *mode)
665 {
666         assert(mode);
667
668         if (get_mode_n_vector_elems(mode) > 1)
669                 panic("vector arithmetic not implemented yet");
670
671         switch (get_mode_sort(mode)) {
672         case irms_control_flow:
673         case irms_memory:
674         case irms_auxiliary:
675                 panic("mode %F does not support one value", mode);
676
677         case irms_internal_boolean:
678                 return tarval_b_true;
679
680         case irms_float_number:
681                 return new_tarval_from_double(1.0, mode);
682
683         case irms_reference:
684         case irms_int_number:
685                 return new_tarval_from_long(1l, mode);
686         }
687         return tarval_bad;
688 }
689
690 ir_tarval *get_tarval_all_one(ir_mode *mode)
691 {
692         assert(mode);
693
694         if (get_mode_n_vector_elems(mode) > 1)
695                 panic("vector arithmetic not implemented yet");
696
697         switch (get_mode_sort(mode)) {
698         case irms_control_flow:
699         case irms_memory:
700         case irms_auxiliary:
701                 panic("mode %F does not support all-one value", mode);
702
703         case irms_int_number:
704         case irms_internal_boolean:
705         case irms_reference:
706                 return tarval_not(get_mode_null(mode));
707
708
709         case irms_float_number:
710                 return new_tarval_from_double(1.0, mode);
711         }
712         return tarval_bad;
713 }
714
715 int tarval_is_constant(ir_tarval *tv)
716 {
717         int num_res = sizeof(reserved_tv) / sizeof(reserved_tv[0]);
718
719         /* reserved tarvals are NOT constants. Note that although
720            tarval_b_true and tarval_b_false are reserved, they are constants of course. */
721         return (tv < &reserved_tv[2] || tv > &reserved_tv[num_res - 1]);
722 }
723
724 ir_tarval *get_tarval_minus_one(ir_mode *mode)
725 {
726         assert(mode);
727
728         if (get_mode_n_vector_elems(mode) > 1)
729                 panic("vector arithmetic not implemented yet");
730
731         switch (get_mode_sort(mode)) {
732         case irms_control_flow:
733         case irms_memory:
734         case irms_auxiliary:
735         case irms_internal_boolean:
736                 panic("mode %F does not support minus one value", mode);
737
738         case irms_reference:
739                 return tarval_bad;
740
741         case irms_float_number:
742                 return mode_is_signed(mode) ? new_tarval_from_double(-1.0, mode) : tarval_bad;
743
744         case irms_int_number:
745                 return new_tarval_from_long(-1l, mode);
746         }
747         return tarval_bad;
748 }
749
750 ir_tarval *get_tarval_nan(ir_mode *mode)
751 {
752         const ieee_descriptor_t *desc;
753
754         assert(mode);
755         if (get_mode_n_vector_elems(mode) > 1)
756                 panic("vector arithmetic not implemented yet");
757
758         if (get_mode_sort(mode) == irms_float_number) {
759                 desc = get_descriptor(mode);
760                 fc_get_qnan(desc, NULL);
761                 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
762         } else
763                 panic("mode %F does not support NaN value", mode);
764 }
765
766 ir_tarval *get_tarval_plus_inf(ir_mode *mode)
767 {
768         assert(mode);
769         if (get_mode_n_vector_elems(mode) > 1)
770                 panic("vector arithmetic not implemented yet");
771
772         if (get_mode_sort(mode) == irms_float_number) {
773                 const ieee_descriptor_t *desc = get_descriptor(mode);
774                 fc_get_plusinf(desc, NULL);
775                 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
776         } else
777                 panic("mode %F does not support +inf value", mode);
778 }
779
780 ir_tarval *get_tarval_minus_inf(ir_mode *mode)
781 {
782         assert(mode);
783
784         if (get_mode_n_vector_elems(mode) > 1)
785                 panic("vector arithmetic not implemented yet");
786
787         if (get_mode_sort(mode) == irms_float_number) {
788                 const ieee_descriptor_t *desc = get_descriptor(mode);
789                 fc_get_minusinf(desc, NULL);
790                 return get_tarval(fc_get_buffer(), fc_get_buffer_length(), mode);
791         } else
792                 panic("mode %F does not support -inf value", mode);
793 }
794
795 /*
796  * Arithmetic operations on tarvals ========================================
797  */
798
799 /*
800  * test if negative number, 1 means 'yes'
801  */
802 int tarval_is_negative(ir_tarval *a)
803 {
804         if (get_mode_n_vector_elems(a->mode) > 1)
805                 panic("vector arithmetic not implemented yet");
806
807         switch (get_mode_sort(a->mode)) {
808         case irms_int_number:
809                 if (!mode_is_signed(a->mode)) return 0;
810                 else
811                         return sc_comp(a->value, get_mode_null(a->mode)->value) == -1 ? 1 : 0;
812
813         case irms_float_number:
814                 return fc_is_negative((const fp_value*) a->value);
815
816         default:
817                 panic("mode %F does not support negation value", a->mode);
818         }
819 }
820
821 /*
822  * test if null, 1 means 'yes'
823  */
824 int tarval_is_null(ir_tarval *a)
825 {
826         return
827                 a != tarval_bad &&
828                 a == get_mode_null(get_tarval_mode(a));
829 }
830
831 /*
832  * test if one, 1 means 'yes'
833  */
834 int tarval_is_one(ir_tarval *a)
835 {
836         return
837                 a != tarval_bad &&
838                 a == get_mode_one(get_tarval_mode(a));
839 }
840
841 int tarval_is_all_one(ir_tarval *tv)
842 {
843         return
844                 tv != tarval_bad &&
845                 tv == get_mode_all_one(get_tarval_mode(tv));
846 }
847
848 /*
849  * test if one, 1 means 'yes'
850  */
851 int tarval_is_minus_one(ir_tarval *a)
852 {
853         return
854                 a != tarval_bad &&
855                 a == get_mode_minus_one(get_tarval_mode(a));
856 }
857
858 /*
859  * comparison
860  */
861 pn_Cmp tarval_cmp(ir_tarval *a, ir_tarval *b)
862 {
863         carry_flag = -1;
864
865         if (a == tarval_bad || b == tarval_bad) {
866                 panic("Comparison with tarval_bad");
867         }
868
869         if (a == tarval_undefined || b == tarval_undefined)
870                 return pn_Cmp_False;
871
872         if (a->mode != b->mode)
873                 return pn_Cmp_False;
874
875         if (get_mode_n_vector_elems(a->mode) > 1) {
876                 /* vector arithmetic not implemented yet */
877                 panic("cmp not implemented for vector modes");
878         }
879
880         /* Here the two tarvals are unequal and of the same mode */
881         switch (get_mode_sort(a->mode)) {
882         case irms_control_flow:
883         case irms_memory:
884         case irms_auxiliary:
885                 if (a == b)
886                         return pn_Cmp_Eq;
887                 return pn_Cmp_False;
888
889         case irms_float_number:
890                 /* it should be safe to enable this even if other arithmetic is disabled */
891                 /*if (no_float)
892                         return pn_Cmp_False;*/
893                 /*
894                  * BEWARE: we cannot compare a == b here, because
895                  * a NaN is always Unordered to any other value, even to itself!
896                  */
897                 switch (fc_comp((const fp_value*) a->value, (const fp_value*) b->value)) {
898                 case -1: return pn_Cmp_Lt;
899                 case  0: return pn_Cmp_Eq;
900                 case  1: return pn_Cmp_Gt;
901                 case  2: return pn_Cmp_Uo;
902                 default: return pn_Cmp_False;
903                 }
904         case irms_reference:
905         case irms_int_number:
906                 if (a == b)
907                         return pn_Cmp_Eq;
908                 return sc_comp(a->value, b->value) == 1 ? pn_Cmp_Gt : pn_Cmp_Lt;
909
910         case irms_internal_boolean:
911                 if (a == b)
912                         return pn_Cmp_Eq;
913                 return a == tarval_b_true ? pn_Cmp_Gt : pn_Cmp_Lt;
914         }
915         return pn_Cmp_False;
916 }
917
918 /*
919  * convert to other mode
920  */
921 ir_tarval *tarval_convert_to(ir_tarval *src, ir_mode *dst_mode)
922 {
923         char                    *buffer;
924         fp_value                *res = NULL;
925         const ieee_descriptor_t *desc;
926         int                      len;
927
928         carry_flag = -1;
929
930         assert(src);
931         assert(dst_mode);
932
933         if (src->mode == dst_mode)
934                 return src;
935
936         if (get_mode_n_vector_elems(src->mode) > 1) {
937                 /* vector arithmetic not implemented yet */
938                 return tarval_bad;
939         }
940
941         switch (get_mode_sort(src->mode)) {
942         case irms_control_flow:
943         case irms_memory:
944         case irms_auxiliary:
945                 break;
946
947                 /* cast float to something */
948         case irms_float_number:
949                 switch (get_mode_sort(dst_mode)) {
950                 case irms_float_number:
951                         desc = get_descriptor(dst_mode);
952                         fc_cast((const fp_value*) src->value, desc, NULL);
953                         return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
954
955                 case irms_int_number:
956                         switch (current_float_to_int_mode) {
957                         case TRUNCATE:
958                                 res = fc_int((const fp_value*) src->value, NULL);
959                                 break;
960                         case ROUND:
961                                 res = fc_rnd((const fp_value*) src->value, NULL);
962                                 break;
963                         }
964                         buffer = (char*) alloca(sc_get_buffer_length());
965                         if (! fc_flt2int(res, buffer, dst_mode))
966                                 return tarval_bad;
967                         return get_tarval(buffer, sc_get_buffer_length(), dst_mode);
968
969                 default:
970                         /* the rest can't be converted */
971                         return tarval_bad;
972                 }
973                 break;
974
975         /* cast int/characters to something */
976         case irms_int_number:
977                 switch (get_mode_sort(dst_mode)) {
978
979                 case irms_reference:
980                 case irms_int_number:
981                         buffer = (char*) alloca(sc_get_buffer_length());
982                         memcpy(buffer, src->value, sc_get_buffer_length());
983                         sign_extend(buffer, dst_mode);
984                         return get_tarval_overflow(buffer, src->length, dst_mode);
985
986                 case irms_internal_boolean:
987                         /* XXX C semantics */
988                         if (src == get_mode_null(src->mode)) return tarval_b_false;
989                         else return tarval_b_true;
990
991                 case irms_float_number:
992                         /* XXX floating point unit does not understand internal integer
993                          * representation, convert to string first, then create float from
994                          * string */
995                         buffer = (char*) alloca(100);
996                         /* decimal string representation because hexadecimal output is
997                          * interpreted unsigned by fc_val_from_str, so this is a HACK */
998                         len = snprintf(buffer, 100, "%s",
999                                 sc_print(src->value, get_mode_size_bits(src->mode), SC_DEC, mode_is_signed(src->mode)));
1000                         buffer[100 - 1] = '\0';
1001                         desc = get_descriptor(dst_mode);
1002                         fc_val_from_str(buffer, len, desc, NULL);
1003                         return get_tarval(fc_get_buffer(), fc_get_buffer_length(), dst_mode);
1004
1005                 default:
1006                         break;
1007                 }
1008                 break;
1009
1010         case irms_internal_boolean:
1011                 /* beware: this is C semantic for the INTERNAL boolean mode */
1012                 if (get_mode_sort(dst_mode) == irms_int_number)
1013                         return src == tarval_b_true ? get_mode_one(dst_mode) : get_mode_null(dst_mode);
1014                 break;
1015
1016         case irms_reference:
1017                 if (get_mode_sort(dst_mode) == irms_int_number) {
1018                         buffer = (char*) alloca(sc_get_buffer_length());
1019                         memcpy(buffer, src->value, sc_get_buffer_length());
1020                         sign_extend(buffer, src->mode);
1021                         return get_tarval_overflow(buffer, src->length, dst_mode);
1022                 }
1023                 break;
1024         }
1025
1026         return tarval_bad;
1027 }
1028
1029 /*
1030  * bitwise negation
1031  */
1032 ir_tarval *tarval_not(ir_tarval *a)
1033 {
1034         char *buffer;
1035
1036         carry_flag = -1;
1037
1038         /* works for vector mode without changes */
1039
1040         switch (get_mode_sort(a->mode)) {
1041         case irms_reference:
1042         case irms_int_number:
1043                 buffer = (char*) alloca(sc_get_buffer_length());
1044                 sc_not(a->value, buffer);
1045                 return get_tarval(buffer, a->length, a->mode);
1046
1047         case irms_internal_boolean:
1048                 if (a == tarval_b_true)
1049                         return tarval_b_false;
1050                 if (a == tarval_b_false)
1051                         return tarval_b_true;
1052                 return tarval_bad;
1053
1054         default:
1055                 panic("bitwise negation is only allowed for integer and boolean");
1056         }
1057 }
1058
1059 /*
1060  * arithmetic negation
1061  */
1062 ir_tarval *tarval_neg(ir_tarval *a)
1063 {
1064         char *buffer;
1065
1066         assert(mode_is_num(a->mode)); /* negation only for numerical values */
1067
1068         carry_flag = -1;
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 = (char*) 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((const fp_value*) 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 ir_tarval *tarval_add(ir_tarval *a, ir_tarval *b)
1100 {
1101         char *buffer;
1102
1103         carry_flag = -1;
1104
1105         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1106                 /* vector arithmetic not implemented yet */
1107                 return tarval_bad;
1108         }
1109
1110         if (mode_is_reference(a->mode) && a->mode != b->mode) {
1111                 b = tarval_convert_to(b, a->mode);
1112         } else if (mode_is_reference(b->mode) && b->mode != a->mode) {
1113                 a = tarval_convert_to(a, b->mode);
1114         }
1115
1116         assert(a->mode == b->mode);
1117
1118         switch (get_mode_sort(a->mode)) {
1119         case irms_reference:
1120         case irms_int_number:
1121                 /* modes of a,b are equal, so result has mode of a as this might be the character */
1122                 buffer = (char*) alloca(sc_get_buffer_length());
1123                 sc_add(a->value, b->value, buffer);
1124                 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1125                 return get_tarval_overflow(buffer, a->length, a->mode);
1126
1127         case irms_float_number:
1128                 if (no_float)
1129                         return tarval_bad;
1130
1131                 fc_add((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1132                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1133
1134         default:
1135                 return tarval_bad;
1136         }
1137 }
1138
1139 /*
1140  * subtraction
1141  */
1142 ir_tarval *tarval_sub(ir_tarval *a, ir_tarval *b, ir_mode *dst_mode)
1143 {
1144         char    *buffer;
1145
1146         carry_flag = -1;
1147
1148         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(b->mode) > 1) {
1149                 /* vector arithmetic not implemented yet */
1150                 return tarval_bad;
1151         }
1152
1153         if (dst_mode != NULL) {
1154                 if (a->mode != dst_mode)
1155                         a = tarval_convert_to(a, dst_mode);
1156                 if (b->mode != dst_mode)
1157                         b = tarval_convert_to(b, dst_mode);
1158         }
1159         assert(a->mode == b->mode);
1160
1161         switch (get_mode_sort(a->mode)) {
1162         case irms_reference:
1163         case irms_int_number:
1164                 /* modes of a,b are equal, so result has mode of a as this might be the character */
1165                 buffer = (char*) alloca(sc_get_buffer_length());
1166                 sc_sub(a->value, b->value, buffer);
1167                 carry_flag = sc_get_bit_at(buffer, get_mode_size_bits(a->mode));
1168                 return get_tarval_overflow(buffer, a->length, a->mode);
1169
1170         case irms_float_number:
1171                 if (no_float)
1172                         return tarval_bad;
1173
1174                 fc_sub((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1175                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1176
1177         default:
1178                 return tarval_bad;
1179         }
1180 }
1181
1182 /*
1183  * multiplication
1184  */
1185 ir_tarval *tarval_mul(ir_tarval *a, ir_tarval *b)
1186 {
1187         char *buffer;
1188
1189         assert(a->mode == b->mode);
1190
1191         carry_flag = -1;
1192
1193         if (get_mode_n_vector_elems(a->mode) > 1) {
1194                 /* vector arithmetic not implemented yet */
1195                 return tarval_bad;
1196         }
1197
1198         switch (get_mode_sort(a->mode)) {
1199         case irms_int_number:
1200                 /* modes of a,b are equal */
1201                 buffer = (char*) alloca(sc_get_buffer_length());
1202                 sc_mul(a->value, b->value, buffer);
1203                 return get_tarval_overflow(buffer, a->length, a->mode);
1204
1205         case irms_float_number:
1206                 if (no_float)
1207                         return tarval_bad;
1208
1209                 fc_mul((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1210                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1211
1212         default:
1213                 return tarval_bad;
1214         }
1215 }
1216
1217 /*
1218  * division
1219  * overflow is impossible, but look out for division by zero
1220  */
1221 ir_tarval *tarval_div(ir_tarval *a, ir_tarval *b)
1222 {
1223         ir_mode *mode = a->mode;
1224         assert(mode == b->mode);
1225
1226         carry_flag = -1;
1227
1228         if (get_mode_n_vector_elems(mode) > 1) {
1229                 /* vector arithmetic not implemented yet */
1230                 return tarval_bad;
1231         }
1232
1233         if (mode_is_int(mode)) {
1234                 /* x/0 error */
1235                 if (b == get_mode_null(mode))
1236                         return tarval_bad;
1237
1238                 /* modes of a,b are equal */
1239                 sc_div(a->value, b->value, NULL);
1240                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1241         } else {
1242                 assert(mode_is_float(mode));
1243                 fc_div((const fp_value*) a->value, (const fp_value*) b->value, NULL);
1244                 return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), mode);
1245         }
1246 }
1247
1248 /*
1249  * remainder
1250  * overflow is impossible, but look out for division by zero
1251  */
1252 ir_tarval *tarval_mod(ir_tarval *a, ir_tarval *b)
1253 {
1254         assert((a->mode == b->mode) && mode_is_int(a->mode));
1255
1256         carry_flag = -1;
1257
1258         if (get_mode_n_vector_elems(a->mode) > 1) {
1259                 /* vector arithmetic not implemented yet */
1260                 return tarval_bad;
1261         }
1262
1263         /* x/0 error */
1264         if (b == get_mode_null(b->mode)) return tarval_bad;
1265         /* modes of a,b are equal */
1266         sc_mod(a->value, b->value, NULL);
1267         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1268 }
1269
1270 /*
1271  * integer division AND remainder
1272  * overflow is impossible, but look out for division by zero
1273  */
1274 ir_tarval *tarval_divmod(ir_tarval *a, ir_tarval *b, ir_tarval **mod)
1275 {
1276         int len = sc_get_buffer_length();
1277         char *div_res = (char*) alloca(len);
1278         char *mod_res = (char*) alloca(len);
1279
1280         assert((a->mode == b->mode) && mode_is_int(a->mode));
1281
1282         carry_flag = -1;
1283
1284         if (get_mode_n_vector_elems(a->mode) > 1) {
1285                 /* vector arithmetic not implemented yet */
1286                 return tarval_bad;
1287         }
1288
1289
1290         /* x/0 error */
1291         if (b == get_mode_null(b->mode)) return tarval_bad;
1292         /* modes of a,b are equal */
1293         sc_divmod(a->value, b->value, div_res, mod_res);
1294         *mod = get_tarval(mod_res, len, a->mode);
1295         return get_tarval(div_res, len, a->mode);
1296 }
1297
1298 /*
1299  * absolute value
1300  */
1301 ir_tarval *tarval_abs(ir_tarval *a)
1302 {
1303         char *buffer;
1304
1305         carry_flag = -1;
1306         assert(mode_is_num(a->mode));
1307
1308         if (get_mode_n_vector_elems(a->mode) > 1) {
1309                 /* vector arithmetic not implemented yet */
1310                 return tarval_bad;
1311         }
1312
1313         switch (get_mode_sort(a->mode)) {
1314         case irms_int_number:
1315                 if (sc_comp(a->value, get_mode_null(a->mode)->value) == -1) {
1316                         buffer = (char*) alloca(sc_get_buffer_length());
1317                         sc_neg(a->value, buffer);
1318                         return get_tarval_overflow(buffer, a->length, a->mode);
1319                 }
1320                 return a;
1321
1322         case irms_float_number:
1323                 /* it should be safe to enable this even if other arithmetic is disabled */
1324                 /*if (no_float)
1325                         return tarval_bad;*/
1326
1327                 if (fc_comp((const fp_value*) a->value,
1328                     (const fp_value*) get_mode_null(a->mode)->value) == -1) {
1329                         fc_neg((const fp_value*) a->value, NULL);
1330                         return get_tarval_overflow(fc_get_buffer(), fc_get_buffer_length(), a->mode);
1331                 }
1332                 return a;
1333
1334         default:
1335                 break;
1336         }
1337         return tarval_bad;
1338 }
1339
1340 /*
1341  * bitwise and
1342  */
1343 ir_tarval *tarval_and(ir_tarval *a, ir_tarval *b)
1344 {
1345         assert(a->mode == b->mode);
1346
1347         /* works even for vector modes */
1348         carry_flag = 0;
1349
1350         switch (get_mode_sort(a->mode)) {
1351         case irms_internal_boolean:
1352                 return (a == tarval_b_false) ? a : b;
1353
1354         case irms_int_number:
1355                 sc_and(a->value, b->value, NULL);
1356                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1357
1358         default:
1359                 panic("operation not defined on mode");
1360         }
1361 }
1362
1363 ir_tarval *tarval_andnot(ir_tarval *a, ir_tarval *b)
1364 {
1365         assert(a->mode == b->mode);
1366
1367         /* works even for vector modes */
1368         carry_flag = 0;
1369
1370         switch (get_mode_sort(a->mode)) {
1371         case irms_internal_boolean:
1372                 return a == tarval_b_true && b == tarval_b_false ? tarval_b_true : tarval_b_false;
1373
1374         case irms_int_number:
1375                 sc_andnot(a->value, b->value, NULL);
1376                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1377
1378         default:
1379                 panic("operation not defined on mode");
1380         }
1381 }
1382
1383 /*
1384  * bitwise or
1385  */
1386 ir_tarval *tarval_or(ir_tarval *a, ir_tarval *b)
1387 {
1388         assert(a->mode == b->mode);
1389
1390         /* works even for vector modes */
1391         carry_flag = 0;
1392
1393         switch (get_mode_sort(a->mode)) {
1394         case irms_internal_boolean:
1395                 return (a == tarval_b_true) ? a : b;
1396
1397         case irms_int_number:
1398                 sc_or(a->value, b->value, NULL);
1399                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1400
1401         default:
1402                 panic("operation not defined on mode");
1403         }
1404 }
1405
1406 /*
1407  * bitwise exclusive or (xor)
1408  */
1409 ir_tarval *tarval_eor(ir_tarval *a, ir_tarval *b)
1410 {
1411         assert((a->mode == b->mode));
1412
1413         /* works even for vector modes */
1414         carry_flag = 0;
1415
1416         switch (get_mode_sort(a->mode)) {
1417         case irms_internal_boolean:
1418                 return (a == b)? tarval_b_false : tarval_b_true;
1419
1420         case irms_int_number:
1421                 sc_xor(a->value, b->value, NULL);
1422                 return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1423
1424         default:
1425                 panic("operation not defined on mode");
1426         }
1427 }
1428
1429 /*
1430  * bitwise left shift
1431  */
1432 ir_tarval *tarval_shl(ir_tarval *a, ir_tarval *b)
1433 {
1434         char *temp_val = NULL;
1435
1436         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1437
1438         carry_flag = -1;
1439
1440         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1441                 /* vector arithmetic not implemented yet */
1442                 return tarval_bad;
1443         }
1444
1445         if (get_mode_modulo_shift(a->mode) != 0) {
1446                 temp_val = (char*) alloca(sc_get_buffer_length());
1447
1448                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1449                 sc_mod(b->value, temp_val, temp_val);
1450         } else
1451                 temp_val = (char*)b->value;
1452
1453         sc_shl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1454         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1455 }
1456
1457 /*
1458  * bitwise unsigned right shift
1459  */
1460 ir_tarval *tarval_shr(ir_tarval *a, ir_tarval *b)
1461 {
1462         char *temp_val = NULL;
1463
1464         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1465
1466         carry_flag = -1;
1467
1468         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1469                 /* vector arithmetic not implemented yet */
1470                 return tarval_bad;
1471         }
1472
1473         if (get_mode_modulo_shift(a->mode) != 0) {
1474                 temp_val = (char*) alloca(sc_get_buffer_length());
1475
1476                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1477                 sc_mod(b->value, temp_val, temp_val);
1478         } else
1479                 temp_val = (char*)b->value;
1480
1481         sc_shr(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1482         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1483 }
1484
1485 /*
1486  * bitwise signed right shift
1487  */
1488 ir_tarval *tarval_shrs(ir_tarval *a, ir_tarval *b)
1489 {
1490         char *temp_val = NULL;
1491
1492         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1493
1494         carry_flag = -1;
1495
1496         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1497                 /* vector arithmetic not implemented yet */
1498                 return tarval_bad;
1499         }
1500
1501         if (get_mode_modulo_shift(a->mode) != 0) {
1502                 temp_val = (char*) alloca(sc_get_buffer_length());
1503
1504                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1505                 sc_mod(b->value, temp_val, temp_val);
1506         } else
1507                 temp_val = (char*)b->value;
1508
1509         sc_shrs(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1510         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1511 }
1512
1513 /*
1514  * bitwise rotation to left
1515  */
1516 ir_tarval *tarval_rotl(ir_tarval *a, ir_tarval *b)
1517 {
1518         char *temp_val = NULL;
1519
1520         assert(mode_is_int(a->mode) && mode_is_int(b->mode));
1521
1522         carry_flag = -1;
1523
1524         if (get_mode_n_vector_elems(a->mode) > 1 || get_mode_n_vector_elems(a->mode) > 1) {
1525                 /* vector arithmetic not implemented yet */
1526                 return tarval_bad;
1527         }
1528
1529         if (get_mode_modulo_shift(a->mode) != 0) {
1530                 temp_val = (char*) alloca(sc_get_buffer_length());
1531
1532                 sc_val_from_ulong(get_mode_modulo_shift(a->mode), temp_val);
1533                 sc_mod(b->value, temp_val, temp_val);
1534         } else
1535                 temp_val = (char*)b->value;
1536
1537         sc_rotl(a->value, temp_val, get_mode_size_bits(a->mode), mode_is_signed(a->mode), NULL);
1538         return get_tarval(sc_get_buffer(), sc_get_buffer_length(), a->mode);
1539 }
1540
1541 /*
1542  * carry flag of the last operation
1543  */
1544 int tarval_carry(void)
1545 {
1546         if (carry_flag == -1)
1547                 panic("Carry undefined for the last operation");
1548         return carry_flag;
1549 }
1550
1551 /*
1552  * Output of tarvals
1553  */
1554 int tarval_snprintf(char *buf, size_t len, ir_tarval *tv)
1555 {
1556         static const tarval_mode_info default_info = { TVO_NATIVE, NULL, NULL };
1557
1558         const char *str;
1559         char tv_buf[100];
1560         const tarval_mode_info *mode_info;
1561         const char *prefix, *suffix;
1562
1563         mode_info = (const tarval_mode_info*) tv->mode->tv_priv;
1564         if (! mode_info)
1565                 mode_info = &default_info;
1566         prefix = mode_info->mode_prefix ? mode_info->mode_prefix : "";
1567         suffix = mode_info->mode_suffix ? mode_info->mode_suffix : "";
1568
1569         switch (get_mode_sort(tv->mode)) {
1570         case irms_reference:
1571                 if (tv == tv->mode->null) return snprintf(buf, len, "NULL");
1572                 /* FALLTHROUGH */
1573         case irms_int_number:
1574                 switch (mode_info->mode_output) {
1575
1576                 case TVO_DECIMAL:
1577                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_DEC, mode_is_signed(tv->mode));
1578                         break;
1579
1580                 case TVO_OCTAL:
1581                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_OCT, 0);
1582                         break;
1583
1584                 case TVO_NATIVE:
1585                         prefix = "0x";
1586                 case TVO_HEX:
1587                 default:
1588                         str = sc_print(tv->value, get_mode_size_bits(tv->mode), SC_HEX, 0);
1589                         break;
1590                 }
1591                 return snprintf(buf, len, "%s%s%s", prefix, str, suffix);
1592
1593         case irms_float_number:
1594                 switch (mode_info->mode_output) {
1595                 case TVO_HEX:
1596                         return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_PACKED), suffix);
1597
1598                 case TVO_HEXFLOAT:
1599                         return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_HEX), suffix);
1600
1601                 case TVO_FLOAT:
1602                 case TVO_NATIVE:
1603                 default:
1604                         return snprintf(buf, len, "%s%s%s", prefix, fc_print((const fp_value*) tv->value, tv_buf, sizeof(tv_buf), FC_DEC), suffix);
1605                 }
1606                 break;
1607
1608         case irms_internal_boolean:
1609                 switch (mode_info->mode_output) {
1610
1611                 case TVO_DECIMAL:
1612                 case TVO_OCTAL:
1613                 case TVO_HEX:
1614                 case TVO_BINARY:
1615                         return snprintf(buf, len, "%s%c%s", prefix, (tv == tarval_b_true) ? '1' : '0', suffix);
1616
1617                 case TVO_NATIVE:
1618                 default:
1619                         return snprintf(buf, len, "%s%s%s", prefix, (tv == tarval_b_true) ? "true" : "false", suffix);
1620                 }
1621
1622         case irms_control_flow:
1623         case irms_memory:
1624         case irms_auxiliary:
1625                 if (tv == tarval_bad)
1626                         return snprintf(buf, len, "<TV_BAD>");
1627                 if (tv == tarval_undefined)
1628                         return snprintf(buf, len, "<TV_UNDEF>");
1629                 if (tv == tarval_unreachable)
1630                         return snprintf(buf, len, "<TV_UNREACHABLE>");
1631                 if (tv == tarval_reachable)
1632                         return snprintf(buf, len, "<TV_REACHABLE>");
1633                 return snprintf(buf, len, "<TV_??""?>");
1634         }
1635
1636         return 0;
1637 }
1638
1639 /**
1640  * Output of tarvals to stdio.
1641  */
1642 int tarval_printf(ir_tarval *tv)
1643 {
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("%s", buf);
1650         return res;
1651 }
1652
1653 char *get_tarval_bitpattern(ir_tarval *tv)
1654 {
1655         int i, j, pos = 0;
1656         int n = get_mode_size_bits(tv->mode);
1657         int bytes = (n + 7) / 8;
1658         char *res = XMALLOCN(char, n + 1);
1659         unsigned char byte;
1660
1661         for (i = 0; i < bytes; i++) {
1662                 byte = get_tarval_sub_bits(tv, i);
1663                 for (j = 1; j < 256; j <<= 1)
1664                         if (pos < n)
1665                                 res[pos++] = j & byte ? '1' : '0';
1666         }
1667
1668         res[n] = '\0';
1669
1670         return res;
1671 }
1672
1673 /*
1674  * access to the bitpattern
1675  */
1676 unsigned char get_tarval_sub_bits(ir_tarval *tv, unsigned byte_ofs)
1677 {
1678         switch (get_mode_arithmetic(tv->mode)) {
1679         case irma_twos_complement:
1680                 return sc_sub_bits(tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1681         case irma_ieee754:
1682                 return fc_sub_bits((const fp_value*) tv->value, get_mode_size_bits(tv->mode), byte_ofs);
1683         default:
1684                 panic("get_tarval_sub_bits(): arithmetic mode not supported");
1685         }
1686 }
1687
1688 /*
1689  * Specify the output options of one mode.
1690  *
1691  * This functions stores the modinfo, so DO NOT DESTROY it.
1692  *
1693  * Returns zero on success.
1694  */
1695 int  set_tarval_mode_output_option(ir_mode *mode, const tarval_mode_info *modeinfo)
1696 {
1697         assert(mode);
1698
1699         mode->tv_priv = modeinfo;
1700         return 0;
1701 }
1702
1703 /*
1704  * Returns the output options of one mode.
1705  *
1706  * This functions returns the mode info of a given mode.
1707  */
1708 const tarval_mode_info *get_tarval_mode_output_option(ir_mode *mode)
1709 {
1710         assert(mode);
1711
1712         return (const tarval_mode_info*) mode->tv_priv;
1713 }
1714
1715 /*
1716  * Returns non-zero if a given (integer) tarval has only one single bit
1717  * set.
1718  */
1719 int tarval_is_single_bit(ir_tarval *tv)
1720 {
1721         int i, l;
1722         int bits;
1723
1724         if (!tv || tv == tarval_bad) return 0;
1725         if (! mode_is_int(tv->mode)) return 0;
1726
1727         l = get_mode_size_bytes(tv->mode);
1728         for (bits = 0, i = l - 1; i >= 0; --i) {
1729                 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1730
1731                 /* check for more than one bit in these */
1732                 if (v) {
1733                         if (v & (v-1))
1734                                 return 0;
1735                         if (++bits > 1)
1736                                 return 0;
1737                 }
1738         }
1739         return bits;
1740 }
1741
1742 /*
1743  * Return the number of set bits in a given (integer) tarval.
1744  */
1745 int get_tarval_popcount(ir_tarval *tv)
1746 {
1747         int i, l;
1748         int bits;
1749
1750         if (!tv || tv == tarval_bad) return -1;
1751         if (! mode_is_int(tv->mode)) return -1;
1752
1753         l = get_mode_size_bytes(tv->mode);
1754         for (bits = 0, i = l - 1; i >= 0; --i) {
1755                 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1756
1757                 bits += popcount(v);
1758         }
1759         return bits;
1760 }
1761
1762 /**
1763  * Return the number of the lowest set bit in a given (integer) tarval.
1764  *
1765  * @param tv    the tarval
1766  *
1767  * @return number of lowest set bit or -1 on error
1768  */
1769 int get_tarval_lowest_bit(ir_tarval *tv)
1770 {
1771         int i, l;
1772
1773         if (!tv || tv == tarval_bad) return -1;
1774         if (! mode_is_int(tv->mode)) return -1;
1775
1776         l = get_mode_size_bytes(tv->mode);
1777         for (i = 0; i < l; ++i) {
1778                 unsigned char v = get_tarval_sub_bits(tv, (unsigned)i);
1779
1780                 if (v)
1781                         return ntz(v) + i * 8;
1782         }
1783         return -1;
1784 }
1785
1786 /*
1787  * Returns non-zero if the mantissa of a floating point IEEE-754
1788  * tarval is zero (i.e. 1.0Exxx)
1789  */
1790 int tarval_ieee754_zero_mantissa(ir_tarval *tv)
1791 {
1792         assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1793         return fc_zero_mantissa((const fp_value*) tv->value);
1794 }
1795
1796 /* Returns the exponent of a floating point IEEE-754 tarval. */
1797 int tarval_ieee754_get_exponent(ir_tarval *tv)
1798 {
1799         assert(get_mode_arithmetic(tv->mode) == irma_ieee754);
1800         return fc_get_exponent((const fp_value*) tv->value);
1801 }
1802
1803 /*
1804  * Check if the tarval can be converted to the given mode without
1805  * precision loss.
1806  */
1807 int tarval_ieee754_can_conv_lossless(ir_tarval *tv, ir_mode *mode)
1808 {
1809         const ieee_descriptor_t *desc = get_descriptor(mode);
1810         return fc_can_lossless_conv_to((const fp_value*) tv->value, desc);
1811 }
1812
1813 /* Set the immediate precision for IEEE-754 results. */
1814 unsigned tarval_ieee754_set_immediate_precision(unsigned bits)
1815 {
1816         return fc_set_immediate_precision(bits);
1817 }
1818
1819 /* Returns non-zero if the result of the last IEEE-754 operation was exact. */
1820 unsigned tarval_ieee754_get_exact(void)
1821 {
1822         return fc_is_exact();
1823 }
1824
1825 /* Return the size of the mantissa in bits (including possible
1826    implicit bits) for the given mode. */
1827 unsigned tarval_ieee754_get_mantissa_size(const ir_mode *mode)
1828 {
1829         const ieee_descriptor_t *desc;
1830
1831         assert(get_mode_arithmetic(mode) == irma_ieee754);
1832         desc = get_descriptor(mode);
1833
1834         return desc->mantissa_size + desc->explicit_one;
1835 }
1836
1837 /* check if its the a floating point NaN */
1838 int tarval_is_NaN(ir_tarval *tv)
1839 {
1840         if (! mode_is_float(tv->mode))
1841                 return 0;
1842         return fc_is_nan((const fp_value*) tv->value);
1843 }
1844
1845 /* check if its the a floating point +inf */
1846 int tarval_is_plus_inf(ir_tarval *tv)
1847 {
1848         if (! mode_is_float(tv->mode))
1849                 return 0;
1850         return fc_is_inf((const fp_value*) tv->value)
1851                 && !fc_is_negative((const fp_value*) tv->value);
1852 }
1853
1854 /* check if its the a floating point -inf */
1855 int tarval_is_minus_inf(ir_tarval *tv)
1856 {
1857         if (! mode_is_float(tv->mode))
1858                 return 0;
1859         return fc_is_inf((const fp_value*) tv->value)
1860                 && fc_is_negative((const fp_value*) tv->value);
1861 }
1862
1863 /* check if the tarval represents a finite value */
1864 int tarval_is_finite(ir_tarval *tv)
1865 {
1866         if (mode_is_float(tv->mode))
1867                 return !fc_is_nan((const fp_value*) tv->value)
1868                         && !fc_is_inf((const fp_value*) tv->value);
1869         return 1;
1870 }
1871
1872 /*
1873  * Sets the overflow mode for integer operations.
1874  */
1875 void tarval_set_integer_overflow_mode(tarval_int_overflow_mode_t ov_mode)
1876 {
1877         int_overflow_mode = ov_mode;
1878 }
1879
1880 /* Get the overflow mode for integer operations. */
1881 tarval_int_overflow_mode_t tarval_get_integer_overflow_mode(void)
1882 {
1883         return int_overflow_mode;
1884 }
1885
1886 /* Enable/Disable floating point constant folding. */
1887 void tarval_enable_fp_ops(int enable)
1888 {
1889         no_float = !enable;
1890 }
1891
1892 int tarval_fp_ops_enabled(void)
1893 {
1894         return !no_float;
1895 }
1896
1897 /**
1898  * default mode_info for output as HEX
1899  */
1900 static const tarval_mode_info hex_output = {
1901         TVO_HEX,
1902         "0x",
1903         NULL,
1904 };
1905
1906 /*
1907  * Initialization of the tarval module: called before init_mode()
1908  */
1909 void init_tarval_1(long null_value, int support_quad_precision)
1910 {
1911         /* if these assertion fail, tarval_is_constant() will follow ... */
1912         assert(tarval_b_false == &reserved_tv[0] && "b_false MUST be the first reserved tarval!");
1913         assert(tarval_b_true  == &reserved_tv[1] && "b_true MUST be the second reserved tarval!");
1914
1915         _null_value = null_value;
1916
1917         /* initialize the sets holding the tarvals with a comparison function and
1918          * an initial size, which is the expected number of constants */
1919         tarvals = new_set(cmp_tv, N_CONSTANTS);
1920         values  = new_set(memcmp, N_CONSTANTS);
1921         /* calls init_strcalc() with needed size */
1922         init_fltcalc(support_quad_precision ? 112 : 64);
1923 }
1924
1925 /*
1926  * Initialization of the tarval module: called after init_mode()
1927  */
1928 void init_tarval_2(void)
1929 {
1930         tarval_bad->kind          = k_tarval;
1931         tarval_bad->mode          = mode_BAD;
1932         tarval_bad->value         = INT_TO_PTR(resid_tarval_bad);
1933
1934         tarval_undefined->kind    = k_tarval;
1935         tarval_undefined->mode    = mode_ANY;
1936         tarval_undefined->value   = INT_TO_PTR(resid_tarval_undefined);
1937
1938         tarval_b_true->kind       = k_tarval;
1939         tarval_b_true->mode       = mode_b;
1940         tarval_b_true->value      = INT_TO_PTR(resid_tarval_b_true);
1941
1942         tarval_b_false->kind      = k_tarval;
1943         tarval_b_false->mode      = mode_b;
1944         tarval_b_false->value     = INT_TO_PTR(resid_tarval_b_false);
1945
1946         tarval_unreachable->kind  = k_tarval;
1947         tarval_unreachable->mode  = mode_X;
1948         tarval_unreachable->value = INT_TO_PTR(resid_tarval_unreachable);
1949
1950         tarval_reachable->kind    = k_tarval;
1951         tarval_reachable->mode    = mode_X;
1952         tarval_reachable->value   = INT_TO_PTR(resid_tarval_reachable);
1953
1954         /*
1955          * assign output modes that are compatible with the
1956          * old implementation: Hex output
1957          */
1958         set_tarval_mode_output_option(mode_Bs, &hex_output);
1959         set_tarval_mode_output_option(mode_Bu, &hex_output);
1960         set_tarval_mode_output_option(mode_Hs, &hex_output);
1961         set_tarval_mode_output_option(mode_Hu, &hex_output);
1962         set_tarval_mode_output_option(mode_Is, &hex_output);
1963         set_tarval_mode_output_option(mode_Iu, &hex_output);
1964         set_tarval_mode_output_option(mode_Ls, &hex_output);
1965         set_tarval_mode_output_option(mode_Lu, &hex_output);
1966         set_tarval_mode_output_option(mode_P,  &hex_output);
1967 }
1968
1969 /* free all memory occupied by tarval. */
1970 void finish_tarval(void)
1971 {
1972         finish_strcalc();
1973         finish_fltcalc();
1974         del_set(tarvals); tarvals = NULL;
1975         del_set(values);  values = NULL;
1976 }
1977
1978 int (is_tarval)(const void *thing)
1979 {
1980         return _is_tarval(thing);
1981 }