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