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