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