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