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