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