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